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_EmpRepository : BaseRepository<RB_Sell_Achievements_Emp>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Sell_Achievements_Emp_ViewModel> GetList(RB_Sell_Achievements_Emp_ViewModel demodel)
        {
            string where = $@" 1=1";
            
            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (demodel.Type > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.Type)} ={demodel.Type}";
            }
            if (demodel.EmpId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.EmpId)} ={demodel.EmpId}";
            }
            if (demodel.IsDept > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.IsDept)} ={demodel.IsDept}";
            }
            if (demodel.PeriodsId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.PeriodsId)} ={demodel.PeriodsId}";
            }
            if (demodel.GiveOutState > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.GiveOutState)} ={demodel.GiveOutState}";
            }
            if (demodel.IsSelectNor == 1)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.PeriodsId)} >0";
            }
            if (!string.IsNullOrEmpty(demodel.OrderIds))
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.OrderId)} in({demodel.OrderIds})";
            }

            string sql = $@" select r.* from RB_Sell_Achievements_Emp r where {where} order by r.Id desc";
            return Get<RB_Sell_Achievements_Emp_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_Emp_ViewModel> GetPageList(int pageIndex,int pageSize,out long count,RB_Sell_Achievements_Emp_ViewModel demodel)
        {
            string where = $@" 1=1";

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.OrderId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.OrderId)} ={demodel.OrderId}";
            }
            if (demodel.Type > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.Type)} ={demodel.Type}";
            }
            if (demodel.EmpId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.EmpId)} ={demodel.EmpId}";
            }
            if (demodel.IsDept > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.IsDept)} ={demodel.IsDept}";
            }
            if (demodel.PeriodsId > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.PeriodsId)} ={demodel.PeriodsId}";
            }
            if (demodel.GiveOutState > 0)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.GiveOutState)} ={demodel.GiveOutState}";
            }
            if (demodel.IsSelectNor == 1)
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.PeriodsId)} >0";
            }
            if (!string.IsNullOrEmpty(demodel.OrderIds))
            {
                where += $@" and r.{nameof(RB_Sell_Achievements_Emp_ViewModel.OrderId)} in({demodel.OrderIds})";
            }

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


        /// <summary>
        /// 获取当月业绩(个人)
        /// </summary>
        /// <param name="empId"></param>
        /// <param name="periodsId"></param>
        /// <param name="group_Id"></param>
        /// <returns></returns>
        public RB_Sell_Achievements_Emp_ViewModel GetMonthSaleMoney(int empId, int periodsId, int group_Id)
        {
            string sql = $@"SELECT MAX(SaleMoney) as SaleMoney,SUM(PushMoney) as PushMoney FROM rb_sell_achievements_emp WHERE Group_Id ={group_Id} and EmpId ={empId} and IsDept =2 and PeriodsId ={periodsId}";
            return Get<RB_Sell_Achievements_Emp_ViewModel>(sql).FirstOrDefault();
        }

        /// <summary>
        /// 获取当月新收/续费业绩金额(个人)
        /// </summary>
        /// <param name="empId"></param>
        /// <param name="periodsId"></param>
        /// <param name="group_Id"></param>
        /// <returns></returns>
        public List<RB_Sell_Achievements_Emp_ViewModel> GetMonthNorOrderMoney(int empId, int periodsId, int group_Id)
        {
            string sql = $@"
SELECT g.RenewState as Type,SUM(e.OrderSaleMoney) as OrderSaleMoney FROM rb_sell_achievements_emp e
INNER JOIN rb_order_guest g on e.OrderId = g.OrderId
WHERE e.Group_Id ={group_Id} and e.EmpId ={empId} and e.IsDept =2 and e.PeriodsId ={periodsId}
GROUP BY g.RenewState";
            return Get<RB_Sell_Achievements_Emp_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 获取用户累计已发提成（个人）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="group_Id"></param>
        /// <returns></returns>
        public RB_Sell_Achievements_Emp_ViewModel GetEmpTotalCommission(int empId, int group_Id)
        {
            string sql = $@"SELECT SUM(PushMoney) as PushMoney,SUM(GiveOutMoney) as GiveOutMoney FROM rb_sell_achievements_emp WHERE Group_Id ={group_Id} and EmpId ={empId} and IsDept =2";
            return Get<RB_Sell_Achievements_Emp_ViewModel>(sql).FirstOrDefault();
        }
    }
}
