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

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

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

        public void dosomething()
        {
            while (true)
            {
                try
                {
                    if(DateTime.Now.Hour > 3 && DateTime.Now.Hour < 19)
                    {
                        string chk = function.ReadInstance("/PrePosRing/" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
                        if(string.IsNullOrEmpty(chk))
                        {
                            function.WritePage("/PrePosRing/", "" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString());
                            PrePosRing();
                            PrePosRingForExpired();
                        }
                    }
                }
                catch (Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "预发机提醒异常");
                }
                Thread.Sleep(300000);
            }
        }

        //已确认状态的预发机25天至29天未完成申请时每天一次弹窗提醒预发创客
        private void PrePosRing()
        {
            WebCMSEntities db = new WebCMSEntities();
            DateTime start = DateTime.Parse(DateTime.Now.AddDays(-29).ToString("yyyy-MM-dd") + " 00:00:00");
            DateTime end = DateTime.Parse(DateTime.Now.AddDays(-24).ToString("yyyy-MM-dd") + " 00:00:00");
            var PrePosList = db.PreSendStockDetail.Where(m => m.ApplyFlag == 0 && m.CreateDate >= start && m.CreateDate < end && m.Status >= 0 && m.Status <= 1).ToList();
            foreach(var Pos in PrePosList)
            {
                Dictionary<string, string> Item = new Dictionary<string, string>();
                Item.Add("SnNo", Pos.SnNo);
                Item.Add("PreDate", Pos.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                string value = Newtonsoft.Json.JsonConvert.SerializeObject(Item);

                RedisDbconn.Instance.AddList("PrePosRing:" + DateTime.Now.ToString("yyyyMMdd") + ":" + Pos.ToUserId, value);
                int ExpireTime = 60 * 60 * 25;
                RedisDbconn.Instance.SetExpire("PrePosRing:" + DateTime.Now.ToString("yyyyMMdd") + ":" + Pos.ToUserId, ExpireTime);
            }
            db.Dispose();
        }

        //20日前对预发机超30天未完成申请的创客每天新增一次弹窗
        private void PrePosRingForExpired()
        {
            if(DateTime.Now.Day >= 15 && DateTime.Now.Day < 20)
            {
                WebCMSEntities db = new WebCMSEntities();
                DateTime end = DateTime.Parse(DateTime.Now.AddDays(-29).ToString("yyyy-MM-dd") + " 00:00:00");
                var PrePosList = db.PreSendStockDetail.Where(m => m.ApplyFlag == 0 && m.CreateDate < end && m.Status >= 0 && m.Status <= 1).ToList();
                foreach(var Pos in PrePosList)
                {
                    Dictionary<string, string> Item = new Dictionary<string, string>();
                    Item.Add("SnNo", Pos.SnNo);
                    Item.Add("PreDate", Pos.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                    string value = Newtonsoft.Json.JsonConvert.SerializeObject(Item);
                    
                    RedisDbconn.Instance.AddList("PrePosRingForExpired:" + DateTime.Now.ToString("yyyyMMdd") + ":" + Pos.ToUserId, value);
                    int ExpireTime = 60 * 60 * 25;
                    RedisDbconn.Instance.SetExpire("PrePosRingForExpired:" + DateTime.Now.ToString("yyyyMMdd") + ":" + Pos.ToUserId, ExpireTime);
                }
                db.Dispose();
            }
        }
    }
}