using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using MySystem.Models.Main;
using Library;
using System.Threading;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;
using LitJson;
/// <summary>
/// 获取商户门店号、微信支付宝商户号
/// </summary>

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

        #region 
        public void Start()
        {
            Thread th = new Thread(StartDo);
            th.IsBackground = true;
            th.Start();
        }
        public void StartDo()
        {
            while (true)
            {
                string MerchantId = RedisDbconn.Instance.RPop<string>("UnionPayGetStoreNoQueue");
                if (!string.IsNullOrEmpty(MerchantId))
                {
                    DoSomeThing(MerchantId);
                    Thread.Sleep(2000);
                }
                else
                {
                    Thread.Sleep(60000);
                }
            }
        }

        public void DoSomeThing(string MerchantId)
        {
            try
            {
                int Id = int.Parse(function.CheckInt(MerchantId));
                WebCMSEntities db = new WebCMSEntities();
                MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
                MerchantInfo merInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
                if (string.IsNullOrEmpty(AddInfo.StoreNo) && string.IsNullOrEmpty(AddInfo.OutMchtNo))
                {
                    var BrhCode = HaodaUnionParam.BrhCode;
                    if (AddInfo.BrandId == 1) BrhCode = HaodaParam.BrhCode;

                    //查询商户审核状态
                    var returnInfo = HaoDaHelper.Instance.QueryMerchantStatus(BrhCode, AddInfo.MchtNo, AddInfo.BrandId);
                    var check = false;
                    JsonData jsonObj = JsonMapper.ToObject(returnInfo);
                    if (jsonObj["resultCode"].ToString() == "1")
                    {
                        var StoreNo = jsonObj["data"]["storeNo"].ToString();
                        var OutMchtNo = jsonObj["data"]["outMchtNo"].ToString();

                        if (!string.IsNullOrEmpty(StoreNo) && !string.IsNullOrEmpty(OutMchtNo))
                        {
                            AddInfo.StoreNo = StoreNo;
                            AddInfo.OutMchtNo = OutMchtNo;
                            db.SaveChanges();
                        }

                        //查询实名认证状态
                        var result = HaoDaHelper.Instance.QueryAuthStatus(AddInfo.OutMchtNo, AddInfo.StoreNo, AddInfo.BrandId);
                        JsonData jsonData = JsonMapper.ToObject(result);

                        if (jsonData["resultCode"].ToString() == "1")
                        {
                            var AliMerchantId = jsonObj["aliMerchantId"].ToString();
                            var WeChatMerchantId = jsonObj["wechatMerchantId"].ToString();

                            if (!string.IsNullOrEmpty(AliMerchantId) && !string.IsNullOrEmpty(WeChatMerchantId))
                            {
                                AddInfo.AliMerchantId = AliMerchantId;
                                AddInfo.WeChatMerchantId = WeChatMerchantId;
                                db.SaveChanges();
                            }
                            check = true;
                        }
                    }

                    if (check)
                    {
                        AddInfo.Status = 0;
                        merInfo.Status = 0;
                        db.SaveChanges();
                    }
                    else
                    {
                        RedisDbconn.Instance.AddList("UnionPayGetStoreNoQueue", MerchantId.ToString());
                    }
                }
                db.Dispose();
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "获取商户门店号队列异常");
            }
        }
        #endregion
    }
}