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);
            if (result.Contains("\"applyment_id\":"))
            {
                JsonData json = JsonMapper.ToObject(result);
                string applyment_id = json["applyment_id"].ToString();
                RedisDbconn.Instance.AddList("WeChatResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"ApplymentId\":\"" + applyment_id + "\"}");
            }
            else if (result.Contains("\"message\":"))
            {
                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();
                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);
            if (jsonObj["alipay_merchant_indirect_authorder_create_response"]["code"].ToString() == "10000")
            {
                string order_no = jsonObj["alipay_merchant_indirect_authorder_create_response"]["order_no"].ToString();
                RedisDbconn.Instance.AddList("AlipayResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"order_no\":\"" + order_no + "\"}");
            }
            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_create_response"]["msg"].ToString();
                db.SaveChanges();
            }
            db.Dispose();
        }
        catch(Exception ex)
        {
            LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "支付宝开户意愿申请异常");
        }
    }
}