using System;
using System.Collections.Generic;
using Library;
using System.Linq;
using System.Data;
using MySystem.MainModels;

namespace MySystem
{
    public class UserDataDbconn
    {
        public readonly static UserDataDbconn Instance = new UserDataDbconn();

        #region 获取单个字段
        public UserData Get(int Id)
        {
            // string key = "UserData:" + Id;
            // if (RedisDbconn.Instance.Exists(key))
            // {
            //     UserData obj = RedisDbconn.Instance.Get<UserData>(key);
            //     if (obj != null)
            //     {
            //         return obj;
            //     }
            // }
            WebCMSEntities db = new WebCMSEntities();
            UserData userData = db.UserData.FirstOrDefault(m => m.UserId == Id);
            if (userData != null)
            {
                // RedisDbconn.Instance.Set(key, userData);
                // RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
            }
            return userData;
        }
        public int GetSelfMakerCount(int Id)
        {
            string key = "MakerCount:" + Id;
            if (RedisDbconn.Instance.Exists(key))
            {
                int obj = RedisDbconn.Instance.Get<int>(key);
                return obj;
            }
            WebCMSEntities db = new WebCMSEntities();
            UserData userData = db.UserData.FirstOrDefault(m => m.UserId == Id);
            if (userData != null)
            {
                RedisDbconn.Instance.Set(key, userData.FansCount);
                RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
            }
            return 0;
        }
        public int GetTeamMakerCount(int Id)
        {
            string key = "TeamMakerCount:" + Id;
            if (RedisDbconn.Instance.Exists(key))
            {
                int obj = RedisDbconn.Instance.Get<int>(key);
                return obj;
            }
            WebCMSEntities db = new WebCMSEntities();
            string IdString = "," + Id + ",";
            int MakerCount = db.Users.Count(m => m.ParentNav.Contains(IdString) && m.AuthFlag == 1);
            RedisDbconn.Instance.Set(key, MakerCount);
            RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
            return MakerCount;
        }
        public int GetSelfMakerCountForMonth(int Id, string TradeMonth)
        {
            string key = "AddMakerCount:" + Id + ":" + TradeMonth;
            if (RedisDbconn.Instance.Exists(key))
            {
                int obj = RedisDbconn.Instance.Get<int>(key);
                return obj;
            }
            DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
            DateTime end = start.AddMonths(1);
            WebCMSEntities db = new WebCMSEntities();
            int MakerCount = db.Users.Count(m => m.ParentUserId == Id && m.AuthDate >= start && m.AuthDate < end);
            RedisDbconn.Instance.Set(key, MakerCount);
            RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
            return MakerCount;
        }
        public int GetTeamMakerCountForMonth(int Id, string TradeMonth)
        {
            string key = "TeamAddMakerCount:" + Id + ":" + TradeMonth;
            if (RedisDbconn.Instance.Exists(key))
            {
                int obj = RedisDbconn.Instance.Get<int>(key);
                return obj;
            }
            DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
            DateTime end = start.AddMonths(1);
            WebCMSEntities db = new WebCMSEntities();
            string IdString = "," + Id + ",";
            int MakerCount = db.Users.Count(m => m.ParentNav.Contains(IdString) && m.AuthDate >= start && m.AuthDate < end);
            RedisDbconn.Instance.Set(key, MakerCount);
            RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
            return MakerCount;
        }

