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())
                    {
                        bool op = false;
                        if (act.ActivateStatus == "00" && act.ProductType == "1")
                        {
                            op = true;
                        }
                        else if (act.ProductType == "2" || act.ProductType == "4" || act.ProductType == "6" || act.ProductType == "7" || act.ProductType == "8" || act.ProductType == "9")
                        {
                            op = true;
                        }
                        if (op)
                        {
                            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)
                            {
                                // pos.ActivationState = 1;
                                // pos.ActivationTime = DateTime.Now;
                                if (pos.BrandId == 2 || pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 6 || pos.BrandId == 7 || pos.BrandId == 8 || pos.BrandId == 9)
                                {
                                    pos.SeoKeyword = act.SeoTitle;
                                    // pos.IsPurchase = 0;
                                    db.SaveChanges();
                                }

                                ActivateRecord edit = spdb.ActivateRecord.FirstOrDefault(m => m.Id == act.Id);
                                if (edit != null)
                                {
                                    edit.Status = 2;
                                }

                                // 激活奖励
                                // ProfitHelper.Instance.StartListenActiveDo(pos);
                            }
                            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);
            }
        }
    }
}