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

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

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

        public void doSomething()
        {
            while (true)
            {
                if (DateTime.Now.Hour > 0 && DateTime.Now.Hour < 22)
                {
                    try
                    {
                        string check = function.ReadInstance("/SftFee/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
                        if (string.IsNullOrEmpty(check))
                        {
                            function.WritePage("/SftFee/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
                            WebCMSEntities db = new WebCMSEntities();
                            var date = DateTime.Now.AddDays(-420);
                            var checkDate = DateTime.Parse("2024-06-01 00:00:00").AddDays(-426);
                            bool op = true;
                            int StartId = 0;
                            while(op)
                            {
                                var posList = db.PosMachinesTwo.Select(m => new { m.Id, m.PosSn, m.Status, m.BindingState, m.BindingTime, m.UpFeeFlag, m.DownFeeFlag, m.DownFee, m.BrandId, m.IsSupplement }).Where(m => m.Id > StartId && m.Status > -1 && m.BindingTime <= date && m.BindingState == 1 && m.BrandId == 7 && m.IsSupplement == 0).OrderBy(m => m.Id).Take(100).ToList();
                                function.WriteLog(DateTime.Now.ToString(), "420天盛付通费率加万2");
                                function.WriteLog(posList.Count.ToString(), "420天盛付通费率加万2");
                                if(posList.Count > 0)
                                {
                                    //循环修改机具费率调升标记
                                    //统计需要推送消息的用户Id
                                    foreach (var item in posList)
                                    {
                                        if(item.BindingTime >= checkDate)
                                        {
                                            string Fee = "";
                                            string AddRate = "";
                                            if(item.UpFeeFlag == 1 && item.DownFeeFlag == 0)
                                            {
                                                Fee = "0.65";
                                                AddRate = "3";
                                            }
                                            else if(item.UpFeeFlag == 1 && item.DownFeeFlag == 1 && item.DownFee == 0.63M)
                                            {
                                                Fee = "0.65";
                                                AddRate = "0";
                                            }
                                            else if(item.UpFeeFlag == 1 && item.DownFeeFlag == 1 && item.DownFee == 0.6M)
                                            {
                                                Fee = "0.62";
                                                AddRate = "0";
                                            }
                                            string info = "{\"RecordId\":\"\",\"PosId\":\"" + item.Id + "\",\"Fee\": \"" + Fee + "\",\"AddRate\": \"" + AddRate + "\",\"Kind\": \"4\",\"OpMan\": \"系统\"}";
                                            function.WriteLog(item.PosSn + "-Fee:" + Fee + "-AddRate:" + AddRate, "420天盛付通费率加万2");
                                            RedisDbconn.Instance.AddList("SetDepositPostQueue", info);
                                            PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == item.Id);
                                            if(pos != null)
                                            {
                                                pos.IsSupplement = 1;
                                                db.SaveChanges();
                                            }
                                        }
                                        StartId = item.Id;
                                    }
                                }
                                else
                                {
                                    op = false;
                                }
                            }
                            db.Dispose();
                        }
                    }
                    catch (Exception ex)
                    {
                        function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "425天盛付通费率加万2异常");
                    }
                }
                Thread.Sleep(800000);
            }
        }
    }
}