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

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

        public void dosomething()
        {
            while (true)
            {
                string data = RedisDbconn.Instance.RPop<string>("AddActQueue");
                if (!string.IsNullOrEmpty(data))
                {
                    try
                    {
                        int PosId = int.Parse(function.CheckInt(data));                    
                        WebCMSEntities db = new WebCMSEntities();
                        PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == PosId);
                        if(pos != null)
                        {
                            decimal CheckMoney = 1000;
                            int CheckDays = 30;
                            DateTime now = DateTime.Now;
                            //判断激活条件并激活
                            DateTime TransferTime = pos.TransferTime == null ? DateTime.Now : pos.TransferTime.Value;
                            if (pos.CreditTrade >= CheckMoney && pos.BuyUserId > 0 && pos.ActivationState == 0 && TransferTime.AddMinutes(-30) < pos.BindingTime)
                            {
                                pos.ActivationState = 1;
                                pos.ActivationTime = DateTime.Now;
                                function.WriteLog("机具" + pos.PosSn, "开机奖励在激活中监控");
                                PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
                                if (merchant != null)
                                {
                                    merchant.ActiveStatus = 1;
                                    merchant.MerStandardDate = DateTime.Now;
                                    db.SaveChanges();
                                    function.WriteLog("商户" + merchant.KqMerNo, "开机奖励在激活中监控");

                                    // 推荐王逻辑(激活)
                                    string SendData = "{\"Kind\":\"3\",\"Data\":{\"UserId\":\"" + pos.BuyUserId + "\",\"PosId\":\"" + pos.Id + "\",\"TradeMonth\":\"" + DateTime.Now.ToString("yyyyMM") + "\"}}";
                                    RedisDbconn.Instance.AddList("RecommendActStatQueue", SendData);

                                    //发放开机奖励
                                    function.WriteLog("首台" + pos.IsFirst, "开机奖励在激活中监控");
                                    if(pos.IsFirst == 1 && pos.BindingTime > DateTime.Now.AddDays(-CheckDays))
                                    {
                                        RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
                                    }
                                }
                                //发放大盟主奖励
                                if(pos.LeaderUserId > 0 && pos.IsFirst == 1 && db.Leaders.Any(m => m.Id == pos.LeaderUserId && m.ExpiredDate > now) && pos.BindingTime > DateTime.Now.AddDays(-CheckDays))
                                {
                                    RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
                                }
                                //发放运营中心奖励
                                if(pos.IsFirst == 1 && pos.BindingTime > DateTime.Now.AddDays(-CheckDays))
                                {
                                    RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
                                }
                                AlipayPayBack2Service.Instance.ActReserveBack(pos.OpId, pos.OpReserve1, pos.OpReserve2, pos.OpReserve3);

                                //统计激活数
                                RedisDbconn.Instance.AddList("StatActQueue", "{\"TradeDate\":\"" + DateTime.Now.ToString("yyyyMMdd") + "\",\"UserId\":\"" + pos.BuyUserId + "\",\"BrandId\":\"" + pos.BrandId + "\"}");
                            }
                            db.Dispose();
                        }
                    }
                    catch (Exception ex)
                    {                        
                        function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "划拨后检查机具激活状态并自动补录异常");
                    }
                }
                else
                {
                    Thread.Sleep(30000);
                }
            }
        }
    }
}