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

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

        #region 获取单个字段
        //个人业绩
        public UserTradeData GetTradeByDate(int UserId, string TradeDate, string kind = "self")
        {
            string key = "UserTradeAmt:" + UserId + ":" + TradeDate + ":" + kind; //总交易
            if (kind == "team")
            {
                key = "Team" + key;
            }
            UserTradeData obj = RedisDbconn.Instance.Get<UserTradeData>(key);
            if (obj != null)
            {
                return obj;
            }
            obj = new UserTradeData();
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.UserTradeDaySummary.Any(m => m.UserId == UserId && m.SeoTitle == kind);
            if (check)
            {
                List<UserTradeDaySummary> data = db.UserTradeDaySummary.Where(m => m.UserId == UserId && m.TradeDate == TradeDate && m.SeoTitle == kind).ToList();
                if (kind == "self")
                {
                    obj.TotalAmt = data.Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
                    if (data.Where(m => m.QueryCount == 0).Count() > 0)
                    {
                        obj.PosAmt = data.Where(m => m.QueryCount == 0).Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
                    }
                    else if (data.Where(m => m.QueryCount == 1).Count() > 0)
                    {
                        obj.QrAmt = data.Where(m => m.QueryCount == 1).Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
                    }
                    obj.Brands = new List<Brand>();
                    foreach (UserTradeDaySummary sub in data.OrderBy(m => m.BrandId).ToList())
                    {
                        if (obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId) == null)
                        {
                            obj.Brands.Add(new Brand()
                            {
                                Id = sub.BrandId,
                                Amt = sub.DirectTradeAmt + sub.DirectDebitTradeAmt,
                            });
                        }
                        else
                        {
                            obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId).Amt += sub.DirectTradeAmt + sub.DirectDebitTradeAmt;
                        }
                    }
                }
                else if (kind == "team")
                {
                    obj.TotalAmt = data.Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
                    if (data.Where(m => m.QueryCount == 0).Count() > 0)
                    {
                        obj.PosAmt = data.Where(m => m.QueryCount == 0).Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
                    }
                    else if (data.Where(m => m.QueryCount == 1).Count() > 0)
                    {
                        obj.QrAmt = data.Where(m => m.QueryCount == 1).Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
                    }
                    obj.Brands = new List<Brand>();
                    foreach (UserTradeDaySummary sub in data.OrderBy(m => m.BrandId).ToList())
                    {
                        if (obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId) == null)
                        {
                            obj.Brands.Add(new Brand()
                            {
                                Id = sub.BrandId,
                                Amt = sub.NonDirectTradeAmt + sub.NonDirectDebitTradeAmt,
                            });
                        }
                        else
                        {
                            obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId).Amt += sub.NonDirectTradeAmt + sub.NonDirectDebitTradeAmt;
                        }
                    }
                }
            }
            RedisDbconn.Instance.Set(key, obj);
            db.Dispose();
            return obj;
        }

        public UserTradeData GetTradeByMonth(int UserId, string TradeMonth, string kind = "self")
        {
            string key = "UserTradeAmt:" + UserId + ":" + TradeMonth + ":" + kind; //总交易
            if (kind == "team")
            {
                key = "Team" + key;
            }
            UserTradeData obj = RedisDbconn.Instance.Get<UserTradeData>(key);
            if (obj != null)
            {
                return obj;
            }
            obj = new UserTradeData();
            WebCMSEntities db = new WebCMSEntities();
            bool check = db.UserTradeDaySummary.Any(m => m.UserId == UserId && m.SeoTitle == kind);
            if (check)
            {
                List<UserTradeDaySummary> data = db.UserTradeDaySummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == kind).ToList();
                if (kind == "self")
                {
                    obj.TotalAmt = data.Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
                    if (data.Where(m => m.QueryCount == 0).Count() > 0)
                    {
                        obj.PosAmt = data.Where(m => m.QueryCount == 0).Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
                    }
                    else if (data.Where(m => m.QueryCount == 1).Count() > 0)
                    {
                        obj.QrAmt = data.Where(m => m.QueryCount == 1).Sum(m => m.DirectTradeAmt + m.DirectDebitTradeAmt);
                    }
                    obj.Brands = new List<Brand>();
                    foreach (UserTradeDaySummary sub in data.OrderBy(m => m.BrandId).ToList())
                    {
                        if (obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId) == null)
                        {
                            obj.Brands.Add(new Brand()
                            {
                                Id = sub.BrandId,
                                Amt = sub.DirectTradeAmt + sub.DirectDebitTradeAmt,
                            });
                        }
                        else
                        {
                            obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId).Amt += sub.DirectTradeAmt + sub.DirectDebitTradeAmt;
                        }
                    }
                }
                else if (kind == "team")
                {
                    obj.TotalAmt = data.Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
                    if (data.Where(m => m.QueryCount == 0).Count() > 0)
                    {
                        obj.PosAmt = data.Where(m => m.QueryCount == 0).Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
                    }
                    else if (data.Where(m => m.QueryCount == 1).Count() > 0)
                    {
                        obj.QrAmt = data.Where(m => m.QueryCount == 1).Sum(m => m.NonDirectTradeAmt + m.NonDirectDebitTradeAmt);
                    }
                    obj.Brands = new List<Brand>();
                    foreach (UserTradeDaySummary sub in data.OrderBy(m => m.BrandId).ToList())
                    {
                        if (obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId) == null)
                        {
                            obj.Brands.Add(new Brand()
                            {
                                Id = sub.BrandId,
                                Amt = sub.NonDirectTradeAmt + sub.NonDirectDebitTradeAmt,
                            });
                        }
                        else
                        {
                            obj.Brands.FirstOrDefault(m => m.Id == sub.BrandId).Amt += sub.NonDirectTradeAmt + sub.NonDirectDebitTradeAmt;
                        }
                    }
                }
            }
            RedisDbconn.Instance.Set(key, obj);
            db.Dispose();
            return obj;
        }
        
        #endregion

    }
}
