using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Serializer;
using System;
using System.Collections.Generic;

namespace Hncore.Pass.PaymentCenter.Domain
{
    public partial class PaymentRecord
    {
        public int Id { get; set; }
        public int TenantId { get; set; }
        public int StoreId { get; set; }
        public DateTime RequestTime { get; set; } = DateTime.Now;
        public string RequestParams { get; set; }
        public PaymentStatus PaymentStatus { get; set; }
        public string PaymentNumber { get; set; }
        public string PaymentCompletionTime { get; set; }
        public int PaymentTotal { get; set; }
        public PaymentType PaymentType { get; set; }
        public int CallbackNumber { get; set; }
        public string CallbackUrl { get; set; }
        public string Openid { get; set; }
        public string Appid { get; set; }
        public string AppName { get; set; }
        public string Body { get; set; }
        public string OrderId { get; set; }
        public DateTime? CreateTime { get; set; } = DateTime.Now;
        public DateTime? UpdateTime { get; set; } = DateTime.Now;
        public int DeleteTag { get; set; } = 0;
        public int BusinessType { get; set; }
        public OrderType OrderType { get; set; }
        public int FromPos { get; set; }
        public int TaskStatus { get; set; }
        public PaymentChannel PaymentChannel { get; set; }
        public string GenSource { get; set; }
        public DateTime? PaySuccessTime { get; set; }
        public string RefundTotalFee { get; set; }
        public PaymentMethod PaymentMethod { get; set; }
        public CallbackStatus CallbackStatus { get; set; }
        public string TransactionId { get; set; }

        public string Attach { get; set; }

        /// <summary>
        /// 设置为支付成功
        /// </summary>
        /// <param name="transactionId">平台交易单号</param>
        /// <param name="paySuccessTime">付款成功时间</param>
        public void SetPaySuccessed(string transactionId, DateTime? paySuccessTime)
        {
            this.PaymentStatus = PaymentStatus.OkPay;
            this.UpdateTime = DateTime.Now;
            this.PaymentCompletionTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            if (paySuccessTime != null)
            {
                this.PaySuccessTime = paySuccessTime;
            }

            if (!string.IsNullOrEmpty(transactionId))
            {
                this.TransactionId = transactionId;
            }

            LogHelper.Info("支付记录设置为支——付成功", this.ToJson());
        }

        /// <summary>
        /// 设置为支付中
        /// </summary>
        public void SetPaying()
        {
            if (this.PaymentStatus == PaymentStatus.OkPay)
            {
                return;
            }

            this.PaymentStatus = PaymentStatus.Paying;
            this.UpdateTime = DateTime.Now;

            LogHelper.Info("支付记录设置为——支付中", this.ToJson());
        }

        /// <summary>
        /// 设置为支付失败
        /// </summary>
        public void SetPayFailed()
        {
            if (this.PaymentStatus == PaymentStatus.OkPay)
            {
                return;
            }

            this.PaymentStatus = PaymentStatus.Fail;
            this.UpdateTime = DateTime.Now;
            this.PaymentCompletionTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 根据支付平台的tradetype设置PaymentType
        /// </summary>
        /// <param name="tradeType"></param>
        public void SetPaymentTypeFromTradeType(string tradeType)
        {
            if (this.PaymentChannel == PaymentChannel.UnionpayAggregateRoot)
            {
                this.PaymentType = PaymentType.OfflinePayCash;

                if (tradeType == "pay.weixin.micropay") //微信付款码支付
                {
                    this.PaymentType = PaymentType.OnlinePayWechart;
                    this.PaymentMethod = PaymentMethod.WechatSwipeCardPay;
                }

                if (tradeType == "pay.alipay.micropay") //支付宝付款码
                {
                    this.PaymentType = PaymentType.OnlineAlipay;
                    this.PaymentMethod = PaymentMethod.AliSwipeCardPay;
                }

                if (tradeType == "pay.alipay.jspay") //支付宝扫码
                {
                    this.PaymentType = PaymentType.OnlineAlipay;
                    this.PaymentMethod = PaymentMethod.AliQrPay;
                }

                if (tradeType == "pay.weixin.jspay") //微信扫码
                {
                    this.PaymentType = PaymentType.OnlinePayWechart;
                    this.PaymentMethod = PaymentMethod.WechatJsAppPay;
                }

                //支付宝支付窗支付
                if (tradeType == "pay.alipay.native")
                {
                    this.PaymentType = PaymentType.OnlineAlipay;
                    this.PaymentMethod = PaymentMethod.AliJsPay;
                }
            }

            if ((this.PaymentChannel == PaymentChannel.WeiFuTong || this.PaymentChannel == PaymentChannel.QuanFuTong)
                && this.FromPos ==1)
            {
                this.PaymentType = PaymentType.OfflinePayCash;

                //微信付款码支付
                if (tradeType == "pay.weixin.micropay" || tradeType == "pay.wechat.micropay")
                {
                    this.PaymentType = PaymentType.OnlinePayWechart;
                    this.PaymentMethod = PaymentMethod.WechatSwipeCardPay;
                }

                //微信公众号支付
                if (tradeType == "pay.weixin.jspay" || tradeType == "pay.wechat.jspay")
                {
                    this.PaymentType = PaymentType.OnlinePayWechart;
                    this.PaymentMethod = PaymentMethod.WechatJsAppPay;
                }

                //微信扫码支付
                if (tradeType == "pay.weixin.native" || tradeType == "pay.wechat.native")
                {
                    this.PaymentType = PaymentType.OnlinePayWechart;
                    this.PaymentMethod = PaymentMethod.WechatQrPay;
                }

                //支付宝付款码支付
                if (tradeType == "pay.alipay.micropay")
                {
                    this.PaymentType = PaymentType.OnlineAlipay;
                    this.PaymentMethod = PaymentMethod.AliSwipeCardPay;
                }

                //支付宝扫码支付
                if (tradeType == "pay.alipay.native")
                {
                    this.PaymentType = PaymentType.OnlineAlipay;
                    this.PaymentMethod = PaymentMethod.AliQrPay;
                }

                //银联支付
                if (tradeType == "pay.unionpay.micropay")
                {
                    this.PaymentType = PaymentType.OnlinePayWechart;
                }

                //支付宝支付窗支付
                if (tradeType == "pay.alipay.jspay")
                {
                    this.PaymentType = PaymentType.OnlineAlipay;
                    this.PaymentMethod = PaymentMethod.AliJsPay;
                }
            }
        }

    }
}
