using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Data;
using MySystem;
using MySystem.PxcModels;
using Library;

public class TestHelper
{
    public readonly static TestHelper Instance = new TestHelper();
    private TestHelper()
    {
    }
    
    public void Start()
    {
        Thread th = new Thread(setLdFee);
        th.IsBackground = true;
        th.Start();
    }

    public void StartEverDay()
    { 
        OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
        DateTime end = DateTime.Parse("2022-04-19 00:00:00");
        DateTime check = DateTime.Parse("2022-03-11");
        while (check <= end)
        {
            StatMerchantTrade(check.ToString("yyyy-MM-dd"));
            check = check.AddDays(1);
        }
    }

    private void StatMerchantTrade(string date)
    {
        function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "执行商户交易额日志");
        WebCMSEntities db = new WebCMSEntities();
        try
        {
            DataTable selfdt = OtherMySqlConn.dtable("select MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y-%m-%d') as TradeDate,sum(TradeAmount) as TradeAmount from TradeRecord group by MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y-%m-%d') order by MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y-%m-%d')");
            foreach (DataRow selfDr in selfdt.Rows)
            {
                int BrandId = int.Parse(selfDr["BrandId"].ToString());
                int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
                string TradeDate = selfDr["TradeDate"].ToString();
                TradeDate = TradeDate.Replace("-", "");
                string TradeMonth = TradeDate.Substring(0, 6);
                decimal TradeAmount = decimal.Parse(selfDr["TradeAmount"].ToString());
                PosMerchantTradeSummay merStat = db.PosMerchantTradeSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId);
                if (merStat == null)
                {
                    merStat = db.PosMerchantTradeSummay.Add(new PosMerchantTradeSummay()
                    {
                        MerchantId = MerchantId,
                        TradeMonth = TradeMonth,
                        TradeDate = TradeDate,
                        BrandId = BrandId,
                    }).Entity;
                    db.SaveChanges();
                }
                merStat.TradeAmount += TradeAmount;
                db.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户的交易额");
        }
        db.Dispose();
        function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "执行商户交易额日志");
    }

    private void setLdFee()
    {
        // DataTable dt = CustomerSqlConn.dtable("select KqMerNo,BrandId from PosMerchantInfo where Id in (select BindMerchantId from PosMachinesTwo where BrandId in (10,11) and DownFeeFlag=1 and BindingTime<'2023-08-06 00:00:00')", "server=rm-2vcs4j67tla603c9d5o.mysql.cn-chengdu.rds.aliyuncs.com;port=3306;user=KxsProfitServer;password=FrW8ZfxlcaVdm1r0;database=KxsProfitServer;charset=utf8;");
        // foreach(DataRow dr in dt.Rows)
        // {
        //     function.WriteLog(dr["KqMerNo"].ToString(), "临时设置联动费率日志");
        //     string result = SetDepositPostService.Instance.LDFeeRate(dr["KqMerNo"].ToString(), "0.0063", int.Parse(dr["BrandId"].ToString()), 0);
        //     function.WriteLog(result + "\n\n", "临时设置联动费率日志");
        // }

        // function.WriteLog("000006026200825805", "临时设置费率日志");
        // string result = SetDepositPostService.Instance.SetFeeForSft("40585414", "0.6300", "0");
        // function.WriteLog(result + "\n\n", "临时设置费率日志");

        // function.WriteLog("0000320226626450030145", "临时设置费率日志");
        // result = SetDepositPostService.Instance.LDFeeRate("110000004265069", "0.0063", 1, 3);
        // function.WriteLog(result + "\n\n", "临时设置费率日志");
        // function.WriteLog("0000320226624650409066", "临时设置费率日志");
        // result = SetDepositPostService.Instance.LDFeeRate("110000004842951", "0.0063", 1, 3);
        // function.WriteLog(result + "\n\n", "临时设置费率日志");

        // function.WriteLog("0000320226636450184893", "临时设置费率日志");
        // result = SetDepositPostService.Instance.LDFeeRate("110000004744970", "0.0063", 3, 0);
        // function.WriteLog(result + "\n\n", "临时设置费率日志");
        // function.WriteLog("0000320226636450180288", "临时设置费率日志");
        // result = SetDepositPostService.Instance.LDFeeRate("110000004760326", "0.0063", 3, 0);
        // function.WriteLog(result + "\n\n", "临时设置费率日志");
        
        // function.WriteLog("00003102702303069483466", "临时设置费率日志");
        // string result = SetDepositPostService.Instance.SetFee("M7000615224RVXMbk7", "00003102702303069483466", 0.63M, 300);
        // function.WriteLog(result + "\n\n", "临时设置费率日志");
        // function.WriteLog("00000302JBNL01471793", "临时设置费率日志");
        // result = SetDepositPostService.Instance.SetFee("M900065131JVMYUBl5", "00000302JBNL01471793", 0.63M, 0);
        // function.WriteLog(result + "\n\n", "临时设置费率日志");

        // return;
        bool op = true;
        while(op)
        {
            int page = int.Parse(function.CheckInt(function.ReadInstance("/TmpSetLdFee/page.txt")));
            if(page < 1) page = 50;
            int skip = (page - 1) * 100;
            DataTable dt = CustomerSqlConn.dtable("select KqMerNo,BrandId from PosMerchantInfo where Id in (select BindMerchantId from PosMachinesTwo where BrandId in (10,11) and DownFeeFlag=0 and BindingTime<'2023-08-06 00:00:00') limit " + skip + ",100", "server=rm-2vcs4j67tla603c9d5o.mysql.cn-chengdu.rds.aliyuncs.com;port=3306;user=KxsProfitServer;password=FrW8ZfxlcaVdm1r0;database=KxsProfitServer;charset=utf8;");
            if(dt.Rows.Count > 0)
            {
                foreach(DataRow dr in dt.Rows)
                {
                    function.WriteLog(dr["KqMerNo"].ToString(), "临时设置联动费率日志");
                    string result = SetDepositPostService.Instance.LDFeeRate(dr["KqMerNo"].ToString(), "0.0063", int.Parse(dr["BrandId"].ToString()), 3);
                    function.WriteLog(result + "\n\n", "临时设置联动费率日志");
                }
                page += 1;
                function.WritePage("/TmpSetLdFee/", "page.txt", page.ToString());
            }
            else
            {
                op = false;
                function.WriteLog("执行结束\n\n", "临时设置联动费率日志");
            }
            dt.Dispose();
            dt.Clear();
        }
    }



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

    private void setJkFee()
    {
        bool op = true;
        while(op)
        {
            int page = int.Parse(function.CheckInt(function.ReadInstance("/TmpSetJkFee/page.txt")));
            if(page < 1) page = 1;
            int skip = (page - 1) * 100;
            DataTable dt = CustomerSqlConn.dtable("select KqMerNo,KqSnNo from PosMerchantInfo where Id in (select BindMerchantId from PosMachinesTwo where BrandId in (1,3) and (DownFeeFlag=1 or UpFeeFlag=0) and BindingState=1 and BindingTime<'" + DateTime.Now.AddDays(-90).ToString("yyyy-MM-dd HH:mm:ss") + "') limit " + skip + ",100", "server=rm-2vcs4j67tla603c9d5o.mysql.cn-chengdu.rds.aliyuncs.com;port=3306;user=KxsProfitServer;password=FrW8ZfxlcaVdm1r0;database=KxsProfitServer;charset=utf8;");
            if(dt.Rows.Count > 0)
            {
                foreach(DataRow dr in dt.Rows)
                {
                    function.WriteLog(dr["KqMerNo"].ToString(), "临时设置金控费率日志");
                    function.WriteLog(dr["KqSnNo"].ToString(), "临时设置金控费率日志");
                    string merNo = dr["KqMerNo"].ToString();
                    string sn = dr["KqSnNo"].ToString();
                    string result = SetDepositPostService.Instance.SetFee(merNo, sn, 0.63M, 0);
                    function.WriteLog(result + "\n\n", "临时设置金控费率日志");
                }
                page += 1;
                function.WritePage("/TmpSetJkFee/", "page.txt", page.ToString());
            }
            else
            {
                op = false;
                function.WriteLog("执行结束\n\n", "临时设置金控费率日志");
            }
            dt.Dispose();
            dt.Clear();
        }
    }


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

    private void setSftFee()
    {
        bool op = true;
        while(op)
        {
            int page = int.Parse(function.CheckInt(function.ReadInstance("/TmpSetSftFee/page.txt")));
            if(page < 1) page = 1;
            int skip = (page - 1) * 100;
            DataTable dt = CustomerSqlConn.dtable("select mer.KqMerNo,DownFeeFlag,UpFeeFlag from (select BindMerchantId,DownFeeFlag,UpFeeFlag from PosMachinesTwo where BrandId=7 and BindingState=1 and BindingTime<'" + DateTime.Now.AddDays(-90).ToString("yyyy-MM-dd HH:mm:ss") + "') tb left join PosMerchantInfo mer on tb.BindMerchantId=mer.Id limit " + skip + ",100", "server=rm-2vcs4j67tla603c9d5o.mysql.cn-chengdu.rds.aliyuncs.com;port=3306;user=KxsProfitServer;password=FrW8ZfxlcaVdm1r0;database=KxsProfitServer;charset=utf8;");
            if(dt.Rows.Count > 0)
            {
                foreach(DataRow dr in dt.Rows)
                {
                    function.WriteLog(dr["KqMerNo"].ToString(), "临时设置盛付通费率日志");
                    string merNo = dr["KqMerNo"].ToString();
                    string DownFeeFlag = dr["DownFeeFlag"].ToString();
                    string UpFeeFlag = dr["UpFeeFlag"].ToString();
                    string addRate = "0";
                    string Fee = "0.6300";
                    if(UpFeeFlag == "1" && DownFeeFlag == "0")
                    {
                        addRate = "3";
                    }
                    string result = SetDepositPostService.Instance.SetFeeForSft(merNo, Fee, addRate);
                    function.WriteLog(result + "\n\n", "临时设置盛付通费率日志");
                }
                page += 1;
                function.WritePage("/TmpSetSftFee/", "page.txt", page.ToString());
            }
            else
            {
                op = false;
                function.WriteLog("执行结束\n\n", "临时设置盛付通费率日志");
            }
            dt.Dispose();
            dt.Clear();
        }
    }

}