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

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

        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 < 19)
                {
                    try
                    {
                        string check = function.ReadInstance("/Fee/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
                        if (string.IsNullOrEmpty(check))
                        {
                            function.WritePage("/Fee/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
                            WebCMSEntities db = new WebCMSEntities();
                            List<int> BrandIds = db.KqProducts.Where(m => m.Status > 0).ToList().Select(m => m.Id).ToList();
                            var date = DateTime.Now.AddDays(-85);
                            bool op = true;
                            int StartId = 0;
                            while(op)
                            {
                                var posList = db.PosMachinesTwo.Select(m => new { m.Id, m.Status, m.BindingState, m.BindingTime, m.UpFeeFlag, m.BrandId }).Where(m => m.Id > StartId && m.Status > -1 && m.BindingTime <= date && m.BindingState == 1 && m.UpFeeFlag == 0 && BrandIds.Contains(m.BrandId)).OrderBy(m => m.Id).Take(100).ToList();
                                function.WriteLog(DateTime.Now.ToString(), "85天提前通知创客费率调升");
                                function.WriteLog(posList.Count.ToString(), "85天提前通知创客费率调升");
                                if(posList.Count > 0)
                                {
                                    //循环修改机具费率调升标记
                                    //统计需要推送消息的用户Id
                                    foreach (var item in posList)
                                    {
                                        function.WriteLog(item.Id.ToString(), "85天提前通知创客费率调升");
                                        string info = "{\"RecordId\":\"\",\"PosId\":\"" + item.Id + "\",\"Fee\": \"" + 0.63 + "\",\"Kind\": \"0\",\"OpMan\": \""+"系统"+"\"}";
                                        RedisDbconn.Instance.AddList("SetDepositQueue", info);
                                        StartId = item.Id;
                                    }
                                }
                                else
                                {
                                    op = false;
                                }
                            }
                            db.Dispose();
                        }
                    }
                    catch (Exception ex)
                    {
                        function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "机具费率标记设置异常");
                    }
                }
                Thread.Sleep(800000);
            }
        }
    }
}