using System;
using System.Collections.Generic;
using Library;
using LitJson;
using System.Linq;
using MySystem.Models;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using System.Text;
using System.Threading;

namespace MySystem
{
    public class WeChatPayBackService
    {
        public readonly static WeChatPayBackService Instance = new WeChatPayBackService();
        private WeChatPayBackService()
        { }

        public void Start()
        {
            Thread th = new Thread(Listen);
            th.IsBackground = true;
            th.Start();
        }

        public void Listen()
        {
            bool op = true;
            while (op)
            {
                string content = RedisDbconn.Instance.RPop<string>("WeChatPayBack");
                if (!string.IsNullOrEmpty(content))
                {
                    try
                    {
                        dosomething(content);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信支付回调异常");
                    }
                }
                else
                {
                    Thread.Sleep(2000);
                }
            }
        }

        public void dosomething(string content)
        {
            JsonData jsonObj = JsonMapper.ToObject(content);
            if (jsonObj.Count > 0)
            {
                if (jsonObj["event_type"].ToString() == "TRANSACTION.SUCCESS")
                {
                    JsonData resource = jsonObj["resource"];
                    //开始解密
                    string WxPayResourceDecryptModel = AesGcmDecrypt(resource["associated_data"].ToString(), resource["nonce"].ToString(), resource["ciphertext"].ToString());
                    // {\"sp_mchid\":\"1611167423\",\"sub_mchid\":\"1622024882\",\"sp_appid\":\"wxe2c051b3e46c0f6f\",\"out_trade_no\":\"2022022621562926396898863\",\"transaction_id\":\"4200001412202202267619496496\",\"trade_type\":\"JSAPI\",\"trade_state\":\"SUCCESS\",\"trade_state_desc\":\"支付成功\",\"bank_type\":\"OTHERS\",\"attach\":\"\",\"success_time\":\"2022-02-26T21:56:42+08:00\",\"payer\":{\"sp_openid\":\"omawy5W6jb0pgPfuKUVs6K3bEhzk\",\"sub_openid\":\"\"},\"amount\":{\"total\":1,\"payer_total\":1,\"currency\":\"CNY\",\"payer_currency\":\"CNY\"}}
                    JsonData orderObj = JsonMapper.ToObject(WxPayResourceDecryptModel);
                    string OrderNo = orderObj["out_trade_no"].ToString();
                    string TradeNo = orderObj["transaction_id"].ToString();
                    WebCMSEntities db = new WebCMSEntities();
                    ConsumerOrderForNo forNo = db.ConsumerOrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
                    if (forNo != null)
                    {
                        ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == forNo.OrderIds && m.Status == 0);
                        if (order != null)
                        {
                            order.Status = 1;
                            order.UpdateDate = DateTime.Now;
                            order.PayMoney = order.PayMoney;
                            order.MaxDivi = order.MaxDivi;
                            order.SeoTitle = TradeNo;
                            MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
                            MerchantAddInfo addMer = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
                            order.UserId = merchant.UserId;
                            ConsumerOrders check = db.ConsumerOrders.FirstOrDefault(m => m.Id < order.Id && m.Status > 0 && m.PayMode == order.PayMode && m.MerchantId == order.MerchantId);
                            if (check != null)
                            {
                                order.QueryCount = check.QueryCount + 1;
                            }
                            else
                            {
                                order.QueryCount = 1;
                            }
                            db.SaveChanges();

                            //语音播报
                            var machines = db.PosMachines.Select(m => new { m.DeviceKind, m.BrandId, m.Detail, m.BindMerchantId }).Where(m => m.DeviceKind == "1" && m.BindMerchantId == order.MerchantId);
                            foreach (var machine in machines)
                            {
                                if (machine.BrandId == 2)
                                {
                                    TianYuVoiceHelper.Instance.doSomething(DateTime.Now.ToString("yyyyMMddHHmmssfff"), machine.Detail, order.PayMoney.ToString("f2"));
                                }
                                else if (machine.BrandId == 3)
                                {
                                    MqLinksHelper.Instance.doSomething(machine.Detail, order.PayMoney.ToString("f2"));
                                }
                            }

                            //发送订单到客小爽MQ
                            OrderMessageHelper.SendOrderMsg(order);

                            //推送交易给java
                            PosPushDataNewHelper.Trade(order, addMer);
                        }
                    }
                    db.Dispose();
                }
            }
        }

