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

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

        #region 获取单个字段
        public Users Get(int Id)
        {
            WebCMSEntities db = new WebCMSEntities();
            Users order = db.Users.FirstOrDefault(m => m.Id == Id);
            if (order != null)
            {
            }
            db.Dispose();
            return order;
        }
        //个人新增创客
        public int GetNewUserCount(int UserId, string TradeMonth = "")
        {
            string key = "AddUser:" + UserId;
            if (!string.IsNullOrEmpty(TradeMonth))
            { 
                key += ":" + TradeMonth;
            }
            if (RedisDbconn.Instance.Exists(key))
            {
                int obj = RedisDbconn.Instance.Get<int>(key);
                if (obj > 0)
                {
                    return obj;
                }
            }
            int count = 0;
            WebCMSEntities db = new WebCMSEntities();
            if (!string.IsNullOrEmpty(TradeMonth))
            {
                if (TradeMonth.Length == 8)
                {
                    DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-" + TradeMonth.Substring(6, 2) + " 00:00:00");
                    DateTime end = start.AddDays(1);
                    count = db.Users.Count(m => m.ParentUserId == UserId && m.AuthDate >= start && m.AuthDate < end);
                }
                else
                {
                    DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
                    DateTime end = start.AddMonths(1);
                    count = db.Users.Count(m => m.ParentUserId == UserId && m.AuthDate >= start && m.AuthDate < end);
                }
            }
            else
            { 
                count = db.Users.Count(m => m.ParentUserId == UserId);
            }
            RedisDbconn.Instance.Set(key, count);
            RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(100, 200));
            db.Dispose();
            return count;
        }
        //团队新增创客
        public int GetTeamNewUserCount(int UserId, string TradeMonth = "")
        {
            string key = "TeamAddUser:" + UserId;
            if (!string.IsNullOrEmpty(TradeMonth))
            { 
                key += ":" + TradeMonth;
            }
            if (RedisDbconn.Instance.Exists(key))
            {
                int obj = RedisDbconn.Instance.Get<int>(key);
                if (obj > 0)
                {
                    return obj;
                }
            }
            int count = 0;
            string UserIdString = "," + UserId + ",";
            WebCMSEntities db = new WebCMSEntities();
            if (!string.IsNullOrEmpty(TradeMonth))
            {
                if (TradeMonth.Length == 8)
                {
                    DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-" + TradeMonth.Substring(6, 2) + " 00:00:00");
                    DateTime end = start.AddDays(1);
                    count = db.Users.Count(m => m.ParentNav.Contains(UserIdString) && m.AuthDate >= start && m.AuthDate < end);
                }
                else
                {
                    DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
                    DateTime end = start.AddMonths(1);
                    count = db.Users.Count(m => m.ParentNav.Contains(UserIdString) && m.AuthDate >= start && m.AuthDate < end);
                }
            }
            else
            { 
                count = db.Users.Count(m => m.ParentNav.Contains(UserIdString));
            }
            RedisDbconn.Instance.Set(key, count);
            RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(100, 200));
            db.Dispose();
            return count;
        }
        #endregion

    }
}
