using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using MySystem.PxcModels;
using Library;
using System.Threading;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;

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

        // A：前一天剩余数量
        // B：第二天释放助利宝商户数量
        // C(20)：每次递减数量
        // D(100)：释放助利宝商户最大数量
        // B = A - C < C ? D : A - C;
        
        public void Start()
        {
            Thread th = new Thread(StartFor);
            th.IsBackground = true;
            th.Start();
        }
        public void StartFor()
        {
            while (true)
            {
                if(DateTime.Now.Hour >= 15 && DateTime.Now.Hour <= 16)
                {
                    StatEveryDay(DateTime.Now.ToString("yyyyMMdd"));
                }
                Thread.Sleep(600000);
            }
        }
        public void StatEveryDay(string Date)
        {
            string check = function.ReadInstance("/HelpProfitResetMerchant/" + Date + ".txt");
            if (!string.IsNullOrEmpty(check))
            {
                return;
            }
            function.WritePage("/HelpProfitResetMerchant/", Date + ".txt", DateTime.Now.ToString());
            DateTime start = DateTime.Parse(Date.Substring(0, 4) + "-" + Date.Substring(4, 2) + "-" + Date.Substring(6, 2) + " 00:00:00");
            string Month1 = start.AddMonths(-1).ToString("yyyyMM");
            string Month2 = start.AddMonths(-2).ToString("yyyyMM");
            string Month3 = start.AddMonths(-3).ToString("yyyyMM");
            OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
            HelpProfitRelease obj = new HelpProfitRelease();
            string JsonParam = function.ReadInstance("/PublicParam/HelpProfitRelease.txt"); //json参数文件
            if(string.IsNullOrEmpty(JsonParam))
            {
                obj.MerBrandId = "1";
                obj.GetCount = 100;
                obj.ExpandCount = 20;
                obj.SourceCount = 100;
                function.WritePage("/PublicParam/", "HelpProfitRelease.txt", Newtonsoft.Json.JsonConvert.SerializeObject(obj));
            }
            else
            {
                obj = Newtonsoft.Json.JsonConvert.DeserializeObject<HelpProfitRelease>(JsonParam);
            }
            int ExpandCount = obj.ExpandCount; //膨胀数量
            int SourceCount = obj.SourceCount; //原始数量
            int GetCount = obj.GetCount; //当前实际发放数量
            string BrandId = obj.MerBrandId; //允许释放的品牌
            List<int> HelpProfitMerchantIds = RedisDbconn.Instance.GetList<int>("HelpProfitMerchantIds", 1, SourceCount);
            int CurrentLastCount = HelpProfitMerchantIds.Count; //前一天剩余数量
            //计算第二天释放助利宝商户数量
            GetCount = CurrentLastCount - ExpandCount < ExpandCount ? SourceCount : CurrentLastCount - ExpandCount;
            obj.GetCount = GetCount;
            function.WritePage("/PublicParam/", "HelpProfitRelease.txt", Newtonsoft.Json.JsonConvert.SerializeObject(obj));
            RedisDbconn.Instance.Clear("HelpProfitMerchantIds");
            DataTable dt = OtherMySqlConn.dtable("SELECT MerchantId,sum FROM( SELECT MerchantId,SUM(TradeAmount) sum FROM PosMerchantTradeSummay WHERE Id>=3809083 and MerchantId not in (select MerchantId from HelpProfitMerIds) AND BrandId in (" + BrandId + ") AND TradeMonth in ('" + Month1 + "','" + Month2 + "','" + Month3 + "') GROUP BY MerchantId)a WHERE a.sum/3 >= 20000 and a.sum/3 <= 60000 limit " + GetCount);
            foreach(DataRow dr in dt.Rows)
            {
                RedisDbconn.Instance.AddList("HelpProfitMerchantIds", int.Parse(function.CheckInt(dr["MerchantId"].ToString())));
            }
        }
    }
}