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

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

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

        public void StartDo()
        {
            int StartId = 26867679;
            bool op = true;
            while(op)
            {
                WebCMSEntities db = new WebCMSEntities();
                List<TradeRecord> trades = db.TradeRecord.Where(m => m.Id > StartId && m.Id <= 27650622 && m.MerHelpFlag == 0 && m.Version == 0).OrderBy(m => m.Id).Take(20).ToList();
                if(trades.Count > 0)
                {
                    foreach (TradeRecord trade in trades)
                    {
                        PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == trade.SnNo) ?? new PosMachinesTwo();
                        ulong MerHelpFlag = 0;
                        decimal DepositAmt = decimal.Parse(function.CheckNum(pos.SeoKeyword));
                        if (pos.DownFeeFlag == 1)
                        {
                            if (pos.DownFee == 0.6M)
                            {
                                MerHelpFlag = 3;
                            }
                            else
                            {
                                MerHelpFlag = 2;
                            }
                        }
                        else if (pos.IsFirst == 0)
                        {
                            if (pos.DownFee == 0.6M)
                            {
                                MerHelpFlag = 3;
                            }
                            else
                            {
                                MerHelpFlag = 2;
                            }
                        }
                        else if (pos.IsFirst == 1 || pos.BrandId == 14)
                        {
                            string DayString = RedisDbconn.Instance.Get<string>("pobjrule:1:HelpPolicy:Days");
                            int Days = int.Parse(DayString); //天数
                            if (pos.BindingTime != null)
                            {
                                DateTime CheckTime = pos.BindingTime.Value.AddDays(Days);
                                if(pos.BrandId == 7)
                                {
                                    CheckTime = DateTime.Parse(pos.BindingTime.Value.AddDays(Days + 1).ToString("yyyy-MM-dd") + " 03:00:00");
                                }
                                if (CheckTime > trade.CreateDate)
                                {
                                    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.SeoDescription == "T015" && MerHelpFlag == 1)
                        {
                            MerHelpFlag = 0;
                        }
                        int Version = 0;
                        if(MerHelpFlag == 2) Version = 1;
                        if(MerHelpFlag == 3) Version = 2;
                        TradeRecord edit = db.TradeRecord.FirstOrDefault(m => m.Id == trade.Id);
                        if(edit != null)
                        {
                            MerHelpFlag = MerHelpFlag >= 2 ? 1 : MerHelpFlag;
                            if(edit.MerHelpFlag != MerHelpFlag)
                            {
                                edit.MerHelpFlag = MerHelpFlag;
                                Utils.WriteLog(trade.SnNo + ";MerHelpFlag:" + MerHelpFlag, "重置活动期标记");
                            }
                            if(edit.Version != Version)
                            {
                                edit.Version = Version;
                                Utils.WriteLog(trade.SnNo + ";Version:" + Version, "重置活动期标记");
                            }
                            db.SaveChanges();
                        }
                        StartId = trade.Id;
                    }
                }
                else
                {
                    op = false;
                }
                db.Dispose();
            }
            Utils.WriteLog("end!!!!!", "重置活动期标记");
        }
    }
}