using Edu.Common.Enum;
using Edu.Model.Entity.Duty;
using Edu.Model.ViewModel.Duty;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Repository.Duty
{
    /// <summary>
    /// 值班计划仓储层
    /// </summary>
    public class RB_Duty_PlanRepository : BaseRepository<RB_Duty_Plan>
    {
        /// <summary>
        /// 获取值班计划分页列表【管理端】
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Plan_ViewModel> GetDutyPlanPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Duty_Plan_ViewModel query)
        {
            rowsCount = 0;
            string joinTable = "";
            if (query != null && (!string.IsNullOrEmpty(query.QDutyMan) || query.QDutyStatus > -1 || query.QAttendanceStatus > 0))
            {
                string joinWhere = "";
                if (!string.IsNullOrEmpty(query.QDutyMan))
                {
                    joinWhere += string.Format(" AND DutyMan IN({0}) ", query.QDutyMan);
                }
                if (query.QDutyStatus > -1)
                {
                    joinWhere += string.Format(" AND Status={0} ", query.QDutyStatus);
                }

                //迟到
                if (query.QAttendanceStatus == 1)
                {
                    joinWhere += string.Format(" AND CheckLateTime>0 ");
                }
                //早退
                if (query.QAttendanceStatus == 2)
                {
                    joinWhere += string.Format(" AND FinishLateTime>0 ");
                }
                //缺勤
                if (query.QAttendanceStatus == 3)
                {
                    joinWhere += string.Format(" AND Status=0 AND FinishLateTime=0 AND CheckLateTime=0 ");
                }
                //正常
                if (query.QAttendanceStatus == 4)
                {
                    joinWhere += string.Format(" AND Status=2 AND FinishLateTime=0 AND CheckLateTime=0 ");
                }
                joinTable = string.Format(" INNER JOIN (SELECT PlanId FROM rb_duty_plandetails WHERE 1=1 {0} GROUP BY PlanId ) AS C  ON A.Id=C.PlanId ", joinWhere);
            }

            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* ,IFNULL(B.SName,'') AS SchoolName
FROM RB_Duty_Plan AS A LEFT JOIN rb_school AS B ON A.School_Id=B.SId 
    {0}
WHERE 1=1 
", joinTable);
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Plan_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_Duty_Plan_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Plan_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Plan_ViewModel.School_Id), query.School_Id);
                }
                if (query.SchoolIdList != null && query.SchoolIdList.Count > 0)
                {
                    string str = "";
                    if (query.SchoolIdList != null && query.SchoolIdList.Count > 0)
                    {
                        for (var i = 0; i < query.SchoolIdList.Count; i++)
                        {
                            if (i == 0)
                            {
                                str += string.Format(" FIND_IN_SET('{0}', A.School_Id)>0 ", query.SchoolIdList[i]);
                            }
                            else
                            {
                                str += string.Format(" OR FIND_IN_SET('{0}', A.School_Id)>0 ", query.SchoolIdList[i]);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(str))
                    {
                        builder.AppendFormat("  AND ({0}) ", str);
                    }
                }
                if (!string.IsNullOrEmpty(query.QStartDate))
                {
                    builder.AppendFormat(@" AND A.{0}>='{1}' ", nameof(RB_Duty_Plan_ViewModel.Date), query.QStartDate);
                }
                if (!string.IsNullOrEmpty(query.QEndDate))
                {
                    builder.AppendFormat(@" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Duty_Plan_ViewModel.Date), query.QEndDate);
                }
                //查询已完成的值班
                if (query.IsFinish == 1)
                {
                    builder.AppendFormat(@" AND A.{0}<'{1}' ", nameof(RB_Duty_Plan_ViewModel.Date), Common.ConvertHelper.FormatDate(DateTime.Now));
                }
            }
            if (query.IsFinish == 1)
            {
                builder.AppendFormat(@" ORDER BY A.{0} DESC ", nameof(RB_Duty_Plan_ViewModel.Date));
            }
            else
            {
                builder.AppendFormat(@" ORDER BY A.{0} ASC ", nameof(RB_Duty_Plan_ViewModel.Date));
            }
            return GetPage<RB_Duty_Plan_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_Duty_Plan_ViewModel> GetMyDutyPlanPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Duty_Plan_ViewModel query)
        {
            rowsCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.ID,A.Date,IFNULL(C.SName,'') AS SchoolName,A.CreateBy,A.School_Id,B.Id AS DetailId
FROM RB_Duty_Plan AS A INNER JOIN rb_duty_plandetails AS B ON A.Id=B.PlanId
     INNER JOIN rb_school AS C ON A.School_Id=C.SId
     LEFT JOIN rb_duty_frequency AS D ON B.Shift=D.Id
WHERE 1=1 AND A.`Status`=0 
");
            if (query == null)
            {
                return new List<RB_Duty_Plan_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Plan_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.QDutyMan))
                {
                    builder.AppendFormat(@" AND B.{0}={1} ", nameof(RB_Duty_PlanDetails_ViewModel.DutyMan), query.QDutyMan);
                }
                if (query.QShift > 0)
                {
                    builder.AppendFormat(@" AND B.{0}={1} ", nameof(RB_Duty_PlanDetails_ViewModel.Shift), query.QShift);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Plan_ViewModel.Id), query.Id);
                }
                if (!string.IsNullOrEmpty(query.QStartDate))
                {
                    builder.AppendFormat(@" AND A.{0}>='{1}' ", nameof(RB_Duty_Plan_ViewModel.Date), query.QStartDate);
                }
                if (!string.IsNullOrEmpty(query.QEndDate))
                {
                    builder.AppendFormat(@" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Duty_Plan_ViewModel.Date), query.QEndDate);
                }
            }
            builder.AppendFormat(" GROUP BY A.Id,A.Date,C.SName,A.CreateBy,A.School_Id,B.Id ");
            builder.AppendFormat(" ORDER BY A.Date ASC,D.StartTime ASC ");
            return GetPage<RB_Duty_Plan_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }

        /// <summary>
        /// 检查值班计划是否存在
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool CheckExistsDutyPlanRepository(RB_Duty_Plan_ViewModel query)
        {
            int result = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append(@" SELECT COUNT(1) FROM RB_Duty_Plan AS A WHERE 1=1 AND A.Status=0 ");

            if (query.Group_Id > 0)
            {
                builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Plan_ViewModel.Group_Id), query.Group_Id);
            }
            if (query.School_Id >= 0)
            {
                builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Plan_ViewModel.School_Id), query.School_Id);
            }
            if (query.Date != null)
            {
                builder.AppendFormat(@" AND A.{0}='{1}' ", nameof(RB_Duty_Plan_ViewModel.Date), query.Date);
            }
            if (query.Id > 0)
            {
                builder.AppendFormat(@" AND A.{0}<>{1} ", nameof(RB_Duty_Plan_ViewModel.Id), query.Id);
            }
            var obj = base.ExecuteScalar(builder.ToString());
            if (obj != null)
            {
                Int32.TryParse(obj.ToString(), out result);
            }
            return result > 0;
        }
    }
}
