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)
        {
            WebCMSEntities db = new WebCMSEntities();
            UserData userData = db.UserData.FirstOrDefault(m => m.UserId == Id);
            if (userData != null)
            {
            }
            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;
            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 GetSelfBrandPosActCount(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 == "self");
                if (check)
                {
                    MakerCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.SeoKeyword == TradeMonthOrDate && m.SeoTitle == "self").Sum(m => m.ActiveBuddyMerStatus);
                }
            }
            else
            {
                bool check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonthOrDate && m.SeoTitle == "self");
                if (check)
                {
                    MakerCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonthOrDate && m.SeoTitle == "self").Sum(m => m.ActiveBuddyMerStatus);
                }
            }
            db.Dispose();
            return MakerCount;
        }

        public int GetTeamPosActCount(int UserId, string TradeMonthOrDate = "")
        {
            int MakerCount = 0;
            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;
        }

        /// <summary>
        /// 获取来客吧区域代理交易额
        /// </summary>
        /// <param name="UserId">创客Id</param>
        /// <param name="TradeMonthOrDate">统计时间</param>
        /// <returns></returns>
        public decimal GetMpAgentTradeAmount(int UserId, string TradeMonthOrDate = "")
        {
            decimal TradeAmount = 0.00M;
            MpMainModels.WebCMSEntities mpdb = new MpMainModels.WebCMSEntities();
            MpMainModels2.WebCMSEntities mpdb2 = new MpMainModels2.WebCMSEntities();
            if (TradeMonthOrDate.Length == 8)
            {
                bool check = mpdb.AgentTradeStatSummary.Any(m => m.UserId == UserId && m.TradeDate == TradeMonthOrDate);
                bool check2 = mpdb2.AgentTradeStatSummary.Any(m => m.UserId == UserId && m.TradeDate == TradeMonthOrDate);
                if (check)
                {
                    TradeAmount = mpdb.AgentTradeStatSummary.Where(m => m.UserId == UserId  && m.TradeDate == TradeMonthOrDate).Sum(m => m.TotalAmount);
                }
                if (check2)
                {
                    TradeAmount += mpdb2.AgentTradeStatSummary.Where(m => m.UserId == UserId  && m.TradeMonth == TradeMonthOrDate).Sum(m => m.TotalAmount);
                }
            }
            else
            {
                bool check = mpdb.AgentTradeStatSummary.Any(m => m.UserId == UserId  && m.TradeMonth == TradeMonthOrDate);
                bool check2 = mpdb2.AgentTradeStatSummary.Any(m => m.UserId == UserId && m.TradeDate == TradeMonthOrDate);
                if (check)
                {
                    TradeAmount = mpdb.AgentTradeStatSummary.Where(m => m.UserId == UserId  && m.TradeMonth == TradeMonthOrDate).Sum(m => m.TotalAmount);
                }
                if (check2)
                {
                    TradeAmount += mpdb2.AgentTradeStatSummary.Where(m => m.UserId == UserId  && m.TradeMonth == TradeMonthOrDate).Sum(m => m.TotalAmount);
                }
            }
            mpdb.Dispose();
            mpdb2.Dispose();
            return TradeAmount;
        }
        #endregion

    }
}
