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

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

        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();
                    DateTime start = DateTime.Now.AddDays(-10);
                    int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/TradeRecordId.txt")));
                    IQueryable<TradeRecord> trades = spdb.TradeRecord.Where(m => m.Id >= StartId && m.CreateDate >= start && m.Status == 1).OrderBy(m => m.CreateDate).Take(20);
                    foreach (TradeRecord trade in trades.ToList())
                    {
                        bool op = true;
                        if (trade.SerEntryMode == "N" && trade.ProductType == "1") //金控押金获取
                        {
                            if (trade.TradeAmount == 9900 || trade.TradeAmount == 19900 || trade.TradeAmount == 29900)
                            {
                                op = false;
                                PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == trade.TradeSnNo) ?? new PxcModels.MachineForSnNo();
                                PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
                                if (pos != null)
                                {
                                    pos.SeoKeyword = trade.TradeAmount.ToString("f2");
                                    // pos.IsPurchase = 0;
                                    db.SaveChanges();
                                }
                                TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
                                if (edit != null)
                                {
                                    edit.Status = 2;
                                }
                                spdb.SaveChanges();
                            }
                        }
                        else if ((trade.ProductType == "4" || trade.ProductType == "8" || trade.ProductType == "9") && trade.Field2 == "5200" && trade.Field3 == "300") //乐刷海科费率0.52的不入库
                        {
                            op = false;
                            TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
                            if (edit != null)
                            {
                                edit.Status = 2;
                            }
                            spdb.SaveChanges();
                        }
                        else if (trade.SerEntryMode == "1" && trade.ProductType == "10") //联动押金获取
                        {
                            if (trade.TradeAmount == 99 || trade.TradeAmount == 199 || trade.TradeAmount == 299)
                            {
                                op = false;
                                PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == trade.TradeSnNo) ?? new PxcModels.MachineForSnNo();
                                PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
                                if (pos != null)
                                {
                                    pos.SeoKeyword = trade.TradeAmount.ToString("f2");
                                    db.SaveChanges();
                                }
                                TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
                                if (edit != null)
                                {
                                    edit.Status = 2;
                                }
                                spdb.SaveChanges();
                            }
                        }
                        else if (trade.SerEntryMode == "40" && trade.ProductType == "12") //盒易付押金获取
                        {
                            if (trade.TradeAmount == 99 || trade.TradeAmount == 199 || trade.TradeAmount == 299)
                            {
                                op = false;
                                PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == trade.TradeSnNo) ?? new PxcModels.MachineForSnNo();
                                PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
                                if (pos != null)
                                {
                                    pos.SeoKeyword = trade.TradeAmount.ToString("f2");
                                    PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PxcModels.PosMerchantInfo();
                                    db.MerchantDepositRecord.Add(new PxcModels.MerchantDepositRecord()
                                    {
                                        CreateDate = DateTime.Now,
                                        SnNo = pos.PosSn,
                                        MerNo = merchant.KqMerNo,
                                        DepositAmount = trade.TradeAmount,
                                        UserId = pos.BuyUserId,
                                        AgentNo = function.CheckNull(merchant.MgrNo),
                                        OrderId = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
                                    });
                                    db.SaveChanges();
                                }
                                TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
                                if (edit != null)
                                {
                                    edit.Status = 2;
                                }
                                spdb.SaveChanges();
                            }
                        }
                        if (trade.DiscountRateFlag == "True")
                        {
                            op = false;
                            TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
                            if (edit != null)
                            {
                                edit.Status = 2;
                            }
                            spdb.SaveChanges();
                        }
                        if(trade.ProductType == "7" && trade.TradeStatus != "00")
                        {
                            op = false;
                            TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
                            if (edit != null)
                            {
                                edit.Status = 2;
                            }
                            spdb.SaveChanges();
                        }
                        if (op)
                        {
                            PxcModels.MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == trade.MerNo) ?? new PxcModels.MachineForMerNo();
                            PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
                            if (pos != null)
                            {
                                PxcModels.KqProducts brand = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId) ?? new PxcModels.KqProducts();
                                if (pos.BindMerchantId > 0)
                                {
                                    PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new PxcModels.Users();
                                    int TopUserId = 0;
                                    if (!string.IsNullOrEmpty(user.ParentNav))
                                    {
                                        TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
                                    }
                                    bool check = db.SpOrderNos.Any(m => m.OrderNo == trade.TradeSerialNo);
                                    if (!check)
                                    {
                                        decimal TradeAmount = trade.TradeAmount;
                                        int BankCardType = -1;
                                        int QrPayFlag = 0;
                                        int VipFlag = 0;
                                        int PayType = 0;
                                        if (pos.BrandId == 1 || pos.BrandId == 3)
                                        {
                                            TradeAmount = TradeAmount / 100;
                                            BankCardType = int.Parse(trade.BankCardType);
                                            if (trade.TradeType == "02") QrPayFlag = 1;
                                            else if (trade.TradeType == "NQ") PayType = 1;
                                            if (trade.SettleMethod == "E") VipFlag = 1;
                                        }
                                        else if (pos.BrandId == 2)
                                        {
                                            if (trade.TradeType == "CREDIT_BY_CARD")
                                            {
                                                BankCardType = 1;
                                            }
                                            else
                                            {
                                                BankCardType = 0;
                                            }
                                            if (trade.TradeType == "CLOUD_PAY") QrPayFlag = 1;
                                        }
                                        else if (pos.BrandId == 4 || pos.BrandId == 5)
                                        {
                                            TradeAmount = TradeAmount / 100;
                                            if (trade.BankCardType == "100")
                                            {
                                                BankCardType = 1;
                                            }
                                            else if (trade.BankCardType == "200")
                                            {
                                                BankCardType = 0;
                                            }
                                            if (trade.TradeType == "200") QrPayFlag = 1;
                                            if (trade.Field1 == "1") VipFlag = 1;
                                        }
                                        else if (pos.BrandId == 6)
                                        {
                                            TradeAmount = TradeAmount / 100;
                                            if (trade.BankCardType == "02" || trade.BankCardType == "12")
                                            {
                                                BankCardType = 1;
                                            }
                                            else
                                            {
                                                BankCardType = 0;
                                            }
                                        }
                                        else if (pos.BrandId == 7)
                                        {
                                            TradeAmount = TradeAmount / 100;
                                            if (trade.BankCardType == "OA" || trade.BankCardType == "CC" || trade.BankCardType == "SCC")
                                            {
                                                BankCardType = 1;
                                            }
                                            else
                                            {
                                                BankCardType = 0;
                                            }
                                        }
                                        else if (pos.BrandId == 8 || pos.BrandId == 9)
                                        {
                                            TradeAmount = TradeAmount / 100;
                                            if (trade.BankCardType == "100")
                                            {
                                                BankCardType = 1;
                                            }
                                            else if (trade.BankCardType == "200")
                                            {
                                                BankCardType = 0;
                                            }
                                            if (trade.TradeType == "200") QrPayFlag = 1;
                                            if (trade.Field1 == "1") VipFlag = 1;
                                        }
                                        else if (pos.BrandId == 10 || pos.BrandId == 11)
                                        {
                                            if (trade.BankCardType == "01")
                                            {
                                                BankCardType = 1;
                                            }
                                            else if (trade.BankCardType == "00")
                                            {
                                                BankCardType = 0;
                                            }
                                            if (trade.TradeType == "F010") QrPayFlag = 1;
                                            PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
                                            if(merchant != null)
                                            {
                                                bool flag = false;
                                                if(function.CheckNull(merchant.MerchantName).Contains("*")) merchant.MerchantName = trade.MerName;
                                                if(string.IsNullOrEmpty(merchant.MerIdcardNo) && !string.IsNullOrEmpty(trade.Field2))
                                                {
                                                    merchant.MerIdcardNo = trade.Field2.ToUpper(); //商户身份证号
                                                    flag = true;
                                                }
                                                if(!string.IsNullOrEmpty(trade.Field1))
                                                {
                                                    merchant.MerchantMobile = trade.Field1; //商户手机号
                                                }
                                                if(flag)
                                                {
                                                    string startNo = merchant.MerIdcardNo.Substring(0, 6);
                                                    string endNo = merchant.MerIdcardNo.Substring(merchant.MerIdcardNo.Length - 4, 4).ToUpper();
                                                    string Name = merchant.MerchantName;
                                                    Name = Name.Replace("*", "");
                                                    function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥");
                                                    PxcModels.PosMerchantInfo chk = db.PosMerchantInfo.FirstOrDefault(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name));
                                                    if(chk != null)
                                                    {
                                                        function.WriteLog("互斥机具---sn:" + chk.KqSnNo + ",merno:" + chk.KqMerNo + ",name:" + chk.MerchantName, "监控机具是否互斥");
                                                        pos.IsFirst = 0;
                                                    }
                                                }
                                                db.SaveChanges();
                                            }
                                        }
                                        else if (pos.BrandId == 12 || pos.BrandId == 13) //盒易付
                                        {
                                            if (trade.BankCardType == "C")
                                            {
                                                BankCardType = 1;
                                            }
                                            else if (trade.BankCardType == "D")
                                            {
                                                BankCardType = 0;
                                            }
                                            if (trade.Field3 == "1") QrPayFlag = 1;
                                        }
                                        int BrandId = pos.BrandId;
                                        // int BrandId = int.Parse(trade.ProductType);
                                        // if (BrandId == 1 && trade.MerNo.StartsWith("M900"))
                                        // {
                                        //     BrandId = 3;
                                        // }
                                        // if (BrandId == 4 && trade.DeviceType == "200")
                                        // {
                                        //     BrandId = 5;
                                        // }
                                        decimal SourceTradeAmount = TradeAmount;
                                        if (pos.Sort == 0 && pos.BrandId == 7)
                                        {
                                            decimal deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
                                            if (deposit > 0)
                                            {
                                                TradeAmount -= deposit;
                                                pos.Sort = 1;
                                            }
                                        }
                                        decimal CheckMoney = brand.ActTradeAmount > 0 ? brand.ActTradeAmount : 1000;
                                        int CheckDays = 30;
                                        //联动
                                        if(BankCardType == 1 && trade.TradeType == "M015" && (pos.BrandId == 10 || pos.BrandId == 11))
                                        {
                                            pos.CreditTrade += SourceTradeAmount;
                                        }
                                        //盒易付
                                        else if(BankCardType == 1 && trade.TradeType == "0" && trade.SerEntryMode != "40" && (pos.BrandId == 12 || pos.BrandId == 13))
                                        {
                                            pos.CreditTrade += SourceTradeAmount;
                                        }
                                        //金控
                                        else if((BankCardType == 1 || BankCardType == 2) && (pos.BrandId == 1 || pos.BrandId == 3))
                                        {
                                            pos.CreditTrade += SourceTradeAmount;
                                        }
                                        //开店宝
                                        else if (BankCardType == 1 && pos.BrandId == 2)
                                        {
                                            pos.CreditTrade += SourceTradeAmount;
                                        }
                                        //乐刷or海科
                                        else if (BankCardType == 1 && (trade.TradeType == "100" || trade.TradeType == "200" || trade.TradeType == "300" || trade.TradeType == "400" || trade.TradeType == "500" || trade.TradeType == "600") && (pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 8 || pos.BrandId == 9))
                                        {
                                            pos.CreditTrade += SourceTradeAmount;
                                        }
                                        //立刷
                                        else if(BankCardType == 1 && trade.Field2 == "0104" && pos.BrandId == 6)
                                        {
                                            pos.CreditTrade += SourceTradeAmount;
                                        }
                                        //盛付通
                                        else if(BankCardType == 1 && pos.BrandId == 7)
                                        {
                                            pos.CreditTrade += SourceTradeAmount;
                                        }
                                        //广电卡
                                        else if(pos.BrandId == 14)
                                        {
                                            pos.CreditTrade += SourceTradeAmount;
                                        }
                                        else if (BankCardType == 0)
                                        {
                                            pos.DebitCardTrade += TradeAmount;
                                        }
                                        //判断激活条件并激活
                                        ActPos(db, pos, CheckMoney, CheckDays);
                                        if (TradeAmount > 0)
                                        {
                                            ulong MerHelpFlag = 0;
                                            decimal DepositAmt = decimal.Parse(function.CheckNum(pos.SeoKeyword));
                                            if (pos.DownFeeFlag == 1)
                                            {
                                                if(pos.BrandId == 12 || pos.BrandId == 13)
                                                {
                                                    MerHelpFlag = 3;
                                                }
                                                else
                                                {
                                                    if (pos.DownFee == 0.6M)
                                                    {
                                                        MerHelpFlag = 3;
                                                    }
                                                    else
                                                    {
                                                        MerHelpFlag = 2;
                                                    }
                                                }
                                            }
                                            else if ((DepositAmt > 0 && pos.IsFirst == 1) || pos.BrandId == 14)
                                            {
                                                string DayString = RedisDbconn.Instance.Get<string>("pobjrule:" + pos.BrandId + ":HelpPolicy:Days");
                                                int Days = int.Parse(DayString); //天数
                                                if (pos.BindingTime != null)
                                                {
                                                    if (pos.BindingTime.Value.AddDays(Days) > DateTime.Now)
                                                    {
                                                        MerHelpFlag = 1;
                                                    }
                                                }
                                            }
                                            if((pos.BrandId == 12 || pos.BrandId == 13) && pos.UpFeeFlag == 1 && pos.DownFeeFlag == 0)
                                            {
                                                MerHelpFlag = 2;
                                            }
                                            if((pos.BrandId == 10 || pos.BrandId == 11) && trade.TradeType == "T015" && MerHelpFlag == 1)
                                            {
                                                MerHelpFlag = 0;
                                            }
                                            int CapFlag = 0;
                                            if(BankCardType == 0 && TradeAmount >= 1666.67M)
                                            {
                                                CapFlag = 1;
                                            }
                                            string HelpProfitFlag = "";
                                            bool checkHelpProfit = db.HelpProfitMerchantForUser.Any(m => m.MerchantId == pos.BindMerchantId);
                                            if(checkHelpProfit)
                                            {
                                                HelpProfitFlag = "HelpProfit";
                                            }
                                            int ActStatus = pos.ActivationState;
                                            // if(BrandId == 4 || BrandId == 5 || BrandId == 8 || BrandId == 9)
                                            // {
                                            //     ActStatus = GetLeShuaHaiKe(pos.UserId, BrandId);
                                            // }
                                            int Version = 0;
                                            if(MerHelpFlag == 2) Version = 1;
                                            if(MerHelpFlag == 3) Version = 2;
                                            decimal SettleFee = trade.SettleFee * 100;
                                            PxcModels.TradeRecord add = db.TradeRecord.Add(new PxcModels.TradeRecord()
                                            {
                                                ParentNav = user.ParentNav,
                                                CreateDate = trade.CreateDate,
                                                UpdateDate = trade.UpdateDate,
                                                RecordNo = trade.TradeSerialNo, //单号
                                                UserId = pos.UserId, //创客
                                                MerchantId = pos.BindMerchantId, //商户
                                                MerNo = trade.MerNo, //渠道商户编号
                                                MerHelpFlag = MerHelpFlag >= 2 ? 1 : MerHelpFlag, //1扶持周,0稳定期,2盈利期
                                                HelpMonthCount = 0, //扶持第几个月
                                                MerBuddyType = user.MerchantType, //商户创客类型
                                                SnNo = trade.TradeSnNo, //渠道SN号
                                                TradeSerialNo = trade.ChannelSerial, //交易流水号
                                                TradeAmount = TradeAmount, //交易金额
                                                BankCardType = BankCardType, //银行卡类型
                                                QrPayFlag = QrPayFlag, //云闪付标识
                                                CapFlag = CapFlag, //借记卡是否封顶
                                                VipFlag = VipFlag, //会员标记
                                                PayType = PayType, //支付方式
                                                BrandId = BrandId, //品牌
                                                Remark = trade.Remark, //备注
                                                TopUserId = TopUserId, //顶级创客
                                                MerUserId = pos.UserId, //商户直属创客
                                                SeoTitle = HelpProfitFlag,
                                                Version = Version,
                                                ActStatus = ActStatus,
                                                CreateMan = trade.AgentNo,
                                                UpdateMan = trade.BankCardType,
                                                SeoKeyword = trade.SerEntryMode,
                                                SeoDescription = trade.TradeType,
                                                Sort = (int)SettleFee,
                                            }).Entity;
                                            db.SaveChanges();
                                            string TradeMonth = trade.CreateDate == null ? DateTime.Now.ToString("yyyyMM") : trade.CreateDate.Value.ToString("yyyyMM");
                                            PublicFunction.SplitTradeRecord(add, TradeMonth);

                                            //统计商户交易额
                                            string TradeDate = trade.CreateDate == null ? DateTime.Now.ToString("yyyyMMdd") : trade.CreateDate.Value.ToString("yyyyMMdd");
                                            RedisDbconn.Instance.AddList("StatMerTradeAmountQueue", "{\"TradeDate\":\"" + TradeDate + "\",\"TradeAmount\":\"" + TradeAmount + "\",\"MerchantId\":\"" + pos.BindMerchantId + "\",\"BrandId\":\"" + BrandId + "\"}");
                                        }
                                        // 立刷60天内刷满10000，再返40
                                        // if (pos.BrandId == 6 && decimal.Parse(function.CheckNum(pos.SeoKeyword)) > 0&& pos.CreditTrade >= 10000 && pos.ActivationState == 1  && pos.BindingTime > DateTime.Now.AddDays(-60))
                                        // {
                                        //     LiShuaActPrize(db, pos, 40);
                                        // }
                                        //9月1日-9月30日立刷机具刷5000激活时额外奖励30元立刷活动奖
                                        // if(pos.BrandId == 6 && pos.CreditTrade >= 5000M && pos.ActivationTime >= DateTime.Parse("2023-08-31 00:00:00") && pos.ActivationTime < DateTime.Parse("2023-11-01 00:00:00"))
                                        // {
                                        //     LiShuaActPrize(db, pos, 30);
                                        // }
                                        db.SpOrderNos.Add(new PxcModels.SpOrderNos()
                                        {
                                            OrderNo = trade.TradeSerialNo
                                        });
                                        db.SaveChanges();

                                        // 推荐王逻辑(交易)
                                        // if(TradeAmount > 0)
                                        // {
                                        //     string Send = "{\"Kind\":\"4\",\"Data\":{\"PosId\":\"" + pos.Id + "\",\"TradeMonth\":\"" + trade.CreateDate.Value.ToString("yyyyMM") + "\",\"TradeAmount\":\"" + TradeAmount + "\"}}";
                                        //     RedisDbconn.Instance.AddList("RecommendActStatQueue", Send);
                                        // }
                                    }
                                    TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
                                    if (edit != null)
                                    {
                                        edit.Status = 2;
                                    }
                                    spdb.SaveChanges();
                                }
                                else
                                {
                                    TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
                                    if (edit != null)
                                    {
                                        edit.Status = 0;
                                    }
                                    spdb.SaveChanges();
                                }
                            }
                            else
                            {
                                TradeRecord edit = spdb.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
                                if (edit != null)
                                {
                                    edit.Status = 0;
                                }
                                spdb.SaveChanges();
                            }
                        }
                    }
                    spdb.Dispose();
                    db.Dispose();
                }
                catch (Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP交易数据到MAIN异常");
                }
                Thread.Sleep(500);
            }
        }

        //机具激活逻辑
        public void ActPos(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, decimal CheckMoney, int CheckDays, bool PrizeFlag = true, string ActivationTime = "")
        {
            DateTime now = DateTime.Now;
            if(!string.IsNullOrEmpty(ActivationTime))
            {
                now = DateTime.Parse(ActivationTime);
            }
            DateTime TransferTime = pos.TransferTime == null ? DateTime.Now : pos.TransferTime.Value;
            int minute = 30;
            if(pos.BrandId == 14)
            {
                minute = 0;
            }
            if (pos.CreditTrade >= CheckMoney && pos.BuyUserId > 0 && pos.ActivationState == 0 && TransferTime.AddMinutes(-minute) < pos.BindingTime)
            {
                pos.ActivationState = 1;
                pos.ActivationTime = now;
                function.WriteLog("机具" + pos.PosSn, "开机奖励在激活中监控");
                function.WriteLog("PrizeFlag:" + PrizeFlag, "开机奖励在激活中监控");
                PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
                if (merchant != null)
                {
                    function.WriteLog("查询到商户", "开机奖励在激活中监控");
                    merchant.ActiveStatus = 1;
                    merchant.MerStandardDate = now;
                    db.SaveChanges();

                    function.WriteLog("商户" + merchant.KqMerNo, "开机奖励在激活中监控");

                    //发放开机奖励
                    function.WriteLog("首台" + pos.IsFirst, "开机奖励在激活中监控");
                    if(pos.IsFirst == 1 && pos.BindingTime > now.AddDays(-CheckDays) && PrizeFlag)
                    {
                        function.WriteLog("开机奖发放", "开机奖励在激活中监控");
                        RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
                    }
                }
                else if(pos.BrandId == 14 && pos.IsFirst == 1 && pos.BindingTime > now.AddDays(-CheckDays) && PrizeFlag)
                {
                    function.WriteLog("开机奖发放", "开机奖励在激活中监控");
                    RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
                }
                //发放大盟主奖励
                function.WriteLog("大盟主---PrizeFlag:" + PrizeFlag + ";LeaderUserId:" + pos.LeaderUserId + ";pos.IsFirst:" + pos.IsFirst + ";pos.BindingTime:" + pos.BindingTime + ";now.AddDays(-CheckDays):" + now.AddDays(-CheckDays) + ";", "开机奖励在激活中监控");
                if (pos.LeaderUserId > 0 && pos.IsFirst == 1 && db.Leaders.Any(m => m.Id == pos.LeaderUserId && m.ExpiredDate > now) && pos.BindingTime > now.AddDays(-CheckDays) && PrizeFlag)
                {
                    function.WriteLog("大盟主奖发放", "开机奖励在激活中监控");
                    RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
                }
                //发放运营中心奖励
                if (pos.IsFirst == 1 && pos.BindingTime > now.AddDays(-CheckDays) && PrizeFlag)
                {
                    function.WriteLog("运营中心奖发放", "开机奖励在激活中监控");
                    RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
                }
                AlipayPayBack2Service.Instance.ActReserveBack(pos.OpId, pos.OpReserve1, pos.OpReserve2, pos.OpReserve3);

                //统计激活数
                RedisDbconn.Instance.AddList("StatActQueue", "{\"TradeDate\":\"" + now.ToString("yyyyMMdd") + "\",\"UserId\":\"" + pos.BuyUserId + "\",\"BrandId\":\"" + pos.BrandId + "\"}");
            }
        }

        //立刷格外奖励
        public void LiShuaActPrize(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, int prize)
        {
            PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
            if(merchant != null)
            {
                PxcModels.Users getUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new PxcModels.Users();
                int BuyTopUserId = 0;
                string ParentNav = getUser.ParentNav;
                if (!string.IsNullOrEmpty(ParentNav))
                {
                    string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
                    if (ParentNavList.Length > 1)
                    {
                        BuyTopUserId = int.Parse(ParentNavList[1]);
                    }
                    else if (ParentNavList.Length == 1)
                    {
                        BuyTopUserId = int.Parse(ParentNavList[0]);
                    }
                }
                if(prize == 50)
                {
                    StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize);
                }
                if(prize == 40)
                {
                    StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize, 12);
                }
                if(prize == 30)
                {
                    StatService.Instance.doActiveReward(db, merchant, pos, getUser.Id, getUser.ParentNav, BuyTopUserId, prize, 133);
                }
            }
        }

        private int GetLeShuaHaiKe(int UserId, int BrandId)
        {
            if(DateTime.Now >= DateTime.Parse("2023-08-01 00:00:00"))
            {
                return 0;
            }
            int ActStatus = 1;
            try
            {
                string Date = DateTime.Now.ToString("yyyyMMdd");
                int checkNum = 0;
                if(Date == "20230720") checkNum = 12;
                if(Date == "20230721") checkNum = 11;
                if(Date == "20230722") checkNum = 10;
                if(Date == "20230723") checkNum = 9;
                if(Date == "20230724") checkNum = 8;
                if(Date == "20230725") checkNum = 7;
                if(Date == "20230726") checkNum = 6;
                if(Date == "20230727") checkNum = 5;
                if(Date == "20230728") checkNum = 4;
                if(Date == "20230729") checkNum = 3;
                if(Date == "20230730") checkNum = 2;
                if(Date == "20230731") checkNum = 1;
                int check = int.Parse(function.CheckInt(RedisDbconn.Instance.Get<string>("lscut:" + UserId + ":" + BrandId + ":" + Date)));
                if(check <= checkNum)
                {
                    check += 1;
                    RedisDbconn.Instance.Set("lscut:" + UserId + ":" + BrandId + ":" + Date, check.ToString());
                }
                else
                {
                    ActStatus = -1;
                    RedisDbconn.Instance.Set("lscut:" + UserId + ":" + BrandId + ":" + Date, "0");
                }
            }
            catch(Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "乐刷海科计数异常");
            }
            return ActStatus;
        }
    }
}