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;
using System.Data;

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

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

    private void StartDo()
    {
        while (true)
        {
            DataTable dt = CustomerSqlConn.dtable("select Id,OutMchtNo,StoreNo from MerchantAddInfo where HdStatus=3 and BrandId=0 and OutMchtNo is not null and StoreNo is not null and ((`Status`=1 and WeChatMerchantId is null) or (`QueryCount`=1 and AliMerchantId is null)) order by Id desc", AppConfig.Base.SqlConnStr);
            foreach (DataRow dr in dt.Rows)
            {
                QueryAuthStatus(dr["Id"].ToString(), dr["OutMchtNo"].ToString(), dr["StoreNo"].ToString());
                Thread.Sleep(2000);
            }
            Thread.Sleep(10000);
        }
    }

    public void QueryAuthStatus(string MerchantId, string MchtNo, string StoreNo)
    {
        try
        {
            var Id = int.Parse(MerchantId);
            WebCMSEntities db = new WebCMSEntities();
            MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
            var jsonObj = JsonMapper.ToObject(HaoDaHelper.Instance.QueryAuthStatus(MchtNo, StoreNo, info.BrandId));
            //成功(已认证)
            if (jsonObj["resultCode"].ToString() == "1")
            {
                // MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
                var wxcheck = jsonObj["wechatVerifyState"].ToString();
                var alicheck = jsonObj["aliVerifyState"].ToString();
                info.WeChatMerchantId = jsonObj["wechatMerchantId"].ToString();
                info.AliMerchantId = jsonObj["aliMerchantId"].ToString();
                // if (wxcheck == "1")
                // {
                //     info.Status = 2;
                //     info.WeChatMerchantId = jsonObj["wechatMerchantId"].ToString();
                //     merchant.Status = 2;
                //     info.WeChatRemark = "";
                // }
                // if (alicheck == "1")
                // {
                //     info.QueryCount = 2;
                //     info.AliMerchantId = jsonObj["aliMerchantId"].ToString();
                //     merchant.QueryCount = 2;
                //     info.AlipayRemark = "";
                // }
                // if (wxcheck != "1" || alicheck != "1")
                // {
                //     RedisDbconn.Instance.AddList("HaoDaAuthQueryHelper", "{\"MerchantId\":\"" + MerchantId + "\",\"MchtNo\":\"" + info.OutMchtNo + "\",\"StoreNo\":\"" + info.StoreNo + "\"}");
                // }

                db.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "查询实名认证状态异常");
        }
    }
}