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

namespace Edu.Repository.Grade
{
    public class RB_Class_LessonPlanRepository : BaseRepository<RB_Class_LessonPlan>
    {
        /// <summary>
        /// 获取老师教案分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_LessonPlan_ViewModel> GetLessonPlanPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Class_LessonPlan_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@$" SELECT *  FROM RB_Class_LessonPlan WHERE 1=1 AND {nameof(RB_Class_LessonPlan.Status)}=0  ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.School_Id), query.School_Id);
                }
                if (query.LessonPlanId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.LessonPlanId), query.LessonPlanId);
                }
                if (query.ClassId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.ClassId), query.ClassId);
                }
                if (query.ClassPlanId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.ClassPlanId), query.ClassPlanId);
                }
            }
            return GetPage<RB_Class_LessonPlan_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取老师教案分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_LessonPlan_ViewModel> GetLessonPlanListRepository(RB_Class_LessonPlan_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@$" SELECT *  FROM RB_Class_LessonPlan WHERE 1=1 AND {nameof(RB_Class_LessonPlan.Status)}=0  ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.School_Id), query.School_Id);
                }
                if (query.LessonPlanId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.LessonPlanId), query.LessonPlanId);
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.CreateBy), query.CreateBy);
                }
                if (query.ClassId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.ClassId), query.ClassId);
                }
                if (query.ClassPlanId > 0 && query.ClassDate.HasValue)
                {
                    builder.AppendFormat(" AND ({0}={1}   or DATE_FORMAT({2},'%y/%m/%d')= DATE_FORMAT('{3}','%y/%m/%d'))", nameof(RB_Class_LessonPlan.ClassPlanId), query.ClassPlanId, nameof(RB_Class_LessonPlan.ClassDate), query.ClassDate.Value);
                }
                else if (query.ClassPlanId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.ClassPlanId), query.ClassPlanId);
                }

                if (query.IsTemplate > -1)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Class_LessonPlan.IsTemplate), query.IsTemplate);
                }
            }
            return Get<RB_Class_LessonPlan_ViewModel>(builder.ToString()).ToList();
        }
    }
}
