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

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

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

        #region 获取单个字段
        //个人业绩
        public decimal GetTrade(int UserId, int IsLeader)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.UserId == UserId);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }
            //子账号
            if (IsLeader == 0)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.PartnerId == UserId);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }

            db.Dispose();
            return amt;
        }


        public decimal GetDateTrade(int UserId, int IsLeader, string TradeDate)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.UserId == UserId && m.TradeDate == TradeDate);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId && m.TradeDate == TradeDate).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }
            //子账号
            if (IsLeader == 0)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.PartnerId == UserId && m.TradeDate == TradeDate);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.PartnerId == UserId && m.TradeDate == TradeDate).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }

            db.Dispose();
            return amt;
        }


        public decimal GetMonthTrade(int UserId, int IsLeader, string TradeMonth)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }
            //子账号
            if (IsLeader == 0)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.PartnerId == UserId && m.TradeMonth == TradeMonth);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.PartnerId == UserId && m.TradeMonth == TradeMonth).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }

            db.Dispose();
            return amt;
        }


        public decimal GetDateTradeForBrand(int UserId, int IsLeader, string TradeDate, int BrandId)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeDate == TradeDate);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeDate == TradeDate).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }
            //子账号
            if (IsLeader == 0)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.PartnerId == UserId && m.BrandId == BrandId && m.TradeDate == TradeDate);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.PartnerId == UserId && m.BrandId == BrandId && m.TradeDate == TradeDate).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }

            db.Dispose();
            return amt;
        }


        public decimal GetMonthTradeForBrand(int UserId, int IsLeader, string TradeMonth, int BrandId)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonth);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonth).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }
            //子账号
            if (IsLeader == 0)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.PartnerId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonth);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.PartnerId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonth).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }

            db.Dispose();
            return amt;
        }


        //贷记卡/借记卡
        public decimal GetDateTradeForCloud(int UserId, int IsLeader, string TradeDate, int QrPayFlag, int BankCardType)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate);
                if (check)
                {
                    if (BankCardType == 0)
                    {
                        amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate).Sum(m => m.HelpDirectDebitTradeAmt + m.NotHelpDirectDebitTradeAmt);
                    }
                    else
                    {
                        amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate).Sum(m => m.HelpDirectTradeAmt + m.NotHelpDirectTradeAmt);
                    }
                }
            }
            //子账号
            if (IsLeader == 0)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.PartnerId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate);
                if (check)
                {
                    if (BankCardType == 0)
                    {
                        amt = db.BusinessTradeSummary.Where(m => m.PartnerId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate).Sum(m => m.HelpDirectDebitTradeAmt + m.NotHelpDirectDebitTradeAmt);
                    }
                    else
                    {
                        amt = db.BusinessTradeSummary.Where(m => m.PartnerId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate).Sum(m => m.HelpDirectTradeAmt + m.NotHelpDirectTradeAmt);
                    }
                }
            }

            db.Dispose();
            return amt;
        }


        //云闪付
        public decimal GetDateTradeForCloud(int UserId, int IsLeader, string TradeDate, int QrPayFlag)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }
            //子账号
            if (IsLeader == 0)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.PartnerId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.PartnerId == UserId && m.QueryCount == QrPayFlag && m.TradeDate == TradeDate).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }

            db.Dispose();
            return amt;
        }


        //云闪付
        public decimal GetMonthTradeForCloud(int UserId, int IsLeader, string TradeMonth, int QrPayFlag)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }
            //子账号
            if (IsLeader == 0)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.PartnerId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.PartnerId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }

            db.Dispose();
            return amt;
        }


        //贷记卡/借记卡
        public decimal GetMonthTradeForCloud(int UserId, int IsLeader, string TradeMonth, int QrPayFlag, int BankCardType)
        {
            decimal amt = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.UserId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }
            //子账号
            if (IsLeader == 0)
            {
                bool check = db.BusinessTradeSummary.Any(m => m.PartnerId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth);
                if (check)
                {
                    amt = db.BusinessTradeSummary.Where(m => m.PartnerId == UserId && m.QueryCount == QrPayFlag && m.TradeMonth == TradeMonth).Sum(m => m.HelpDirectTradeAmt + m.HelpDirectDebitTradeAmt + m.NotHelpDirectTradeAmt + m.NotHelpDirectDebitTradeAmt);

                }
            }

            db.Dispose();
            return amt;
        }


        public int GetActCount(int UserId, int IsLeader, string TradeMonthOrDate = "")
        {
            int MakerCount = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                if (string.IsNullOrEmpty(TradeMonthOrDate))
                {
                    bool check = db.BusinessActSummary.Any(m => m.UserId == UserId);
                    if (check)
                    {
                        MakerCount = db.BusinessActSummary.Where(m => m.UserId == UserId).Sum(m => m.ActCount);
                    }
                }
                else
                {
                    if (TradeMonthOrDate.Length == 8)
                    {
                        bool check = db.BusinessActSummary.Any(m => m.UserId == UserId && m.TradeDate == TradeMonthOrDate);
                        if (check)
                        {
                            MakerCount = db.BusinessActSummary.Where(m => m.UserId == UserId && m.TradeDate == TradeMonthOrDate).Sum(m => m.ActCount);
                        }
                    }
                    else
                    {
                        bool check = db.BusinessActSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonthOrDate);
                        if (check)
                        {
                            MakerCount = db.BusinessActSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonthOrDate).Sum(m => m.ActCount);
                        }
                    }
                }
            }
            //子账号
            if (IsLeader == 0)
            {
                if (string.IsNullOrEmpty(TradeMonthOrDate))
                {
                    bool check = db.BusinessActSummary.Any(m => m.PartnerId == UserId);
                    if (check)
                    {
                        MakerCount = db.BusinessActSummary.Where(m => m.PartnerId == UserId).Sum(m => m.ActCount);
                    }
                }
                else
                {
                    if (TradeMonthOrDate.Length == 8)
                    {
                        bool check = db.BusinessActSummary.Any(m => m.PartnerId == UserId && m.TradeDate == TradeMonthOrDate);
                        if (check)
                        {
                            MakerCount = db.BusinessActSummary.Where(m => m.PartnerId == UserId && m.TradeDate == TradeMonthOrDate).Sum(m => m.ActCount);
                        }
                    }
                    else
                    {
                        bool check = db.BusinessActSummary.Any(m => m.PartnerId == UserId && m.TradeMonth == TradeMonthOrDate);
                        if (check)
                        {
                            MakerCount = db.BusinessActSummary.Where(m => m.PartnerId == UserId && m.TradeMonth == TradeMonthOrDate).Sum(m => m.ActCount);
                        }
                    }
                }
            }

            db.Dispose();
            return MakerCount;
        }


        public int GetBrandActCount(int UserId, int IsLeader, int BrandId, string TradeMonthOrDate = "")
        {
            int MakerCount = 0;
            WebCMSEntities db = new WebCMSEntities();
            //主账号
            if (IsLeader == 1)
            {
                if (TradeMonthOrDate.Length == 8)
                {
                    bool check = db.BusinessActSummary.Any(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeDate == TradeMonthOrDate);
                    if (check)
                    {
                        MakerCount = db.BusinessActSummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeDate == TradeMonthOrDate).Sum(m => m.ActCount);
                    }
                }
                else
                {
                    bool check = db.BusinessActSummary.Any(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonthOrDate);
                    if (check)
                    {
                        MakerCount = db.BusinessActSummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonthOrDate).Sum(m => m.ActCount);
                    }
                }
            }
            //子账号
            if (IsLeader == 0)
            {
                if (TradeMonthOrDate.Length == 8)
                {
                    bool check = db.BusinessActSummary.Any(m => m.PartnerId == UserId && m.BrandId == BrandId && m.TradeDate == TradeMonthOrDate);
                    if (check)
                    {
                        MakerCount = db.BusinessActSummary.Where(m => m.PartnerId == UserId && m.BrandId == BrandId && m.TradeDate == TradeMonthOrDate).Sum(m => m.ActCount);
                    }
                }
                else
                {
                    bool check = db.BusinessActSummary.Any(m => m.PartnerId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonthOrDate);
                    if (check)
                    {
                        MakerCount = db.BusinessActSummary.Where(m => m.PartnerId == UserId && m.BrandId == BrandId && m.TradeMonth == TradeMonthOrDate).Sum(m => m.ActCount);
                    }
                }
            }

            db.Dispose();
            return MakerCount;
        }


        //激活机具数
        public int GetCount(int UserId, int BrandId)
        {
            int ActCount = 0;
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.BrandId == BrandId && m.SeoTitle == "self");
            if (check)
            {
                ActCount = db.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.BrandId == BrandId && m.SeoTitle == "self").Sum(m => m.ActiveBuddyMerStatus);
            }
            db.Dispose();
            return ActCount;
        }


        #endregion

    }
}
