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

        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 < 5)
                    {
                        string chk = function.ReadInstance("/WifiWithholdDo/" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
                        if(string.IsNullOrEmpty(chk))
                        {
                            function.WritePage("/WifiWithholdDo/", "" + 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();
            DateTime end = DateTime.Parse(DateTime.Now.AddDays(-29).ToString("yyyy-MM-dd") + " 00:00:00");
            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(-35);
                List<PosMachinesTwo> WifiList = db.PosMachinesTwo.Where(m => m.Id > Id && BrandIds.Contains(m.BrandId) && m.ActivationState == 0 && m.TransferTime < time && m.Sort == 1).OrderBy(m => m.Id).Take(20).ToList();
                if(WifiList.Count > 0)
                {
                    foreach(var Pos in WifiList)
                    {
                        KqProducts brand = db.KqProducts.FirstOrDefault(m => m.Id == Pos.BrandId) ?? new KqProducts();
                        decimal ChargeAmount = 0;
                        if(Pos.BrandId == 23)
                        {
                            ChargeAmount = 30;
                        }
                        if(Pos.BrandId == 24)
                        {
                            ChargeAmount = 120;
                        }
                        if(Pos.BrandId == 25)
                        {
                            ChargeAmount = 200;
                        }
                        if(Pos.BrandId == 26)
                        {
                            ChargeAmount = 750;
                        }
                        bool check = db.ToChargeBackRecord.Any(m => m.SeoDescription.Contains(Pos.PosSn) && (m.Status == 3 || m.Status == 0));
                        if(!check)
                        {
                            db.ToChargeBackRecord.Add(new ToChargeBackRecord
                            {
                                CreateDate = DateTime.Now,
                                SeoTitle = "系统",
                                SeoDescription = "WIFI超时未激活扣款",
                                Remark = "WIFI超时未激活扣款",
                                ChargeType = 1,
                                ChargeAmount = ChargeAmount,
                                UserId = Pos.BuyUserId,
                                Field1 = Pos.PosSn,
                            });
                            //增加账户预扣总额
                            Utils.Instance.ToChargeAmount(Pos.BuyUserId, ChargeAmount);
                        }
                        PosMachinesTwo edit = db.PosMachinesTwo.FirstOrDefault(m => m.Id == Pos.Id);
                        if(edit != null)
                        {
                            edit.Sort = 2;
                        }
                        Id = Pos.Id;
                    }
                    db.SaveChanges();
                }
                else
                {
                    op = false;
                }
            }
            db.Dispose();
        }
    }
}