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_TeaCon_Commission_DetailsRepository : BaseRepository<RB_TeaCon_Commission_Details>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_TeaCon_Commission_Details_ViewModel> GetList(RB_TeaCon_Commission_Details_ViewModel demodel)
        {
            string where = $@" 1=1";

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.TeacherId)} ={demodel.TeacherId}";
            }
            if (!string.IsNullOrEmpty(demodel.TeacherIds))
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.TeacherId)} in({demodel.TeacherIds})";
            }
            if (demodel.ContributeId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.ContributeId)} ={demodel.ContributeId}";
            }


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

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id >= 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.Depart_Id > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.Depart_Id)} ={demodel.Depart_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.TeacherId)} ={demodel.TeacherId}";
            }
            if (!string.IsNullOrEmpty(demodel.TeacherIds))
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.TeacherId)} in({demodel.TeacherIds})";
            }
            if (demodel.ContributeId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.ContributeId)} ={demodel.ContributeId}";
            }


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

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

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id >= 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.Depart_Id > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.Depart_Id)} ={demodel.Depart_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.TeacherId)} ={demodel.TeacherId}";
            }
            if (demodel.ContributeId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.ContributeId)} ={demodel.ContributeId}";
            }


            string sql = $@" select r.TeacherId,r.Depart_Id,r.School_Id,sum(r.CommissionMoney) as CommissionMoney
from RB_TeaCon_Commission_Details r where {where} group by r.TeacherId,r.Depart_Id,r.School_Id";
            return Get<RB_TeaCon_Commission_Details_ViewModel>(sql).ToList();
        }

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

            if (demodel.Group_Id > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id >= 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.Depart_Id > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.Depart_Id)} ={demodel.Depart_Id}";
            }
            if (demodel.PeriodId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.PeriodId)} ={demodel.PeriodId}";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.TeacherId)} ={demodel.TeacherId}";
            }
            if (demodel.ContributeId > 0)
            {
                where += $@" and r.{nameof(RB_TeaCon_Commission_Details_ViewModel.ContributeId)} ={demodel.ContributeId}";
            }


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