using Edu.Common.Enum;
using Edu.Model.Entity.Course;
using Edu.Model.Entity.Sell;
using Edu.Model.ViewModel.Course;
using Edu.Model.ViewModel.Sell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;

namespace Edu.Repository.Sell
{
    /// <summary>
    /// 业绩提成期数明细仓储层
    /// </summary>
    public class RB_Sell_Achievements_DetailsRepository : BaseRepository<RB_Sell_Achievements_Details>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Sell_Achievements_Details_ViewModel> GetList(RB_Sell_Achievements_Details_ViewModel demodel)
        {
            string where = $@" 1=1";

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.UserId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserId)} ={demodel.UserId}";
            }
            if (!string.IsNullOrEmpty(demodel.UserIds))
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserId)} in({demodel.UserIds})";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.ClassId)} ={demodel.ClassId}";
            }
            if (!string.IsNullOrEmpty(demodel.ClassIds))
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.ClassId)} in({demodel.ClassIds})";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (!string.IsNullOrEmpty(demodel.OrderIds))
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.OrderId)} in({demodel.OrderIds})";
            }
            if (demodel.UserType > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserType)} ={demodel.UserType}";
            }
            if (demodel.IsDept > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.IsDept)} ={demodel.IsDept}";
            }


            string sql = $@" select r.* from RB_Sell_Achievements_Details r where {where} order by r.Id asc";
            return Get<RB_Sell_Achievements_Details_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取用户明细统计
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Sell_Achievements_Details_ViewModel> GetSellCommissionUserDetailList(RB_Sell_Achievements_Details_ViewModel demodel)
        {
            string where = $@" 1=1";

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.UserId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserId)} ={demodel.UserId}";
            }
            if (!string.IsNullOrEmpty(demodel.UserIds))
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserId)} in({demodel.UserIds})";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.ClassId)} ={demodel.ClassId}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (!string.IsNullOrEmpty(demodel.OrderIds))
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.OrderId)} in({demodel.OrderIds})";
            }
            if (demodel.UserType > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserType)} ={demodel.UserType}";
            }
            if (demodel.IsDept > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.IsDept)} ={demodel.IsDept}";
            }


            string sql = $@" select r.*,
(select count(0) from RB_Sell_Achievements_Details r1 where r1.UserId = r.UserId and r1.OrderId = r.OrderId and r1.IsDept = r.IsDept and r1.Id <= r.Id) as IssueNum
from RB_Sell_Achievements_Details r where {where} order by r.Id asc";
            return Get<RB_Sell_Achievements_Details_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Sell_Achievements_Details_ViewModel> GetPageList(int pageIndex, int pageSize, out long count, RB_Sell_Achievements_Details_ViewModel demodel)
        {
            string where = $@" 1=1";

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id >= 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.Depart_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.Depart_Id)} ={demodel.Depart_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.UserId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserId)} ={demodel.UserId}";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.ClassId)} ={demodel.ClassId}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (demodel.UserType > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserType)} ={demodel.UserType}";
            }
            if (demodel.IsDept > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.IsDept)} ={demodel.IsDept}";
            }

            string sql = $@" select r.* from RB_Sell_Achievements_Details r where {where} order by r.Id desc";
            return GetPage<RB_Sell_Achievements_Details_ViewModel>(pageIndex, pageSize, out count, sql).ToList();
        }

        /// <summary>
        /// 获取用户提成列表
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Sell_Achievements_Details_ViewModel> GetSellCommissionUserList(RB_Sell_Achievements_Details_ViewModel demodel)
        {
            string where = $@" 1=1";

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id >= 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.UserId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserId)} ={demodel.UserId}";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.ClassId)} ={demodel.ClassId}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (demodel.UserType > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserType)} ={demodel.UserType}";
            }
            if (demodel.IsDept > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.IsDept)} ={demodel.IsDept}";
            }


            string sql = $@" select r.UserId,r.Depart_Id,r.School_Id,sum(r.StudentCount) as StudentCount,sum(r.CurrentPeriodMoney + r.DeductionMoney) as CurrentPeriodMoney
