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 WifiWarningService
    {
        public readonly static WifiWarningService Instance = new WifiWarningService();
        private WifiWarningService()
        { }

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

        public void dosomething()
        {
            while (true)
            {
                try
                {
                    if(DateTime.Now.Hour > 2 && DateTime.Now.Hour < 4)
                    {
                        string chk = function.ReadInstance("/WifiWithhold/" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
                        if(string.IsNullOrEmpty(chk))
                        {
                            function.WritePage("/WifiWithhold/", "" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString());
                            WifiWithhold();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "WIFI提醒异常");
                }
                Thread.Sleep(30000);
            }
        }

        //WIFI30天未完成申请时向预发创客增加一条预扣款
        private void WifiWithhold()
        {
            WebCMSEntities db = new WebCMSEntities();
            int Id = 0;
            bool op = true;
            while(op)
            {
                List<int> BrandIds = new List<int>();
                BrandIds.Add(23);
                BrandIds.Add(24);
                BrandIds.Add(25);
                BrandIds.Add(26);
                BrandIds.Add(32);
                DateTime time = DateTime.Now.AddDays(-30);
                List<PosMachinesTwo> WifiList = db.PosMachinesTwo.Where(m => m.Id > Id && BrandIds.Contains(m.BrandId) && m.ActivationState == 0 && m.TransferTime < time && m.Sort == 0).OrderBy(m => m.Id).Take(20).ToList();
                if(WifiList.Count > 0)
                {
                    foreach(var Pos in WifiList)
                    {
                        RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
                        {
                            UserId = Pos.BuyUserId, //创客
                            Title = "WIFI设备超时提醒", //标题
                            Content = "<div class='f16'>尊敬的客小爽创客：<br />您好，您名下的部分WIFI设备已接收30天，超过35天未完成激活时将预扣您的创客余额。<br>为保障您的收益不受影响，请您尽快完成WIFI设备激活！</ div > ",//内容
                            CreateDate = DateTime.Now,
                        }));
                        PosMachinesTwo edit = db.PosMachinesTwo.FirstOrDefault(m => m.Id == Pos.Id);
                        if(edit != null)
                        {
                            edit.Sort = 1;
                        }
                        Id = Pos.Id;
                    }
                    db.SaveChanges();
                }
                else
                {
                    op = false;
                }
            }
            db.Dispose();
        }
    }
}