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 HaoDaExtQueryHelper
{
    public readonly static HaoDaExtQueryHelper Instance = new HaoDaExtQueryHelper();
    private HaoDaExtQueryHelper()
    {
    }

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

    private void StartWeChatDo()
    {
        while (true)
        {
            string content = RedisDbconn.Instance.RPop<string>("WeChatResultForHaoDaQueue");
            if (!string.IsNullOrEmpty(content))
            {
                JsonData json = JsonMapper.ToObject(content);
                WeChatAddInfo(int.Parse(json["MerchantId"].ToString()), json["ApplymentId"].ToString());
            }
            else
            {
                Thread.Sleep(10000);
            }
        }
    }

    public void WeChatAddInfo(int Id, string ApplymentId)
    {
        try
        {
            WebCMSEntities db = new WebCMSEntities();
            MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
            string result = WeChatFunctionForHD.Instance.QueryMerchant(ApplymentId);
            JsonData jsonObj = JsonMapper.ToObject(result);
            if (jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT")
            {
                MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
                info.Status = 1;
                merchant.Status = 1;
                info.WeChatRemark = "";
                info.WeChatSignUrl = jsonObj["qrcode_data"].ToString();
                db.SaveChanges();
                RedisDbconn.Instance.AddList("HaoDaAuthQueryHelper", "{\"MerchantId\":\"" + Id + "\",\"MchtNo\":\"" + info.MchtNo + "\",\"StoreNo\":\"" + info.StoreNo + "\"}");
            }
            else if (jsonObj["applyment_state"].ToString() == "APPLYMENT_STATE_REJECTED")
            {
                MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
                info.Status = -1;
                merchant.Status = -1;
                info.WeChatRemark = jsonObj["reject_reason"].ToString();
                db.SaveChanges();
            }
            else
            {
                RedisDbconn.Instance.AddList("WeChatResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"ApplymentId\":\"" + ApplymentId + "\"}");
            }
            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>("AlipayResultForHaoDaQueue");
            if (!string.IsNullOrEmpty(content))
            {
                JsonData json = JsonMapper.ToObject(content);
                AlipayAddInfo(int.Parse(json["MerchantId"].ToString()), json["order_no"].ToString());
            }
            else
            {
                Thread.Sleep(10000);
            }
        }
    }

    public void AlipayAddInfo(int Id, string OrderNo)
    {
        try
        {
            WebCMSEntities db = new WebCMSEntities();
            MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
            string result = AlipayFunctionForHD.Instance.AuthOrderQuery(OrderNo);
            JsonData jsonObj = JsonMapper.ToObject(result);
            if (jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["code"].ToString() == "10000")
            {
                string order_status = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["order_status"].ToString();
                if (order_status == "CONTACT_CONFIRM")
                {
                    MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
                    info.QueryCount = 1;
                    merchant.QueryCount = 1;
                    info.AlipayRemark = "";
                    info.AlipaySignUrl = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["qr_code"].ToString();
                    db.SaveChanges();
                    RedisDbconn.Instance.AddList("HaoDaAuthQueryHelper", "{\"MerchantId\":\"" + Id + "\",\"MchtNo\":\"" + info.MchtNo + "\",\"StoreNo\":\"" + info.StoreNo + "\"}");
                }
                else if (order_status == "AUDIT_REJECT" || order_status == "AUDIT_FREEZE")
                {
                    MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
                    info.QueryCount = -1;
                    merchant.QueryCount = -1;
                    info.AlipayRemark = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["msg"].ToString();
                    db.SaveChanges();
                }
                else if (order_status == "AUDIT_PASS")
                {
                    MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
                    info.QueryCount = 2;
                    merchant.QueryCount = 2;
                    info.AlipayRemark = "";
                    db.SaveChanges();
                }
                else
                {
                    RedisDbconn.Instance.AddList("AlipayResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"order_no\":\"" + OrderNo + "\"}");
                }
            }
            else
            {
                MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
                info.QueryCount = -1;
                merchant.QueryCount = -1;
                info.AlipayRemark = jsonObj["alipay_merchant_indirect_authorder_querystatus_response"]["msg"].ToString();
                db.SaveChanges();
            }
            db.Dispose();
        }
        catch (Exception ex)
        {
            LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "支付宝开户意愿申请异常");
        }
    }
}