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

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

        public void Start(JobMqMsg jobInfo)
        {
            try
            {
                dosomething();
                // string Msg = "success";
                // jobInfo.Status = Msg == "success" ? 1 : 0;
                // jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
                // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
            }
            catch (Exception ex)
            {
                function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
            }
        }

        public void dosomething()
        {
            WebCMSEntities db = new WebCMSEntities();
            string yesterday = DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd HH:mm:ss");
            string today = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            // 统计商户激活数据
            DataTable dt = dbconn.dtable("select UserId,BrandId,MerStandardDate from PosMerchantInfo where MerStandardDate>='" + yesterday + "' and MerStandardDate<'" + today + "' and QueryCount=0");
            dbconn.op("update PosMerchantInfo set QueryCount=1 where MerStandardDate>='" + yesterday + "' and MerStandardDate<'" + today + "' and QueryCount=0");
            foreach (DataRow dr in dt.Rows)
            {
                DateTime date = DateTime.Parse(dr["MerStandardDate"].ToString());
                string TradeMonth = date.ToString("yyyyMM");
                string TradeDate = date.ToString("yyyyMMdd");
                int UserId = int.Parse(dr["UserId"].ToString()); //创客ID
                int BrandId = int.Parse(dr["BrandId"].ToString()); //品牌
                int MerchantCount = 1;
                int Level = 0;
                while (UserId > 0)
                {
                    Level += 1;
                    Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
                    if (user != null)
                    {
                        if (user.AuthFlag == 1)
                        {
                            if (Level == 1)
                            {
                                RedisDbconn.Instance.AddInt("TotalPosMerchant:" + UserId + ":" + TradeDate, MerchantCount); //累计激活POS机商户
                                RedisDbconn.Instance.AddInt("TotalPosMerchant:" + UserId + ":" + TradeMonth, MerchantCount); //累计激活POS机商户
                                RedisDbconn.Instance.AddInt("TotalPosMerchant:" + UserId + ":" + BrandId + ":" + TradeDate, MerchantCount); //累计激活POS机商户
                                RedisDbconn.Instance.AddInt("TotalPosMerchant:" + UserId + ":" + BrandId + ":" + TradeMonth, MerchantCount); //累计激活POS机商户
                            }
                            RedisDbconn.Instance.AddInt("TeamTotalPosMerchant:" + UserId + ":" + TradeDate, MerchantCount); //累计激活POS机商户
                            RedisDbconn.Instance.AddInt("TeamTotalPosMerchant:" + UserId + ":" + TradeMonth, MerchantCount); //累计激活POS机商户
                            RedisDbconn.Instance.AddInt("TeamTotalPosMerchant:" + UserId + ":" + BrandId + ":" + TradeDate, MerchantCount); //累计激活POS机商户
                            RedisDbconn.Instance.AddInt("TeamTotalPosMerchant:" + UserId + ":" + BrandId + ":" + TradeMonth, MerchantCount); //累计激活POS机商户
                        }
                        UserId = user.ParentUserId;
                    }
                    else
                    { 
                        UserId = 0;
                    }
                }
            }
            db.Dispose();
        }

        public void ResetUserData()
        {
            RedisDbconn.Instance.Clear("TotalUser:*");
            RedisDbconn.Instance.Clear("TeamTotalUser:*");
            RedisDbconn.Instance.Clear("AddUser:*");
            RedisDbconn.Instance.Clear("TeamAddUser:*");
            WebCMSEntities db = new WebCMSEntities();
            // 统计创客数据
            DataTable dt = dbconn.dtable("select Id,AuthFlag from Users where AuthFlag=1 order by Id");
            foreach (DataRow dr in dt.Rows)
            {
                int UserId = int.Parse(dr["Id"].ToString()); //创客ID
                DateTime date = DateTime.Parse(dr["AuthFlag"].ToString());
                string TradeMonth = date.ToString("yyyyMM");
                string TradeDate = date.ToString("yyyyMMdd");
                int Level = 0;
                while (UserId > 0)
                {
                    Level += 1;
                    Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
                    if (user != null)
                    {
                        if (user.AuthFlag == 1)
                        {
                            if (Level == 1)
                            {
                                RedisDbconn.Instance.AddInt("TotalUser:" + UserId); //累计总创客                
                                RedisDbconn.Instance.AddInt("AddUser:" + UserId + ":" + TradeDate); //新增创客       
                                RedisDbconn.Instance.AddInt("AddUser:" + UserId + ":" + TradeMonth); //新增创客
                            }
                            RedisDbconn.Instance.AddInt("TeamTotalUser:" + UserId); //累计总创客                
                            RedisDbconn.Instance.AddInt("TeamAddUser:" + UserId + ":" + TradeDate); //新增创客
                            RedisDbconn.Instance.AddInt("TeamAddUser:" + UserId + ":" + TradeMonth); //新增创客
                        }
                        UserId = user.ParentUserId;
                    }
                }
            }
            db.Dispose();
        }

        public void SetUserData(int uid)
        {
            WebCMSEntities db = new WebCMSEntities();
            // 统计创客数据
            DataTable dt = dbconn.dtable("select Id,AuthFlag from Users where Id=" + uid + " and AuthFlag=1 order by Id");
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                int UserId = int.Parse(dr["Id"].ToString()); //创客ID
                DateTime date = DateTime.Parse(dr["AuthFlag"].ToString());
                string TradeMonth = date.ToString("yyyyMM");
                string TradeDate = date.ToString("yyyyMMdd");
                int Level = 0;
                while (UserId > 0)
                {
                    Level += 1;
                    Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
                    if (user != null)
                    {
                        if (user.AuthFlag == 1)
                        {
                            if (Level == 1)
                            {
                                RedisDbconn.Instance.AddInt("TotalUser:" + UserId); //累计总创客                
                                RedisDbconn.Instance.AddInt("AddUser:" + UserId + ":" + TradeDate); //新增创客       
                                RedisDbconn.Instance.AddInt("AddUser:" + UserId + ":" + TradeMonth); //新增创客
                            }
                            RedisDbconn.Instance.AddInt("TeamTotalUser:" + UserId); //累计总创客                
                            RedisDbconn.Instance.AddInt("TeamAddUser:" + UserId + ":" + TradeDate); //新增创客
                            RedisDbconn.Instance.AddInt("TeamAddUser:" + UserId + ":" + TradeMonth); //新增创客
                        }
                        UserId = user.ParentUserId;
                    }
                }
            }
            db.Dispose();
        }
    }
}