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

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

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

        public void doSomething()
        {
            while (true)
            {
                string content = RedisDbconn.Instance.RPop<string>("SIMActCountQueue");
                if (!string.IsNullOrEmpty(content))
                {
                    try
                    {
                        JsonData jsonObj = JsonMapper.ToObject(content);
                        int UserId = int.Parse(jsonObj["UserId"].ToString());
                        int ActCount = int.Parse(jsonObj["ActCount"].ToString());
                        WebCMSEntities db = new WebCMSEntities();
                        Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
                        string ParentNav = user.ParentNav;
                        UserSimActSummary selfStat = db.UserSimActSummary.FirstOrDefault(m => m.UserId == UserId && m.Kind == 0);
                        if (selfStat == null)
                        {
                            selfStat = db.UserSimActSummary.Add(new UserSimActSummary()
                            {
                                UserId = UserId
                            }).Entity;
                            db.SaveChanges();
                        }
                        selfStat.ActCount += ActCount;
                        ParentNav += "," + UserId + ",";
                        if (!string.IsNullOrEmpty(ParentNav))
                        {
                            string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
                            foreach (string NavUserIdString in ParentNavList)
                            {
                                int NavUserId = int.Parse(NavUserIdString);
                                UserSimActSummary teamStat = db.UserSimActSummary.FirstOrDefault(m => m.UserId == NavUserId && m.Kind == 1);
                                if (teamStat == null)
                                {
                                    teamStat = db.UserSimActSummary.Add(new UserSimActSummary()
                                    {
                                        UserId = NavUserId,
                                        Kind = 1,
                                    }).Entity;
                                    db.SaveChanges();
                                }
                                teamStat.ActCount += ActCount;
                            }
                        }
                        db.SaveChanges();
                        db.Dispose();
                    }
                    catch (Exception ex)
                    {
                        function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "广电卡激活数变更异常");
                    }
                }
                else
                {
                    Thread.Sleep(60000);
                }
            }
        }
    }
}