from RB_Sell_Achievements_Details r where {where} group by r.UserId,r.Depart_Id,r.School_Id";
            return Get<RB_Sell_Achievements_Details_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取每期校区对应提成
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="periodsIds"></param>
        /// <returns></returns>
        public List<RB_Sell_Achievements_Details_ViewModel> GetSchoolCommission(int schoolId, string periodsIds)
        {
            string sql = $@"select PeriodId,SUM(CurrentPeriodMoney) as CurrentPeriodMoney from RB_Sell_Achievements_Details 
WHERE School_Id ={schoolId} and PeriodId in ({periodsIds})
GROUP BY PeriodId ";

            return Get<RB_Sell_Achievements_Details_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 累计提成金额
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public decimal GetSellCommissionStatistics(RB_Sell_Achievements_Details_ViewModel demodel)
        {
            string where = $@" 1=1";

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id >= 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.Depart_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.Depart_Id)} ={demodel.Depart_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.UserId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserId)} ={demodel.UserId}";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.ClassId)} ={demodel.ClassId}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (demodel.UserType > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserType)} ={demodel.UserType}";
            }
            if (demodel.IsDept > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.IsDept)} ={demodel.IsDept}";
            }


            string sql = $@" select sum(r.CurrentPeriodMoney + r.DeductionMoney) from RB_Sell_Achievements_Details r where {where}";
            var obj = ExecuteScalar(sql);
            return obj == null ? 0 : Convert.ToDecimal(obj);
        }

        /// <summary>
        /// 获取销售提成根据用户id
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Sell_Achievements_Details_ViewModel> GetSellCommissionByUser(RB_Sell_Achievements_Details_ViewModel demodel)
        {
            string where = $@" 1=1";

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id >= 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.Depart_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.Depart_Id)} ={demodel.Depart_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.UserId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserId)} ={demodel.UserId}";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.ClassId)} ={demodel.ClassId}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (!string.IsNullOrWhiteSpace(demodel.PeriodsTime))
            {
                where += $@" and DATE_FORMAT(STR_TO_DATE(CONCAT(p.Periods,'-01'),'%Y-%m-%d'),'%Y-%m-%d')>=DATE_FORMAT('{demodel.PeriodsTime}','%Y-%m-%d')";
            }
            if (demodel.UserType > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.UserType)} ={demodel.UserType}";
            }
            if (demodel.IsDept > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Details_ViewModel.IsDept)} ={demodel.IsDept}";
            }

            string sql = $@" SELECT
	r.*, p.Periods AS PeriodsTime
FROM
	RB_Sell_Achievements_Details r
LEFT JOIN rb_sell_achievements_periods AS p ON r.PeriodId = p.Id where {where} ";
            return Get<RB_Sell_Achievements_Details_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取日期范围 应发放的销售提成
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="classIds"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<RB_Sell_Achievements_Details_ViewModel> GetSellCommissionForDate(int group_Id, int schoolId, string classIds, string startTime, string endTime)
        {
            string where = "";
            if (!string.IsNullOrEmpty(classIds)) {
                where += $@" and d.ClassId in ({classIds})";
            }
            if (schoolId >= 0)
            {
                where = $@" and c.School_Id ={schoolId}";
            }

            string sql = $@"SELECT d.ClassId,SUM(d.CurrentPeriodMoney + d.DeductionMoney) AS CurrentPeriodMoney FROM RB_Sell_Achievements_Details d 
INNER JOIN rb_sell_achievements_periods p on d.PeriodId = p.Id
left join rb_class c on d.ClassId = c.ClassId
WHERE p.Group_Id ={group_Id} {where} and STR_TO_DATE(CONCAT(p.Periods,'-01'),'%Y-%m-%d') >='{startTime}' and STR_TO_DATE(CONCAT(p.Periods,'-01'),'%Y-%m-%d') <='{endTime} 23:59:59'
GROUP BY d.ClassId";
            return Get<RB_Sell_Achievements_Details_ViewModel>(sql).ToList();
        }
    }
}
