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 < 8)
                {
                    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 = new List<int>();
                            BrandIds.Add(1);
                            BrandIds.Add(2);
                            BrandIds.Add(3);
                            BrandIds.Add(4);
                            BrandIds.Add(5);
                            BrandIds.Add(7);
                            BrandIds.Add(8);
                            BrandIds.Add(9);
                            BrandIds.Add(10);
                            BrandIds.Add(11);
                            var date = DateTime.Now.AddDays(-118);
                            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();
                                LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "118天提前通知创客费率调升");
                                LogHelper.Instance.WriteLog(posList.Count.ToString(), "118天提前通知创客费率调升");
                                if(posList.Count > 0)
                                {
                                    //循环修改机具费率调升标记
                                    //统计需要推送消息的用户Id
                                    foreach (var item in posList)
                                    {
                                        LogHelper.Instance.WriteLog(item.Id.ToString(), "118天提前通知创客费率调升");
                                        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)
                    {
                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "机具费率标记设置异常");
                    }
                }
                Thread.Sleep(800000);
            }
        }
    }
}