using System;
using System.Collections.Generic;
using System.Linq;
using MySystem.PxcModels;

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

        /// <summary>
        /// 操作收支明细
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="Money"></param>
        /// <param name="ChangeType"></param>
        public void OpAccount(int UserId, decimal Money, int ChangeType, bool IsTotal = true)
        {
            WebCMSEntities db = new WebCMSEntities();
            UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
            if (account == null)
            {
                account = db.UserAccount.Add(new UserAccount()
                {
                    Id = UserId,
                    UserId = UserId,
                }).Entity;
                db.SaveChanges();
            }
            decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
            decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
            decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
            account.BalanceAmount += Money;
            if(IsTotal)
            {
                account.TotalAmount += Money;
            }
            decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
            decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
            decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
            UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
            {
                CreateDate = DateTime.Now,
                UpdateDate = DateTime.Now,
                UserId = UserId, //创客
                ChangeType = ChangeType, //变动类型
                ChangeAmount = Money, //变更金额
                BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
                AfterTotalAmount = AfterTotalAmount, //变更后总金额
                BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
                AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
                BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
                AfterBalanceAmount = AfterBalanceAmount, //变更后余额
            }).Entity;
            db.SaveChanges();
            db.Dispose();
        }

        public void ToChargeAmount(int UserId, decimal Money)
        {
            WebCMSEntities db = new WebCMSEntities();
            UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
            if (account == null)
            {
                account = db.UserAccount.Add(new UserAccount()
                {
                    Id = UserId,
                    UserId = UserId,
                }).Entity;
                db.SaveChanges();
            }
            account.ToChargeAmount += Money;
            db.SaveChanges();
            db.Dispose();
        }

        /// <summary>
        /// 判断是否达标创客
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public bool IsStandardUser(int UserId)
        {
            WebCMSEntities db = new WebCMSEntities();
            // Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
            UserRankItem user = PosCouponPrizeService.Instance.GetUserLevel(db, UserId);
            if(user.UserType == 1)
            {
                return true;
            }
            if(user.LeaderLevel > 0)
            {
                Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == UserId) ?? new Leaders();
                if(leader.ExpiredDate > DateTime.Now)
                {
                    return true;
                }
            }
            MpMainModels.WebCMSEntities mpdb = new MpMainModels.WebCMSEntities();
            MpMainModels2.WebCMSEntities mpdb2 = new MpMainModels2.WebCMSEntities();
            int couponCount = db.PosCoupons.Count(m => m.UserId == UserId && m.IsUse == 0); //1. 个人名下电签兑换券 + 大机兑换券 ≥ 3
            // TODO:待确认条件
            if(couponCount >= 3)
            {
                db.Dispose();
                mpdb.Dispose();
                mpdb2.Dispose();
                return true;
            }
            else
            {
                List<int> ids = new List<int>();
                ids.Add(10);
                ids.Add(11);
                ids.Add(77);
                ids.Add(78);
                ids.Add(79);
                bool orderCheck = db.Orders.Any(m => m.UserId == UserId && ids.Contains(m.ProductId)); //1. 商城下单过id为10、11、77、78、79的商品
                if(orderCheck)
                {
                    db.Dispose();
                    mpdb.Dispose();
                    mpdb2.Dispose();
                    return true;
                }
                else
                {
                    //1. 激活POS机数量 + 激活来客吧商户数量 + 激活广电卡数量 ≥ 3
                    int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == UserId && m.ActivationState == 1);
                    int ActMerchantCount = mpdb.MerchantDepositOrder.Count(m => m.UserId == UserId && m.Status > 0) + mpdb2.MerchantDepositOrder.Count(m => m.UserId == UserId && m.Status > 0);
                    if(ActiveCount + ActMerchantCount >= 3)
                    {
                        db.Dispose();
                        mpdb.Dispose();
                        mpdb2.Dispose();
                        return true;
                    }
                }
            }
            db.Dispose();
            mpdb.Dispose();
            mpdb2.Dispose();
            return false;
        }

        /// <summary>
        /// 预设职级
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="LeaderKind"></param>
        public void LeaderPreUserLevel(int UserId, int UserLevel, DateTime ExpiredDate)
        {
            WebCMSEntities db = new WebCMSEntities();
            UserRankWhite rank = db.UserRankWhite.FirstOrDefault(m => m.Id == UserId);
            if(rank == null)
            {
                rank = db.UserRankWhite.Add(new UserRankWhite()
                {
                    CreateDate = DateTime.Now, //设置时间
                    UserId = UserId, //用户
                    Id = UserId,
                }).Entity;
                db.SaveChanges();
            }
            rank.Rank = UserLevel;
            rank.UpdateDate = ExpiredDate;
            db.SaveChanges();
            db.Dispose();
        }


        #region 获取创客职级
        public int GetUserLevel(int Id)
        {
            WebCMSEntities db = new WebCMSEntities();
            DateTime now = DateTime.Now;
            List<int> Levels = new List<int>();
            Users user = db.Users.FirstOrDefault(m => m.Id == Id) ?? new Users();
            UserRankWhite userRank = db.UserRankWhite.FirstOrDefault(m => m.Id == Id && m.UpdateDate > now) ?? new UserRankWhite();
            LeaderRankWhite leaderRank = db.LeaderRankWhite.FirstOrDefault(m => m.Id == Id && m.UpdateDate > now) ?? new LeaderRankWhite();
            OperatorRankWhite operatorRank = db.OperatorRankWhite.FirstOrDefault(m => m.Id == Id && m.UpdateDate > now) ?? new OperatorRankWhite();
            Levels.Add(user.UserLevel);
            Levels.Add(userRank.Rank);
            Levels.Add(leaderRank.Rank);
            Levels.Add(operatorRank.Rank);
            db.Dispose();
            return Levels.Max();
        }
        #endregion


    }
}