using System;
using System.Collections.Generic;
using System.Linq;
using MySystem.ReadModels;
using Library;
using LitJson;
using System.Threading;

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

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

        public void StartDo()
        {
            try
            {
                WebCMSEntities db = new WebCMSEntities();
                DateTime start = DateTime.Parse("2024-08-01 00:00:00");
                List<WifiTradeRecord> trades = db.WifiTradeRecord.Where(m => m.CreateDate >= start).ToList();
                List<string> SnNos = trades.Select(m => m.SnNo).ToList();
                List<PosMachinesTwo> poslist = db.PosMachinesTwo.Where(m => SnNos.Contains(m.PosSn)).ToList();
                foreach(WifiTradeRecord trade in trades)
                {
                    decimal TradeAmount = trade.TradeAmount;
                    bool statFlag = false;
                    PosMachinesTwo pos = poslist.FirstOrDefault(m => m.PosSn == trade.SnNo);
                    if (pos != null)
                    {
                        statFlag = db.WifiTradeRecord.Count(m => m.SnNo == trade.SnNo) > 1;
                    }

                    //开始统计
                    int Months = 0;

                    if(pos.BrandId == 23 && TradeAmount == 49) Months = 1;
                    if(pos.BrandId == 23 && TradeAmount == 79) Months = 3;
                    if(pos.BrandId == 23 && TradeAmount == 119) Months = 12;
                    if(pos.BrandId == 23 && TradeAmount == 199) Months = 24;

                    if(pos.BrandId == 24 && TradeAmount == 59) Months = 1;
                    if(pos.BrandId == 24 && TradeAmount == 159) Months = 3;
                    if(pos.BrandId == 24 && TradeAmount == 399) Months = 12;
                    if(pos.BrandId == 24 && TradeAmount == 699) Months = 24;

                    if(pos.BrandId == 25 && TradeAmount == 69) Months = 1;
                    if(pos.BrandId == 25 && TradeAmount == 199) Months = 3;
                    if(pos.BrandId == 25 && TradeAmount == 499) Months = 12;
                    if(pos.BrandId == 25 && TradeAmount == 799) Months = 24;

                    if(pos.BrandId == 26 && TradeAmount == 219) Months = 1;
                    if(pos.BrandId == 26 && TradeAmount == 449) Months = 3;
                    if(pos.BrandId == 26 && TradeAmount == 1399) Months = 12;
                    if(pos.BrandId == 26 && TradeAmount == 2698) Months = 24;

                    if(Months > 0)
                    {
                        decimal TradeAmt = TradeAmount / Months;
                        TradeAmt = PublicFunction.NumberFormat(TradeAmt);
                        string TradeMonth = trade.CreateDate.Value.AddMonths(-1).ToString("yyyyMM");
                        if(statFlag)
                        {
                            TradeDaySummary stat = db.TradeDaySummary.Where(m => m.UserId == pos.BuyUserId && m.BrandId == pos.BrandId).OrderByDescending(m => m.Id).FirstOrDefault() ?? new TradeDaySummary();
                            TradeMonth = string.IsNullOrEmpty(stat.TradeMonth) ? trade.CreateDate.Value.AddMonths(-1).ToString("yyyyMM") : stat.TradeMonth;
                        }
                        DateTime StartMonth = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
                        for (int i = 1; i <= Months; i++)
                        {
                            StartMonth = StartMonth.AddMonths(1);
                            StatTrade(pos.BuyUserId, pos.BrandId, StartMonth.ToString("yyyyMM"), TradeAmt);
                        }
                    }
                }
                db.Dispose();
            }
            catch (Exception ex)
            {
                Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步WIFI数据异常");
            }
        }

        public void StatTrade(int UserId, int BrandId, string TradeMonth, decimal TradeAmount)
        {
            PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
            PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new PxcModels.Users();
            string ParentNav = user.ParentNav;
            string TradeDate = TradeMonth + "01";
            ParentNav += "," + UserId + ",";

            PxcModels.TradeDaySummary selfStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "self");
            if (selfStat == null)
            {
                selfStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
                {
                    UserId = UserId,
                    TradeMonth = TradeMonth,
                    TradeDate = TradeDate,
                    BrandId = BrandId,
                    SeoTitle = "self",
                }).Entity;
                db.SaveChanges();
            }
            selfStat.HelpDirectTradeAmt += TradeAmount;
            if (!string.IsNullOrEmpty(ParentNav))
            {
                string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
                foreach (string NavUserIdString in ParentNavList)
                {
                    int NavUserId = int.Parse(NavUserIdString);
                    PxcModels.TradeDaySummary teamStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.SeoTitle == "team");
                    if (teamStat == null)
                    {
                        teamStat = db.TradeDaySummary.Add(new PxcModels.TradeDaySummary()
                        {
                            UserId = NavUserId,
                            TradeMonth = TradeMonth,
                            TradeDate = TradeDate,
                            BrandId = BrandId,
                            SeoTitle = "team",
                        }).Entity;
                        db.SaveChanges();
                    }
                    teamStat.HelpNonDirectTradeAmt += TradeAmount;
                    db.SaveChanges();
                }
            }
            db.Dispose();
        }
    }
}