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

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

    private void StartDo()
    {
        while (true)
        {
            string content = RedisDbconn.Instance.RPop<string>("HaoDaFeeQueue");
            if (!string.IsNullOrEmpty(content))
            {
                SetFee(content);
            }
            else
            {
                Thread.Sleep(10000);
            }
        }
    }

    public void SetFee(string data)
    {
        try
        {
            string[] list = data.Split('|');
            int Id = int.Parse(list[0]);
            string FeeRate = list[1];
            WebCMSEntities db = new WebCMSEntities();
            MerchantAddInfo addinfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id);
            MerchantInfo info = db.MerchantInfo.FirstOrDefault(m => m.Id == Id);
            if(info != null && addinfo != null)
            {
                // string FeeRate = addinfo.FeeRate;
                if(string.IsNullOrEmpty(FeeRate)) FeeRate = "0.38";
                bool op = true;
                int channel = int.Parse(function.CheckInt(addinfo.AgentName));
                int reqCount = 0;
                while(op)
                {
                    string result = HaoDaHelper.Instance.SetFee(addinfo.MchtNo, FeeRate, addinfo.BrandId, channel.ToString());
                    JsonData jsonObj = JsonMapper.ToObject(result);
                    result = result.Replace("null", "\"\"");
                    int Status = 0;
                    string Remark = "";
                    string resultCode = jsonObj["resultCode"].ToString();
                    if(resultCode != "1")
                    {
                        Remark = jsonObj["errorDesc"].ToString();
                    }
                    else
                    {
                        Status = 1;
                        addinfo.FeeRate = FeeRate;
                        op = false;
                    }
                    db.MerchantFeeRecord.Add(new MerchantFeeRecord()
                    {
                        Status = Status,
                        CreateDate = DateTime.Now,
                        SettleFee = FeeRate,
                        MerchantId = addinfo.Id,
                        UserId = info.UserId,
                        Remark = Remark,
                    });
                    db.SaveChanges();

                    reqCount += 1;
                    if(reqCount >= 5)
                    {
                        op = false;
                    }
                    if(Remark == "无权限的操作")
                    {
                        channel += 1;
                        if(channel > 5) channel = 1;
                    }
                }
            }            
            db.Dispose();
        }
        catch (Exception ex)
        {
            LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "修改商户费率异常");
        }
    }
}