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

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

        #region 获取单个字段
        public UserAccount Get(int Id)
        {
            WebCMSEntities db = new WebCMSEntities();
            UserAccount order = db.UserAccount.FirstOrDefault(m => m.Id == Id);
            if (order != null)
            {
            }
            db.Dispose();
            return order;
        }
        public decimal GetIncome(int Id, string TradeMonth)
        {
            string key = "UserAccount:Income:" + Id + ":" + TradeMonth;
            if (RedisDbconn.Instance.Exists(key))
            {
                decimal obj = RedisDbconn.Instance.Get<decimal>(key);
                return obj;
            }
            WebCMSEntities db = new WebCMSEntities();
            decimal result = 0;
            DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
            DateTime end = start.AddMonths(1);
            List<int> ChangeTypes = PublicFunction.IncomeTypes();
            bool check = db.UserAccountRecord.Any(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && ChangeTypes.Contains(m.ChangeType));
            if (check)
            {
                result = db.UserAccountRecord.Where(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && ChangeTypes.Contains(m.ChangeType)).Sum(m => m.ChangeAmount);
                RedisDbconn.Instance.Set(key, result);
                RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(180, 300));
            }
            return result;
        }
        public decimal GetExpend(int Id, string TradeMonth)
        {
            string key = "UserAccount:Expend:" + Id + ":" + TradeMonth;
            if (RedisDbconn.Instance.Exists(key))
            {
                decimal obj = RedisDbconn.Instance.Get<decimal>(key);
                return obj;
            }
            decimal result = 0;
            WebCMSEntities db = new WebCMSEntities();
            DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
            DateTime end = start.AddMonths(1);
            List<int> ChangeTypes = PublicFunction.ExpendTypes();
            bool check = db.UserAccountRecord.Any(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && ChangeTypes.Contains(m.ChangeType));
            if (check)
            {
                result += db.UserAccountRecord.Where(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && ChangeTypes.Contains(m.ChangeType)).Sum(m => m.ChangeAmount);
            }
            DateTime checkTime = DateTime.Parse("2022-04-11 19:31:39");
            check = db.UserCashRecord.Any(m => m.UserId == Id && m.CreateDate < checkTime && m.CreateDate >= start && m.CreateDate < end && m.Status == 1);
            if (check)
            {
                result += db.UserCashRecord.Where(m => m.UserId == Id && m.CreateDate < checkTime && m.CreateDate >= start && m.CreateDate < end && m.Status == 1).Sum(m => m.TradeAmount);
            }
            RedisDbconn.Instance.Set(key, result);
            RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(180, 600));
            return result;
        }
        #endregion

    }
}
