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

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

        public void Start()
        {
            Thread th = new Thread(StartDo);
            th.IsBackground = true;
            th.Start();
        }
        public void StartDo()
        {
            while(true)
            {
                string content = RedisDbconn.Instance.RPop<string>("ActiveRewardFailQueue");
                if(!string.IsNullOrEmpty(content))
                {
                    try
                    {
                        DoSomething(content);
                    }
                    catch(Exception ex)
                    {
                        Utils.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "实时监听无商户号未发奖励异常");
                    }
                }
                Thread.Sleep(20000);
            }
        }
        public void DoSomething(string content)
        {
            JsonData jsonObj = JsonMapper.ToObject(content);
            int PosId = int.Parse(function.CheckInt(jsonObj["PosId"].ToString()));
            string UserId = jsonObj["UserId"].ToString();
            decimal ActPrize = decimal.Parse(function.CheckNum(jsonObj["ActPrize"].ToString()));
            int ChangeType = int.Parse(function.CheckInt(jsonObj["ChangeType"].ToString()));
            int ActType = int.Parse(function.CheckInt(jsonObj["ActType"].ToString()));
            WebCMSEntities db = new WebCMSEntities();
            PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == PosId);
            if(pos != null)
            {
                PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
                if(merchant != null)
                {
                    if(!string.IsNullOrEmpty(merchant.KqMerNo))
                    {
                        Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
                        int GetUserId = user.Id;
                        string ParentNav = user.ParentNav;
                        int TopUserId = 0;
                        if (!string.IsNullOrEmpty(ParentNav))
                        {
                            TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
                        }
                        StatService.Instance.doActiveReward(db, merchant, pos, GetUserId, ParentNav, TopUserId, ActPrize, ChangeType, ActType);
                        db.Dispose();
                        return;
                    }
                    else
                    {
                        RedisDbconn.Instance.AddList("ActiveRewardFailQueue", content);
                    }
                }
                else
                {
                    RedisDbconn.Instance.AddList("ActiveRewardFailQueue", content);
                }
            }
            db.Dispose();
        }
    }
}