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_Teaching_BonusRepository : BaseRepository<RB_Teaching_Bonus>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Teaching_Bonus_ViewModel> GetList(RB_Teaching_Bonus_ViewModel demodel)
        {
            string where = $@" 1=1";
            
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_Teaching_Bonus.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and {nameof(RB_Teaching_Bonus.School_Id)} ={demodel.School_Id}";
            }
            if (!string.IsNullOrEmpty(demodel.Month))
            {
                where += $@" and {nameof(RB_Teaching_Bonus.Month)} ='{demodel.Month}'";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and FIND_IN_SET('{demodel.TeacherId}',{nameof(RB_Teaching_Bonus.TeacherIds)})";
            }
            if (demodel.State > 0)
            {
                where += $@" and {nameof(RB_Teaching_Bonus.State)} ={(int)demodel.State}";
            }

            string sql = $@" select * from RB_Teaching_Bonus where {where} order by Id desc";
            return Get<RB_Teaching_Bonus_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_Bonus_ViewModel> GetPageList(int pageIndex,int pageSize,out long count,RB_Teaching_Bonus_ViewModel demodel)
        {
            string where = $@" 1=1";

            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_Teaching_Bonus.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.School_Id > 0)
            {
                where += $@" and {nameof(RB_Teaching_Bonus.School_Id)} ={demodel.School_Id}";
            }
            if (!string.IsNullOrEmpty(demodel.Month))
            {
                where += $@" and {nameof(RB_Teaching_Bonus.Month)} ='{demodel.Month}'";
            }
            if (demodel.TeacherId > 0)
            {
                where += $@" and FIND_IN_SET('{demodel.TeacherId}',{nameof(RB_Teaching_Bonus.TeacherIds)})";
            }
            if (demodel.State > 0)
            {
                where += $@" and {nameof(RB_Teaching_Bonus.State)} ={(int)demodel.State}";
            }

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

        /// <summary>
        /// 获取教师最近几月的走势图
        /// </summary>
        /// <param name="id"></param>
        /// <param name="group_Id"></param>
        /// <param name="monthStr"></param>
        /// <returns></returns>
        public List<RB_Teaching_Bonus_ViewModel> GetTeachingBonusLastlyChart(int id, int group_Id, string monthStr)
        {
            string sql = $@"select b.`Month`,SUM(bd.Money) AS Money
from RB_Teaching_BonusDetail bd
INNER JOIN rb_teaching_bonus b on bd.BonusId = b.Id
where bd.`Status`=0 AND bd.Group_Id ={group_Id} AND b.State=2 AND bd.TeacherId ={id} AND b.`Month` in ({monthStr})
GROUP BY b.`Month` ORDER BY b.`Month` DESC";

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