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
                {
                    if (Utils.PushTimeCheck())
                    {
                        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);
                            decimal PrizeAmt = decimal.Parse(function.CheckNum(act.Field3));
                            if (Utils.Instance.IsHaoDa(int.Parse(function.CheckInt(act.ProductType))) && PrizeAmt > 0)
                            {
                                string PrizeKind = act.SeoKeyword;
                                if ((
                                    PrizeKind == "830_01" ||
                                    PrizeKind == "830_21" ||
                                    PrizeKind == "830_22" ||
                                    PrizeKind == "833_01" ||
                                    PrizeKind == "833_02" ||
                                    PrizeKind == "836_01" ||
                                    PrizeKind == "836_02" ||
                                    PrizeKind == "972" ||
                                    PrizeKind == "972_01" ||
                                    PrizeKind == "985" ||
                                    PrizeKind == "987" ||
                                    PrizeKind == "990" ||
                                    PrizeKind == "993" ||
                                    PrizeKind == "995_21" ||
                                    PrizeKind == "995_22" ||
                                    PrizeKind == "995"
                                    ) && PrizeAmt > 0)
                                {
                                    JavaProductModels.KxsMachine pos = javaprodb.KxsMachine.FirstOrDefault(m => m.PosSn == act.SnNo && m.UserId > 0);
                                    if (pos != null)
                                    {
                                        Dictionary<string, object> prizeData = new Dictionary<string, object>();
                                        prizeData.Add("brand_id", int.Parse(function.CheckInt(act.ProductType)));
                                        prizeData.Add("ext_field", PrizeKind);
                                        prizeData.Add("pos_sn", act.SnNo);
                                        prizeData.Add("user_id", pos.UserId);
                                        prizeData.Add("prize", PrizeAmt);
                                        string req = Newtonsoft.Json.JsonConvert.SerializeObject(prizeData);
                                        string ids = PrizeKind == "833_02" ? "82" : "81";
                                        function.GetWebRequest("http://logic-executor-api.kexiaoshuang.com/noauth/prize/pushbyone?n=QUEUE_KXS_HAODA_PRIZE_CONFIG_DIVISION&m=" + req + "&ids=" + ids + ",83");
                                    }
                                }
                                // 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);
            }
        }
    }
}