using System;
using System.Collections.Generic;
using System.Linq;
using MySystem.SpModels;
using Library;
using LitJson;
using System.Threading;

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

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

        public void StartDo()
        {
            while (true)
            {
                try
                {
                    WebCMSEntities spdb = new WebCMSEntities();
                    PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
                    string Month = DateTime.Now.ToString("yyyyMM");
                    string Date = DateTime.Now.ToString("yyyyMMdd");
                    DateTime start = DateTime.Now.AddDays(-5);
                    int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/ActivateRecordId.txt")));
                    IQueryable<ActivateRecord> acts = spdb.ActivateRecord.Where(m => m.Id >= StartId && m.ActivateDate >= start && m.Status == 1).OrderByDescending(m => m.Id);
                    foreach (ActivateRecord act in acts.ToList())
                    {
                        PxcModels.MachineForMerNo posFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == act.MerNo) ?? new PxcModels.MachineForMerNo();
                        PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
                        if (pos != null)
                        {
                            PxcModels.KqProducts kqproduct = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId) ?? new PxcModels.KqProducts();
                            if (kqproduct.SingleDepositApi == 1)
                            {
                                pos.SeoKeyword = act.SeoTitle;
                                if(pos.BrandId == 15 || pos.BrandId == 16) //来客吧
                                {
                                    pos.Detail = ""; //清除激活奖励标记
                                    RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString()); //发开机奖励
                                }
                                db.SaveChanges();
                            }

                            ActivateRecord edit = spdb.ActivateRecord.FirstOrDefault(m => m.Id == act.Id);
                            if (edit != null)
                            {
                                edit.Status = 2;
                            }
                        }
                        pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId && m.ActivationState == 1);
                        if (pos != null)
                        {
                            ActivateRecord edit = spdb.ActivateRecord.FirstOrDefault(m => m.Id == act.Id);
                            if (edit != null)
                            {
                                edit.Status = 2;
                            }
                        }
                    }
                    spdb.SaveChanges();
                    spdb.Dispose();
                    db.SaveChanges();
                    db.Dispose();
                }
                catch (Exception ex)
                {
                    function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP激活数据到MAIN异常");
                }
                Thread.Sleep(1000);
            }
        }
    }
}