using Edu.Model.Entity.Grade;
using Edu.Model.Entity.Sell;
using Edu.Model.Entity.User;
using Edu.Model.ViewModel.Sell;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Edu.Repository.Sell
{
    /// <summary>
    /// 教师绩效仓储层
    /// </summary>
    public class RB_Teaching_PerfRepository : BaseRepository<RB_Teaching_Perf>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Teaching_Perf_ViewModel> GetList(RB_Teaching_Perf_ViewModel demodel)
        {
            string where = $@" 1=1 and p.Status =0";
            
            if (demodel.Group_Id > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.ClassId)} ={demodel.ClassId}";
            }
            if (!string.IsNullOrEmpty(demodel.ClassName))
            {
                where += $@" and c.{nameof(RB_Class.ClassName)} like '%{demodel.ClassName}%'";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.TeacherId)} ={demodel.TeacherId}";
            }
            if (!string.IsNullOrEmpty(demodel.TeacherName))
            {
                where += $@" and t.{nameof(RB_Teacher.TeacherName)} like '%{demodel.TeacherName}%'";
            }
            if (demodel.Q_IsCreateFinance == 1)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.FinanceId)} >0";
            }
            else if (demodel.Q_IsCreateFinance == 2)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.FinanceId)} =0";
            }
            if (demodel.Q_IsComfirm == 1)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.PerfState)} =2";
            }
            else if (demodel.Q_IsComfirm == 2)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.PerfState)} in(1,3)";
            }
            if (!string.IsNullOrEmpty(demodel.StartTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.EndDate)} >='{demodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(demodel.EndTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.EndDate)} <='{demodel.EndTime} 23:59:59'";
            }
            if (!string.IsNullOrEmpty(demodel.OPStartTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.ConfirmDate)} >='{demodel.OPStartTime}'";
            }
            if (!string.IsNullOrEmpty(demodel.OPEndTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.ConfirmDate)} <='{demodel.OPEndTime} 23:59:59'";
            }

            string sql = $@" select p.*,t.TeacherName,t.TeacherIcon,c.ClassName from RB_Teaching_Perf p
left join rb_teacher t on p.TeacherId = t.TId
left join rb_class c on p.ClassId = c.ClassId
where {where} order by p.Id desc";
            return Get<RB_Teaching_Perf_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_Teaching_Perf_ViewModel> GetPageList(int pageIndex,int pageSize,out long count, RB_Teaching_Perf_ViewModel demodel)
        {
            string where = $@" 1=1 and p.Status =0";

            if (demodel.Group_Id > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.ClassId)} ={demodel.ClassId}";
            }
            if (!string.IsNullOrEmpty(demodel.ClassIds))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.ClassId)} in({demodel.ClassIds})";
            }
            if (!string.IsNullOrEmpty(demodel.ClassName))
            {
                where += $@" and c.{nameof(RB_Class.ClassName)} like '%{demodel.ClassName}%'";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.TeacherId)} ={demodel.TeacherId}";
            }
            if (!string.IsNullOrEmpty(demodel.TeacherName))
            {
                where += $@" and t.{nameof(RB_Teacher.TeacherName)} like '%{demodel.TeacherName}%'";
            }
            if (demodel.PerfState > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.PerfState)} ={(int)demodel.PerfState}";
            }
            if (demodel.Q_IsCreateFinance == 1)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.FinanceId)} >0";
            }
            else if (demodel.Q_IsCreateFinance == 2)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.FinanceId)} =0";
            }
            if (demodel.Q_IsComfirm == 1)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.PerfState)} =2";
            }
            else if (demodel.Q_IsComfirm == 2)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.PerfState)} in(1,3)";
            }
            if (!string.IsNullOrEmpty(demodel.StartTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.EndDate)} >='{demodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(demodel.EndTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.EndDate)} <='{demodel.EndTime} 23:59:59'";
            }
            if (!string.IsNullOrEmpty(demodel.OPStartTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.ConfirmDate)} >='{demodel.OPStartTime}'";
            }
            if (!string.IsNullOrEmpty(demodel.OPEndTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.ConfirmDate)} <='{demodel.OPEndTime} 23:59:59'";
            }

            string sql = $@" select p.*,t.TeacherName,t.TeacherHead as TeacherIcon,c.ClassName from RB_Teaching_Perf p
