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

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

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

        public void StartDo()
        {
            while (true)
            {
                try
                {
                    WebCMSEntities spdb = new WebCMSEntities();
                    PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
                    MpMainModels2.WebCMSEntities mpdb = new MpMainModels2.WebCMSEntities();
                    string Month = DateTime.Now.ToString("yyyyMM");
                    string Date = DateTime.Now.ToString("yyyyMMdd");
                    DateTime start = DateTime.Now.AddDays(-5);
                    DateTime end = DateTime.Now.AddMinutes(-1);
                    int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/ActivateRecordId.txt")));
                    IQueryable<ActivateRecord> acts = spdb.ActivateRecord.Where(m => m.Id >= StartId && m.CreateDate >= start && m.CreateDate < end && m.Status == 1).OrderByDescending(m => m.Id);
                    foreach (ActivateRecord act in acts.ToList())
                    {
                        // PxcModels.MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == act.MerNo) ?? new PxcModels.MachineForMerNo();
                        PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == act.SnNo);
                        if (pos != null)
                        {
                            if(pos.BrandId == 18 || pos.BrandId == 19 || pos.BrandId == 20 || pos.BrandId == 21)
                            {
                                pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == act.SnNo);
                            }
                        }
                        if (pos != null)
                        {
                            PxcModels.KqProducts kqproduct = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId) ?? new PxcModels.KqProducts();
                            if (kqproduct.SingleDepositApi == 1)
                            {
                                if(string.IsNullOrEmpty(pos.SeoKeyword) && decimal.Parse(function.CheckNum(act.SeoTitle)) > 0) Utils.Instance.SendActData(pos);
                                pos.SeoKeyword = act.SeoTitle;
                                if(pos.BrandId == 15 || pos.BrandId == 16) //来客吧
                                {
                                    pos.Detail = ""; //清除激活奖励标记
                                    RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString()); //发开机奖励
                                }
                                db.SaveChanges();
                                PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
                                if(pos.BrandId < 18 || pos.BrandId > 21) //来客吧
                                {
                                    StatService.Instance.PreActPrize(db, pos, merchant, act.SeoTitle);
                                }
                            }

                            if(pos.BrandId == 7 || pos.BrandId == 12 || pos.BrandId == 13 || pos.BrandId == 15 || pos.BrandId == 16 || pos.BrandId == 22)
                            {
                                decimal CheckMoney = kqproduct.ActTradeAmount > 0 ? kqproduct.ActTradeAmount : 1000;
                                int CheckDays = 30;
                                SycnSpTradeService.Instance.ActPos(db, pos, CheckMoney, CheckDays);
                                if(pos.ActivationState == 1)
                                {
                                    decimal Deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
                                    DateTime now = DateTime.Now;

                                    function.WriteLog("大盟主---Deposit:" + Deposit + ";LeaderUserId:" + pos.LeaderUserId + ";pos.IsFirst:" + pos.IsFirst + ";pos.BindingTime:" + pos.BindingTime + ";now.AddDays(-CheckDays):" + now.AddDays(-CheckDays) + ";", "押金推送发放奖励监控");
                                    if (Deposit > 0 && pos.LeaderUserId > 0 && db.Leaders.Any(m => m.Id == pos.LeaderUserId && m.ExpiredDate > now) && pos.BindingTime > now.AddDays(-CheckDays) && pos.CardType < 100)
                                    {
                                        function.WriteLog("大盟主奖发放", "押金推送发放奖励监控");
                                        RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
                                    }
                                    //发放运营中心奖励
                                    if (Deposit > 0 && pos.BindingTime > now.AddDays(-CheckDays) && pos.CardType < 100)
                                    {
                                        function.WriteLog("运营中心奖发放", "押金推送发放奖励监控");
                                        RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
                                    }
                                }
                            }
                            if(pos.BrandId == 18 || pos.BrandId == 19 || pos.BrandId == 20 || pos.BrandId == 21)
                            {
                                int CheckDays = 30;
                                DateTime now = DateTime.Now;
                                PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
                                if(pos.BindingTime > now.AddDays(-CheckDays))
                                {
                                    pos.PrizeParams = pos.SeoKeyword;
                                    decimal Deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
                                    MpMainModels2.MerchantAddInfo merchantAddInfo = mpdb.MerchantAddInfo.FirstOrDefault(m => m.MchtNo == merchant.KqMerNo) ?? new MpMainModels2.MerchantAddInfo();
                                    decimal Prize = 0;
                                    if(pos.BindingTime > DateTime.Parse("2024-07-02 00:00:00"))
                                    {
                                        if(Deposit == 198)
                                        {
                                            if(merchantAddInfo.SubjectType == "SUBJECT_TYPE_SMALL")
                                            {
                                                Prize = 0;
                                            }
                                            else
                                            {
                                                Prize = 120;
                                            }
                                        }
                                        else if(Deposit == 249)
                                        {
                                            if(merchantAddInfo.SubjectType == "SUBJECT_TYPE_SMALL")
                                            {
                                                Prize = 50;
                                            }
                                            else
                                            {
                                                Prize = 150;
                                            }
                                        }
                                        else if(Deposit == 228)
                                        {

                                            if(merchantAddInfo.SubjectType == "SUBJECT_TYPE_SMALL")
                                            {
                                                Prize = 0;
                                            }
                                            else
                                            {
                                                Prize = 110;
                                            }
                                        }
                                        else if(Deposit == 380)
                                        {

                                            if(merchantAddInfo.SubjectType == "SUBJECT_TYPE_SMALL")
                                            {
                                                Prize = 100;
                                            }
                                            else
                                            {
                                                Prize = 200;
                                            }
                                        }
                                        else if(Deposit == 399)
                                        {

                                            if(merchantAddInfo.SubjectType == "SUBJECT_TYPE_SMALL")
                                            {
                                                Prize = 0;
                                            }
                                            else
                                            {
                                                Prize = 150;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if(Deposit == 198) Prize = 70;
                                        if(Deposit == 249) Prize = 100;
                                        if(Deposit == 228) Prize = 61;
                                        if(Deposit == 380) Prize = 151;
                                        if(Deposit == 399) Prize = 100;
                                    }

                                    PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new PxcModels.Users();
                                    int TopUserId = 0;
                                    if (!string.IsNullOrEmpty(user.ParentNav))
                                    {
                                        TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
                                    }
                                    pos.ActivationState = 1;
                                    pos.ActivationTime = now;
                                    merchant.ActiveStatus = 1;
                                    merchant.MerStandardDate = now;
                                    db.SaveChanges();
                                    if (Prize > 0)
                                    {
                                        StatService.Instance.doActiveReward(db, merchant, pos, pos.BuyUserId, user.ParentNav, TopUserId, Prize);
                                    }
                                    if (pos.CardType < 100)
                                    {
                                        RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
                                    }
                                    if (Deposit > 0 && pos.LeaderUserId > 0 && db.Leaders.Any(m => m.Id == pos.LeaderUserId && m.ExpiredDate > now) && pos.CardType < 100)
                                    {
                                        RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
                                    }
                                    //发放运营中心奖励
                                    if (Deposit > 0 && pos.CardType < 100)
                                    {
                                        RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
                                    }
                                }
                                else
                                {
                                    pos.ActivationState = 1;
                                    pos.ActivationTime = now;
                                    merchant.ActiveStatus = 1;
                                    merchant.MerStandardDate = now;
                                    db.SaveChanges();
                                }
                            }

                            ActivateRecord edit = spdb.ActivateRecord.FirstOrDefault(m => m.Id == act.Id);
                            if (edit != null)
                            {
                                edit.Status = 2;
                            }
                        }
                        pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == act.SnNo && m.ActivationState == 1);
                        if (pos != null)
                        {
                            ActivateRecord edit = spdb.ActivateRecord.FirstOrDefault(m => m.Id == act.Id);
                            if (edit != null)
                            {
                                edit.Status = 2;
                            }
                        }
                    }
                    spdb.SaveChanges();
                    spdb.Dispose();
                    db.SaveChanges();
                    db.Dispose();
                    mpdb.Dispose();
                }
                catch (Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP激活数据到MAIN异常");
                }
                Thread.Sleep(1000);
            }
        }
    }
}