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

namespace MySystem
{
    /// <summary>
    /// 设置机具费率标记并推送消息
    /// </summary>
    public class LiShuaFeeSetService
    {
        public readonly static LiShuaFeeSetService Instance = new LiShuaFeeSetService();
        private LiShuaFeeSetService()
        { }

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

        public void doSomething()
        {
            while (true)
            {
                try
                {
                    DateTime check = DateTime.Now.AddHours(1);
                    WebCMSEntities db = new WebCMSEntities();
                    var list = db.LiShuaFeeSetRecord.Where(m => m.CreateDate < check && string.IsNullOrEmpty(m.TradeFeeAmt) && string.IsNullOrEmpty(m.TradeFeeRate)).OrderBy(m => m.Id).ToList();
                    foreach(var sub in list)
                    {
                        string back = PublicImportDataService.Instance.QueryLiSDeposit(sub.MerNo, "");
                        JsonData jsonObj = JsonMapper.ToObject(back);
                        if(jsonObj["ret_code"].ToString() == "00")
                        {                            
                            string TradeFeeAmt = "";
                            string TradeFeeRate = "";
                            JsonData feeList = jsonObj["feeList"];
                            for (int i = 0; i < feeList.Count; i++)
                            {
                                string feeCalcType = feeList["feeCalcType"].ToString();
                                if(feeCalcType == "M5") TradeFeeRate = feeList["rate"].ToString();
                                if(feeCalcType == "T0") TradeFeeAmt = feeList["rate"].ToString();
                            }
                            if(!string.IsNullOrEmpty(TradeFeeAmt) && !string.IsNullOrEmpty(TradeFeeRate))
                            {
                                LiShuaFeeSetRecord edit = db.LiShuaFeeSetRecord.FirstOrDefault(m => m.Id == sub.Id);
                                if(edit != null)
                                {
                                    edit.TradeFeeAmt = TradeFeeAmt;
                                    edit.TradeFeeRate = TradeFeeRate;
                                    db.SaveChanges();
                                }
                            }
                        }
                    }
                    db.Dispose();
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "立刷费率查询异常");
                }
                Thread.Sleep(600000);
            }
        }


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

        public void doSomethingDo()
        {
            while (true)
            {
                try
                {
                    DateTime check = DateTime.Now.AddHours(1);
                    WebCMSEntities db = new WebCMSEntities();
                    var list = db.LiShuaFeeSetRecord.Where(m => m.CreateDate < check && !string.IsNullOrEmpty(m.TradeFeeAmt) && !string.IsNullOrEmpty(m.TradeFeeRate)).OrderBy(m => m.Id).ToList();
                    foreach(var sub in list)
                    {
                        AddData(db, sub.PosSn, sub.TradeFeeRate, sub.TradeFeeAmt);
                        LiShuaFeeSetRecord item = db.LiShuaFeeSetRecord.FirstOrDefault(m => m.Id == sub.Id);
                        if(item != null)
                        {
                            db.LiShuaFeeSetRecord.Remove(item);
                            db.SaveChanges();
                        }
                    }
                    db.Dispose();
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "立刷费率提交报警异常");
                }
                Thread.Sleep(600000);
            }
        }

        public void AddData(WebCMSEntities db, string posSn, string feeRate, string feeAmt)
        {
            if(feeRate != "0.63" && feeRate != "0.6")
            {
                return;
            }
            PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == posSn);
            if(pos != null)
            {
                bool op = false;
                if(feeRate == "0.63" && feeAmt == "3" && pos.UpFeeFlag == 1 && pos.DownFeeFlag == 0) //稳定期
                {
                    op = true;
                }
                else if(feeRate == "0.63" && feeAmt == "0" && pos.DownFeeFlag == 1 && pos.DownFee == 0.63M) //稳定期A
                {
                    op = true;
                }
                else if(feeRate == "0.6" && feeAmt == "0" && pos.DownFeeFlag == 0 && pos.DownFee == 0M) //扶持期
                {
                    op = true;
                }
                else if(feeRate == "0.6" && feeAmt == "0" && pos.DownFeeFlag == 1 && pos.DownFee == 0.6M) //稳定期B
                {
                    op = true;
                }
                if(!op)
                {
                    DateTime check = DateTime.Now.AddHours(-1);
                    if(!db.PosFeeWarningRecord.Any(m => m.PosSn == posSn && m.CreateDate >= check))
                    {
                        db.PosFeeWarningRecord.Add(new PosFeeWarningRecord()
                        {
                            CreateDate = DateTime.Now,
                            UpdateDate = DateTime.Now,
                            DownFee = pos.DownFee,
                            DownFeeFlag = pos.DownFeeFlag,
                            UpFeeFlag = pos.UpFeeFlag,
                            TradeFeeAmt = feeAmt,
                            TradeFeeRate = feeRate,
                            PosSn = posSn,
                            PosId = pos.Id,
                            BrandId = pos.BrandId,
                        });
                        db.SaveChanges();
                    }
                }
            }
        }
    }
}