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
                {
                    List<string> SaveDataBindIds = Utils.Instance.SaveDataBindIds();
                    SaveDataBindIds.Add("36");
                    WebCMSEntities spdb = new WebCMSEntities();
                    PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
                    MpMainModels2.WebCMSEntities mpdb = new MpMainModels2.WebCMSEntities();
                    JavaProductModels.WebCMSEntities javaprodb = new JavaProductModels.WebCMSEntities();
                    string Month = DateTime.Now.ToString("yyyyMM");
                    string Date = DateTime.Now.ToString("yyyyMMdd");
                    DateTime start = DateTime.Now.AddDays(-30);
                    DateTime end = DateTime.Now.AddMinutes(-1);
                    int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/ActivateRecordId.txt")));
                    IQueryable<ActivateRecord> acts = spdb.ActivateRecord.Where(m => m.Id >= StartId && m.CreateDate >= start && m.CreateDate < end && SaveDataBindIds.Contains(m.ProductType) && m.Status == 1).OrderByDescending(m => m.Id).Take(20);
                    foreach (ActivateRecord act in acts.ToList())
                    {
                        PosPushDataNewHelper.Deposit(act);
                        if(act.ProductType == "36" && decimal.Parse(function.CheckNum(act.Field3)) > 0)
                        {
                            JavaProductModels.KxsMachine pos = javaprodb.KxsMachine.FirstOrDefault(m => m.PosSn == act.SnNo && m.UserId > 0);
                            if(pos != null)
                            {
                                RedisDbconn.Instance.AddList("HaoDaPrizeDataQueue", "{\"brand_id\":36,\"ext_field\":\"" + act.Field3 + "\",\"pos_sn\":\"" + act.SnNo + "\",\"user_id\":" + pos.UserId + "}");
                            }
                        }
                        
                        ActivateRecord edit = spdb.ActivateRecord.FirstOrDefault(m => m.Id == act.Id);
                        if (edit != null)
                        {
                            edit.Status = 2;
                        }
                    }
                    spdb.SaveChanges();
                    spdb.Dispose();
                    db.SaveChanges();
                    db.Dispose();
                    mpdb.Dispose();
                    javaprodb.Dispose();
                }
                catch (Exception ex)
                {
                    Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP激活数据到MAIN异常");
                }
                Thread.Sleep(1000);
            }
        }
    }
}