        public int GetPosActCount(int UserId, string TradeMonthOrDate = "")
        {
            int MakerCount = 0;
            // string key = "TotalPosMerchant:" + Id;
            // if (RedisDbconn.Instance.Exists(key))
            // {
            //     int obj = RedisDbconn.Instance.Get<int>(key);
            //     return obj;
            // }
            // WebCMSEntities db = new WebCMSEntities();
            // string condi = "";
            // if (!string.IsNullOrEmpty(TradeMonth))
            // {
            //     string start = TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00";
            //     string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
            //     condi += " and ActivationTime>='" + start + "' and ActivationTime<'" + end + "'";
            // }
            // OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
            // DataTable dt = OtherMySqlConn.dtable("select count(Id) from PosMachinesTwo where BuyUserId=" + Id + " and ActivationState=1 and UserId>0" + condi);
            // if (dt.Rows.Count > 0)
            // {
            //     MakerCount = int.Parse(function.CheckInt(dt.Rows[0][0].ToString()));
            // }
            // RedisDbconn.Instance.Set(key, MakerCount);
            // OtherMySqlConn.connstr = "";
            WebCMSEntities db = new WebCMSEntities();
            if(string.IsNullOrEmpty(TradeMonthOrDate))
            {
                bool check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.SeoTitle == "self");
                if (check)
                {
                    MakerCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.SeoTitle == "self").Sum(m => m.ActiveBuddyMerStatus);
                }
            }
            else
            {
                if(TradeMonthOrDate.Length == 8)
                {
                    bool check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.SeoKeyword == TradeMonthOrDate && m.SeoTitle == "self");
                    if (check)
                    {
                        MakerCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.SeoKeyword == TradeMonthOrDate && m.SeoTitle == "self").Sum(m => m.ActiveBuddyMerStatus);
                    }
                }
                else
                {
                    bool check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonthOrDate && m.SeoTitle == "self");
                    if (check)
                    {
                        MakerCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonthOrDate && m.SeoTitle == "self").Sum(m => m.ActiveBuddyMerStatus);
                    }
                }
            }
            return MakerCount;
        }

        public int GetTeamPosActCount(int UserId, string TradeMonthOrDate = "")
        {
            int MakerCount = 0;
            // string key = "TeamTotalPosMerchant:" + Id;
            // if (RedisDbconn.Instance.Exists(key))
            // {
            //     int obj = RedisDbconn.Instance.Get<int>(key);
            //     return obj;
            // }
            // WebCMSEntities db = new WebCMSEntities();
            // string condi = "";
            // if (!string.IsNullOrEmpty(TradeMonth))
            // {
            //     string start = TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00";
            //     string end = DateTime.Parse(start).AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss");
            //     condi += " and ActivationTime>='" + start + "' and ActivationTime<'" + end + "'";
            // }
            // OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
            // DataTable dt = OtherMySqlConn.dtable("select count(Id) from PosMachinesTwo where BuyUserId in (select Id from Users where ParentNav like '%," + Id + ",%' or Id=" + Id + ") and ActivationState=1 and UserId>0" + condi);
            // if (dt.Rows.Count > 0)
            // {
            //     MakerCount = int.Parse(function.CheckInt(dt.Rows[0][0].ToString()));
            // }
            // RedisDbconn.Instance.Set(key, MakerCount);
            // OtherMySqlConn.connstr = "";
            WebCMSEntities db = new WebCMSEntities();
            if(string.IsNullOrEmpty(TradeMonthOrDate))
            {
                bool check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.SeoTitle == "team");
                if (check)
                {
                    MakerCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.SeoTitle == "team").Sum(m => m.ActiveBuddyMerStatus);
                }
            }
            else
            {
                if(TradeMonthOrDate.Length == 8)
                {
                    bool check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.SeoKeyword == TradeMonthOrDate && m.SeoTitle == "team");
                    if (check)
                    {
                        MakerCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.SeoKeyword == TradeMonthOrDate && m.SeoTitle == "team").Sum(m => m.ActiveBuddyMerStatus);
                    }
                }
                else
                {
                    bool check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonthOrDate && m.SeoTitle == "team");
                    if (check)
                    {
                        MakerCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonthOrDate && m.SeoTitle == "team").Sum(m => m.ActiveBuddyMerStatus);
                    }
                }
            }
            db.Dispose();
            return MakerCount;
        }

        public int GetTeamBrandPosActCount(int UserId, int BrandId, string TradeMonthOrDate = "")
        {
            int MakerCount = 0;
            WebCMSEntities db = new WebCMSEntities();
            if(TradeMonthOrDate.Length == 8)
            {
                bool check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.BrandId == BrandId && m.SeoKeyword == TradeMonthOrDate && m.SeoTitle == "team");
                if (check)
                {
                    MakerCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.SeoKeyword == TradeMonthOrDate && m.SeoTitle == "team").Sum(m => m.ActiveBuddyMerStatus);
                }
            }
            else
            {
                bool check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonthOrDate && m.SeoTitle == "team");
                if (check)
                {
                    MakerCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonthOrDate && m.SeoTitle == "team").Sum(m => m.ActiveBuddyMerStatus);
                }
            }
            db.Dispose();
            return MakerCount;
        }
        #endregion

    }
}
