using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.Extensions.Hosting;
using MySystem;
using MySystem.Models;
using LitJson;
using Library;
using MySystem.Models.Main;

public class HaoDaExtHelper
{
    public readonly static HaoDaExtHelper Instance = new HaoDaExtHelper();
    private HaoDaExtHelper()
    {
    }

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

    private void StartWeChatDo()
    {
        while (true)
        {
            string content = RedisDbconn.Instance.RPop<string>("WeChatForHaoDaQueue");
            if (!string.IsNullOrEmpty(content))
            {
                WeChatAddInfo(int.Parse(content));
            }
            else
            {
                Thread.Sleep(10000);
            }
        }
    }

    public void WeChatAddInfo(int Id)
    {
        try
        {
            WebCMSEntities db = new WebCMSEntities();
            MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
            string result = WeChatFunctionForHD.Instance.MerchantApply(info);
            LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
            LogHelper.Instance.WriteLog(result, "提交微信商户意愿申请单");
            if (result.Contains("\"applyment_id\":"))
            {
                LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
                LogHelper.Instance.WriteLog("success", "提交微信商户意愿申请单");
                JsonData json = JsonMapper.ToObject(result);
                string applyment_id = json["applyment_id"].ToString();
                info.ApplymentId = applyment_id;
                db.SaveChanges();
                // RedisDbconn.Instance.AddList("WeChatResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"ApplymentId\":\"" + applyment_id + "\"}");
                // RedisDbconn.Instance.AddList("WeChatAuthResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\"}");
                LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
                LogHelper.Instance.WriteLog("end", "提交微信商户意愿申请单");
            }
            else if (result.Contains("\"message\":"))
            {
                LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
                LogHelper.Instance.WriteLog("false", "提交微信商户意愿申请单");
                MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
                JsonData jsonObj = JsonMapper.ToObject(result);
                info.Status = -1;
                merchant.Status = -1;
                info.WeChatRemark = jsonObj["message"].ToString();
                LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
                LogHelper.Instance.WriteLog("end", "提交微信商户意愿申请单");
                db.SaveChanges();
            }
            db.Dispose();
        }
        catch (Exception ex)
        {
            LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "微信开户意愿申请异常");
        }
    }



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

    private void StartAlipayDo()
    {
        while (true)
        {
            string content = RedisDbconn.Instance.RPop<string>("AlipayForHaoDaQueue");
            if (!string.IsNullOrEmpty(content))
            {
                AlipayAddInfo(int.Parse(content));
            }
            else
            {
                Thread.Sleep(10000);
            }
        }
    }

    public void AlipayAddInfo(int Id)
    {
        try
        {
            WebCMSEntities db = new WebCMSEntities();
            MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
            string result = AlipayFunctionForHD.Instance.AuthOrderCreate(info, info.AlipayAuthToken);
            JsonData jsonObj = JsonMapper.ToObject(result);
            LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
            LogHelper.Instance.WriteLog(result, "提交支付宝商户意愿申请单");
            if (jsonObj["alipay_merchant_indirect_authorder_create_response"]["code"].ToString() == "10000")
            {
                LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
                LogHelper.Instance.WriteLog("success", "提交支付宝商户意愿申请单");
                string order_no = jsonObj["alipay_merchant_indirect_authorder_create_response"]["order_no"].ToString();
                info.SeoTitle = order_no;
                db.SaveChanges();
                // RedisDbconn.Instance.AddList("AlipayResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"order_no\":\"" + order_no + "\"}");
                // RedisDbconn.Instance.AddList("AlipayAuthResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\"}");
                LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
                LogHelper.Instance.WriteLog("end", "提交支付宝商户意愿申请单");
            }
            else
            {
                LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
                LogHelper.Instance.WriteLog("false", "提交支付宝商户意愿申请单");
                MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
                info.QueryCount = -1;
                merchant.QueryCount = -1;
                string msg = jsonObj["alipay_merchant_indirect_authorder_create_response"]["msg"].ToString();
                if(result.Contains("\"sub_msg\""))
                {
                    msg = jsonObj["alipay_merchant_indirect_authorder_create_response"]["sub_msg"].ToString();
                }
                info.AlipayRemark = msg;
                LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
                LogHelper.Instance.WriteLog("end", "提交支付宝商户意愿申请单");
                db.SaveChanges();
            }
            db.Dispose();
        }
        catch (Exception ex)
        {
            LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "支付宝开户意愿申请异常");
        }
    }
}