left join rb_teacher t on p.TeacherId = t.TId
left join rb_class c on p.ClassId = c.ClassId
where {where} order by p.Id desc";
            return GetPage<RB_Teaching_Perf_ViewModel>(pageIndex, pageSize, out count, sql).ToList();
        }

        /// <summary>
        /// 获取统计金额
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public decimal GetMyTeachingPerfTotalMoney(RB_Teaching_Perf_ViewModel demodel)
        {
            string where = $@" 1=1 and p.Status =0";

            if (demodel.Group_Id > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.School_Id)} ={demodel.School_Id}";
            }
            if (demodel.ClassId > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.ClassId)} ={demodel.ClassId}";
            }
            if (!string.IsNullOrEmpty(demodel.ClassName))
            {
                where += $@" and c.{nameof(RB_Class.ClassName)} like '%{demodel.ClassName}%'";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.TeacherId)} ={demodel.TeacherId}";
            }
            if (!string.IsNullOrEmpty(demodel.TeacherName))
            {
                where += $@" and t.{nameof(RB_Teacher.TeacherName)} like '%{demodel.TeacherName}%'";
            }
            if (demodel.PerfState > 0)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.PerfState)} ={(int)demodel.PerfState}";
            }
            if (demodel.Q_IsCreateFinance == 1)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.FinanceId)} >0";
            }
            else if (demodel.Q_IsCreateFinance == 2)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.FinanceId)} =0";
            }
            if (demodel.Q_IsComfirm == 1)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.PerfState)} =2";
            }
            else if (demodel.Q_IsComfirm == 2)
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.PerfState)} in(1,3)";
            }
            if (!string.IsNullOrEmpty(demodel.StartTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.EndDate)} >='{demodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(demodel.EndTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.EndDate)} <='{demodel.EndTime} 23:59:59'";
            }
            if (!string.IsNullOrEmpty(demodel.OPStartTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.ConfirmDate)} >='{demodel.OPStartTime}'";
            }
            if (!string.IsNullOrEmpty(demodel.OPEndTime))
            {
                where += $@" and p.{nameof(RB_Teaching_Perf.ConfirmDate)} <='{demodel.OPEndTime} 23:59:59'";
            }

            string sql = $@" select sum(p.Money) as Money from RB_Teaching_Perf p
left join rb_teacher t on p.TeacherId = t.TId
left join rb_class c on p.ClassId = c.ClassId
where {where}";
            var obj = ExecuteScalar(sql);
            return obj == null ? 0 : Convert.ToDecimal(obj);
        }

        /// <summary>
        /// 获取时间范围 班级绩效
        /// </summary>
        /// <param name="group_Id"></param>
        /// <param name="classIds"></param>
        /// <param name="sTime"></param>
        /// <param name="eTime"></param>
        /// <returns></returns>
        public List<RB_Teaching_Perf_ViewModel> GetTeacherPerfForDate(int group_Id,int schoolId, string classIds, string sTime, string eTime)
        {
            string where = "";
            if (!string.IsNullOrEmpty(classIds)) {
                where = $@" and d.ClassId in ({classIds})";
            }
            if (schoolId >= 0)
            {
                where = $@" and c.School_Id ={schoolId}";//left join rb_class c on d.ClassId = c.ClassId
            }

            string sql = $@"SELECT d.ClassId,SUM(d.Money) AS Money FROM rb_teaching_perf d 
left join rb_class c on d.ClassId = c.ClassId
WHERE d.Group_Id ={group_Id} {where} and d.ConfirmDate >='{sTime}' and d.ConfirmDate <='{eTime} 23:59:59'
GROUP BY d.ClassId";
            return Get<RB_Teaching_Perf_ViewModel>(sql).ToList();
        }
    }
}
