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

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

        // TODO:读取很慢，感觉redis没生效

        #region 获取单个字段
        //个人业绩
        public decimal GetTrade(int UserId, string kind = "self")
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.TradeDaySummary.Any(m => m.UserId == UserId && m.SeoTitle == kind);
            if (check)
            {
                if (kind == "self")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.SeoTitle == kind).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt + m.ProfitDirectTradeAmt + m.ProfitDirectDebitTradeAmt);
                }
                else if (kind == "team")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.SeoTitle == kind).Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
                }
            }
            // RedisDbconn.Instance.Set(key, amt);
            db.Dispose();
            return amt;
        }
        public decimal GetDateTrade(int UserId, string TradeDate, string kind = "self")
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.TradeDaySummary.Any(m => m.UserId == UserId && m.TradeDate == TradeDate && m.SeoTitle == kind);
            if (check)
            {
                if (kind == "self")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.TradeDate == TradeDate && m.SeoTitle == kind).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt + m.ProfitDirectTradeAmt + m.ProfitDirectDebitTradeAmt);
                }
                else if (kind == "team")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.TradeDate == TradeDate && m.SeoTitle == kind).Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
                }
            }
            // RedisDbconn.Instance.Set(key, amt);
            db.Dispose();
            return amt;
        }
        public decimal GetMonthTrade(int UserId, string TradeMonth, string kind = "self")
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.TradeDaySummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == kind);
            if (check)
            {
                if (kind == "self")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == kind).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt + m.ProfitDirectTradeAmt + m.ProfitDirectDebitTradeAmt);
                }
                else if (kind == "team")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == kind).Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
                }
            }
            // RedisDbconn.Instance.Set(key, amt);
            db.Dispose();
            return amt;
        }
        public decimal GetDateTradeForBrand(int UserId, string TradeDate, int BrandId, string kind = "self")
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.TradeDaySummary.Any(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeDate == TradeDate && m.
            SeoTitle == kind);
            if (check)
            {
                if (kind == "self")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeDate == TradeDate && m.SeoTitle == kind).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt + m.ProfitDirectTradeAmt + m.ProfitDirectDebitTradeAmt);
                }
                else if (kind == "team")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeDate == TradeDate && m.SeoTitle == kind).Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
                }
            }
            // RedisDbconn.Instance.Set(key, amt);
            db.Dispose();
            return amt;
        }
        public decimal GetMonthTradeForBrand(int UserId, string TradeMonth, int BrandId, string kind = "self")
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.TradeDaySummary.Any(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonth && m.SeoTitle == kind);
            if (check)
            {
                if (kind == "self")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonth && m.SeoTitle == kind).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt + m.ProfitDirectTradeAmt + m.ProfitDirectDebitTradeAmt);
                }
                else if (kind == "team")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonth && m.SeoTitle == kind).Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
                }
            }
            // RedisDbconn.Instance.Set(key, amt);
            db.Dispose();
            return amt;
        }
        public decimal GetDateTradeForCloud(int UserId, string TradeDate, int QrPayFlag, string kind = "self")
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.TradeDaySummary.Any(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate && m.SeoTitle == kind);
            if (check)
            {
                if (kind == "self")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate && m.SeoTitle == kind).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt + m.ProfitDirectTradeAmt + m.ProfitDirectDebitTradeAmt);
                }
                else if (kind == "team")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate && m.SeoTitle == kind).Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
                }
            }
            // RedisDbconn.Instance.Set(key, amt);
            db.Dispose();
            return amt;
        }
        public decimal GetMonthTradeForCloud(int UserId, string TradeMonth, int QrPayFlag, string kind = "self")
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.TradeDaySummary.Any(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth && m.
            SeoTitle == kind);
            if (check)
            {
                if (kind == "self")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth && m.SeoTitle == kind).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt + m.ProfitDirectTradeAmt + m.ProfitDirectDebitTradeAmt);
                }
                else if (kind == "team")
                {
                    amt = db.TradeDaySummary.Where(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth && m.SeoTitle == kind).Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
                }
            }
            // RedisDbconn.Instance.Set(key, amt);
            db.Dispose();
            return amt;
        }
        #endregion

    }
}
