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

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


        public void Start()
        {
            Thread th = new Thread(ready);
            th.IsBackground = true;
            th.Start();
        }
        
        private void ready()
        { 
            bool op = true;
            while (op)
            {
                string content = RedisDbconn.Instance.RPop<string>("OperateAddServiceQueue");
                if (!string.IsNullOrEmpty(content))
                {
                    try
                    {
                        dosomething(int.Parse(function.CheckInt(content)));
                    }
                    catch(Exception ex)
                    {
                        Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "添加运营中心合伙人异常");
                    }
                }
                else
                {
                    Thread.Sleep(2000);
                }
            }
        }

        public void dosomething(int Id)
        { 
            WebCMSEntities db = new WebCMSEntities();
            OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
            OpModels.SysAdmin edit = opdb.SysAdmin.FirstOrDefault(m => m.Id == Id);
            if (edit != null)
            {
                UserRankItem orderUser = PosCouponPrizeService.Instance.GetUserLevel(db, edit.UserId);
                if(orderUser.UserType != 1)
                {
                    return;
                }
                string ParentNav = orderUser.ParentNav;
                string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
                Array.Reverse(ParentNavList);
                int index = 0;
                bool PrizeFlag = false; //奖励发放标识
                // bool DirectPrizeFlag = false; //奖励发放标识
                foreach(string ParentId in ParentNavList)
                {
                    UserRankItem parentUser = PosCouponPrizeService.Instance.GetUserLevel(db, int.Parse(ParentId));
                    index += 1;

                    if(parentUser.OperateLevel > 1 && PosCouponPrizeService.Instance.CheckOpReserve(opdb, 160000M, parentUser.Id) && !PrizeFlag)
                    {
                        //扣减备用金
                        PosCouponPrizeService.Instance.OperateAmountChange(opdb, parentUser.Id, 160000, 2, 1, "购买运营中心", true);
                        //返回到余额
                        PosCouponPrizeService.Instance.OperateAmountChange(opdb, parentUser.Id, 160000, 1, 2, "购买运营中心", true);
                        PrizeFlag = true;
                    }

                    // if((parentUser.OperateLevel > 0 || parentUser.LeaderLevel > 0) && !DirectPrizeFlag)
                    // {
                    //     //发放5888推荐奖励
                    //     PosCouponPrizeService.Instance.OpAccount(db, parentUser.Id, parentUser.Id, 5888, 1, 129);
                    //     DirectPrizeFlag = true;
                    // }
                }
            }
            db.Dispose();
            opdb.Dispose();
        }
    }
}