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

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






        // 统计交易额
        public void StartCheck()
        {
            Thread th = new Thread(StartCheckDo);
            th.IsBackground = true;
            th.Start();
        }
        public void StartCheckDo()
        {
            while (true)
            {
                string content = RedisDbconn.Instance.RPop<string>("StatTradeCheck2Queue");
                if(!string.IsNullOrEmpty(content))
                {
                    StatTradeAmountCheckDo(content);
                    // StatTradeAmountCheck2Do(content);
                    Thread.Sleep(100);
                }
                else
                {
                    Thread.Sleep(10000);
                }
            }
        }


        public void StatTradeAmountCheckDo(string date)
        {
            function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计交易额日志");
            try
            {
                date = date.Replace("-", "");
                List<int> userids = new List<int>();
                userids.Add(202711);
                userids.Add(215646);
                userids.Add(216006);
                userids.Add(220410);
                userids.Add(220975);
                userids.Add(223954);
                userids.Add(227153);
                userids.Add(227345);
                userids.Add(227347);
                userids.Add(227772);
                userids.Add(227889);
                userids.Add(228112);
                userids.Add(228236);
                userids.Add(228934);
                userids.Add(228979);
                userids.Add(229634);
                userids.Add(229878);
                userids.Add(231672);
                userids.Add(234055);
                userids.Add(234274);
                userids.Add(235130);
                userids.Add(236059);
                userids.Add(236570);
                userids.Add(236577);
                userids.Add(237366);
                userids.Add(238700);
                userids.Add(238701);
                userids.Add(239360);
                userids.Add(242247);
                userids.Add(243375);
                userids.Add(244562);
                userids.Add(244704);
                userids.Add(244727);
                userids.Add(244790);
                userids.Add(245127);
                userids.Add(245273);
                userids.Add(246228);
                userids.Add(246458);
                userids.Add(246500);
                userids.Add(248077);
                userids.Add(248535);
                userids.Add(248899);
                userids.Add(249313);
                userids.Add(249520);
                userids.Add(249574);
                userids.Add(249753);
                userids.Add(251083);
                userids.Add(251900);
                userids.Add(252596);
                userids.Add(252798);
                userids.Add(255414);
                userids.Add(255516);
                userids.Add(257895);
                WebCMSEntities db = new WebCMSEntities();
                KxsMainModels.WebCMSEntities kxsdb = new KxsMainModels.WebCMSEntities();
                List<UserAmountSummary> list = db.UserAmountSummary.Where(m => m.TradeDate == date && userids.Contains(m.UserId)).ToList();
                List<int> uids = list.Select(m => m.UserId).Distinct().ToList();
                var users = kxsdb.Users.Select(m => new { m.Id, m.ParentNav }).Where(m => uids.Contains(m.Id)).ToList();
                db.Dispose();
                foreach(UserAmountSummary sub in list.Where(m => m.SeoTitle == "team").ToList())
                {
                    string puidstr = "," + sub.UserId + ",";
                    List<int> subuids = users.Where(m => m.Id == sub.UserId || m.ParentNav.Contains(puidstr)).ToList().Select(m => m.Id).ToList();
                    bool op = list.Any(m => subuids.Contains(m.UserId) && m.PayMode == sub.PayMode && m.TradeDate == sub.TradeDate && m.SeoTitle == "self" && m.IsAct == sub.IsAct);
                    if(op)
                    {
                        decimal TotalAmount = list.Where(m => subuids.Contains(m.UserId) && m.PayMode == sub.PayMode && m.TradeDate == sub.TradeDate && m.SeoTitle == "self" && m.IsAct == sub.IsAct).Sum(m => m.TotalAmount);
                        int TradeCount = list.Where(m => subuids.Contains(m.UserId) && m.PayMode == sub.PayMode && m.TradeDate == sub.TradeDate && m.SeoTitle == "self" && m.IsAct == sub.IsAct).Sum(m => m.TradeCount);
                        string setField = "";
                        if(sub.TotalAmount != TotalAmount)
                        {
                            setField += "TotalAmount=" + TotalAmount + ",";
                        }
                        if(sub.TradeCount != TradeCount)
                        {
                            setField += "TradeCount=" + TradeCount + ",";
                        }
                        if(!string.IsNullOrEmpty(setField))
                        {
                            function.WriteLog("UserId:" + sub.UserId + ";  TradeDate:" + sub.TradeDate + ";  TotalAmount:" + sub.TotalAmount + "--" + TotalAmount + ";  TradeCount:" + sub.TradeCount + "--" + TradeCount + ";", "交易不一致的创客");
                            function.WriteLog("update UserAmountSummary set " + setField.TrimEnd(',') + " where Id=" + sub.Id, "要执行的sql脚本");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计交易额异常");
            }
            function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计交易额日志");
        }

        public void StatTradeAmountCheck2Do(string date)
        {
            function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计交易额日志");
            try
            {
                date = date.Replace("-", "");
                PxcModels2.WebCMSEntities db = new PxcModels2.WebCMSEntities();
                KxsMainModels.WebCMSEntities kxsdb = new KxsMainModels.WebCMSEntities();
                List<PxcModels2.UserAmountSummary> list = db.UserAmountSummary.Where(m => m.TradeDate == date).ToList();
                List<int> uids = list.Select(m => m.UserId).Distinct().ToList();
                var users = kxsdb.Users.Select(m => new { m.Id, m.ParentNav }).Where(m => uids.Contains(m.Id)).ToList();
                db.Dispose();
                foreach(PxcModels2.UserAmountSummary sub in list.Where(m => m.SeoTitle == "team").ToList())
                {
                    string puidstr = "," + sub.UserId + ",";
                    List<int> subuids = users.Where(m => m.Id == sub.UserId || m.ParentNav.Contains(puidstr)).ToList().Select(m => m.Id).ToList();
                    bool op = list.Any(m => subuids.Contains(m.UserId) && m.PayMode == sub.PayMode && m.TradeDate == sub.TradeDate && m.SeoTitle == "self" && m.IsAct == sub.IsAct);
                    if(op)
                    {
                        decimal TotalAmount = list.Where(m => subuids.Contains(m.UserId) && m.PayMode == sub.PayMode && m.TradeDate == sub.TradeDate && m.SeoTitle == "self" && m.IsAct == sub.IsAct).Sum(m => m.TotalAmount);
                        int TradeCount = list.Where(m => subuids.Contains(m.UserId) && m.PayMode == sub.PayMode && m.TradeDate == sub.TradeDate && m.SeoTitle == "self" && m.IsAct == sub.IsAct).Sum(m => m.TradeCount);
                        string setField = "";
                        if(sub.TotalAmount != TotalAmount)
                        {
                            setField += "TotalAmount=" + TotalAmount + ",";
                        }
                        if(sub.TradeCount != TradeCount)
                        {
                            setField += "TradeCount=" + TradeCount + ",";
                        }
                        if(!string.IsNullOrEmpty(setField))
                        {
                            function.WriteLog("UserId:" + sub.UserId + ";  TradeDate:" + sub.TradeDate + ";  TotalAmount:" + sub.TotalAmount + "--" + TotalAmount + ";  TradeCount:" + sub.TradeCount + "--" + TradeCount + ";", "交易不一致的创客2");
                            function.WriteLog("update UserAmountSummary set " + setField.TrimEnd(',') + " where Id=" + sub.Id, "要执行的sql脚本2");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计交易额异常");
            }
            function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计交易额日志");
        }
        
    }
}