        public string AesGcmDecrypt(string associatedData, string nonce, string ciphertext)
        {
            GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine());
            AeadParameters aeadParameters = new AeadParameters(
                new KeyParameter(Encoding.UTF8.GetBytes(AppConfig.WeChatParam.AesGemKey)),
                128,
                Encoding.UTF8.GetBytes(nonce),
                Encoding.UTF8.GetBytes(associatedData));
            gcmBlockCipher.Init(false, aeadParameters);

            byte[] data = Convert.FromBase64String(ciphertext);
            byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(data.Length)];
            int length = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0);
            gcmBlockCipher.DoFinal(plaintext, length);
            return Encoding.UTF8.GetString(plaintext);
        }








        //分账队列
        public void StartProfitShare()
        {
            Thread th = new Thread(StartProfitShareListen);
            th.IsBackground = true;
            th.Start();
        }

        public void StartProfitShareListen()
        {
            bool op = true;
            while (op)
            {
                try
                {
                    WebCMSEntities db = new WebCMSEntities();
                    DateTime checkDate = DateTime.Now.AddMinutes(-1);
                    var list = db.ConsumerOrders.Select(m => new { m.Id, m.CreateDate, m.Status, m.IsAct, m.PayMode }).Where(m => m.Status == 1 && m.PayMode == 2 && m.IsAct == 1 && m.CreateDate < checkDate).OrderBy(m => m.Id).Take(10).ToList();
                    foreach (var sub in list)
                    {
                        LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "微信分账队列异常");
                        LogHelper.Instance.WriteLog("Id:" + sub.Id, "微信分账队列异常");
                        ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
                        if (order != null)
                        {
                            order.Status = 2;
                            MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
                            MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
                            LogHelper.Instance.WriteLog("IsAct:" + order.IsAct, "微信分账队列监听");
                            LogHelper.Instance.WriteLog("PayMoney:" + order.PayMoney, "微信分账队列监听");
                            LogHelper.Instance.WriteLog("MinPayMoney:" + set.MinPayMoney, "微信分账队列监听");
                            if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
                            {
                                MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
                                //添加分账接收方
                                if (merchant.Version == 0)
                                {
                                    WeChatFunction.Instance.AddReceive(merchantAdd.SubMchid, "MERCHANT_ID", WeChatFunction.Instance.MchId, WeChatFunction.Instance.MchName, "PARTNER");
                                    merchant.Version = 1;
                                    db.SaveChanges();
                                }
                                LogHelper.Instance.WriteLog("准备分账", "微信分账队列监听");
                                //发起分账
                                decimal fee = order.PayMoney; //单位：分
                                if (fee >= 1)
                                {
                                    string TradeNo = order.SeoTitle;
                                    string OrderNo = order.OrderNo;
                                    // decimal hdfee = 0.0038M * order.PayMoney * (1 - set.GetPercent - 0.01M);
                                    // hdfee = decimal.Parse(hdfee.ToString("f2"));
                                    // decimal servicefee = 0.01M * order.PayMoney - hdfee;
                                    // servicefee = servicefee * 100;
                                    // int servicefeeNum = int.Parse(servicefee.ToString("f0"));
                                    List<ReceiverList> Receivers = new List<ReceiverList>();
                                    Receivers.Add(new ReceiverList()
                                    {
                                        type = "MERCHANT_ID",  //分账接收方类型
                                        account = WeChatFunction.Instance.MchId,  //分账接收方账号
                                        amount = int.Parse(fee.ToString("f0")),  //分账金额
                                        description = "服务费",  //分账描述
                                    });
                                    LogHelper.Instance.WriteLog("TradeNo:" + TradeNo, "微信分账队列监听");
                                    LogHelper.Instance.WriteLog("OrderNo:" + OrderNo, "微信分账队列监听");
                                    LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(Receivers), "微信分账队列监听");
                                    string ProfitShareResult = WeChatFunction.Instance.ProfitShare(merchantAdd.SubMchid, TradeNo, OrderNo, Receivers);
                                    LogHelper.Instance.WriteLog("分账结果:" + ProfitShareResult, "微信分账队列监听");
                                    order.DivideLog = "请求分账日志：" + ProfitShareResult;
                                    order.DivideFlag = 1;
                                    db.SaveChanges();

                                    //开始监听分账状态
                                    Dictionary<string, object> req = new Dictionary<string, object>();
                                    req.Add("SubMchid", merchantAdd.SubMchid); //子商户号
                                    req.Add("TradeNo", TradeNo); //微信订单号
                                    req.Add("OrderNo", OrderNo); //商户订单号
                                    RedisDbconn.Instance.AddList("ProfitShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
                                }
                                else
                                {
                                    RedisDbconn.Instance.AddList("ConsumerOrders:Divi:2:List", order.Id.ToString());
                                    // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:2:" + order.MerchantId, order);
                                }
                            }
                        }
                        LogHelper.Instance.WriteLog("\n\n", "微信分账队列监听");
                    }
                    db.SaveChanges();
                    db.Dispose();
                }
                catch (Exception ex)
                {
                    LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信分账队列异常");
                }
                Thread.Sleep(2000);
            }
        }


        //触发分账接口
        public void StartDivi()
        {
            Thread th = new Thread(ListenDivi);
            th.IsBackground = true;
            th.Start();
        }

        public void ListenDivi()
        {
            bool op = true;
            while (op)
            {
                string content = RedisDbconn.Instance.RPop<string>("AddWeChatDiviQueue");
                if (!string.IsNullOrEmpty(content))
                {
                    try
                    {
                        int id = int.Parse(function.CheckInt(content));
                        WebCMSEntities db = new WebCMSEntities();
                        ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == id);
                        if (order != null)
                        {
                            MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
                            MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
                            if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
                            {
                                //发起分账
                                decimal fee = order.PayMoney; //单位：分
                                if (fee >= 1)
                                {
                                    string TradeNo = order.SeoTitle;
                                    string OrderNo = order.OrderNo;
                                    List<ReceiverList> Receivers = new List<ReceiverList>();
                                    Receivers.Add(new ReceiverList()
                                    {
                                        type = "MERCHANT_ID",  //分账接收方类型
                                        account = WeChatFunction.Instance.MchId,  //分账接收方账号
                                        amount = int.Parse(fee.ToString("f0")),  //分账金额
                                        description = "服务费",  //分账描述
                                    });
                                    LogHelper.Instance.WriteLog("TradeNo:" + TradeNo, "微信分账队列监听");
                                    LogHelper.Instance.WriteLog("OrderNo:" + OrderNo, "微信分账队列监听");
                                    LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(Receivers), "微信分账队列监听");
                                    string ProfitShareResult = WeChatFunction.Instance.ProfitShare(merchantAdd.SubMchid, TradeNo, OrderNo, Receivers);
                                    LogHelper.Instance.WriteLog("分账结果:" + ProfitShareResult, "微信分账队列监听");
                                    order.DivideLog = "请求分账日志：" + ProfitShareResult;
                                    order.DivideFlag = 1;
                                    db.SaveChanges();

                                    //开始监听分账状态
                                    Dictionary<string, object> req = new Dictionary<string, object>();
                                    req.Add("SubMchid", merchantAdd.SubMchid); //子商户号
                                    req.Add("TradeNo", TradeNo); //微信订单号
                                    req.Add("OrderNo", OrderNo); //商户订单号
                                    RedisDbconn.Instance.AddList("ProfitShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
                                }
                            }
                        }
                        db.Dispose();
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "补微信分账异常");
                    }
                }
                else
                {
                    Thread.Sleep(2000);
                }
            }
        }
    }
}