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_PlanDetailsRepository:BaseRepository<RB_Duty_PlanDetails>
    {
        /// <summary>
        /// 获取值班班次详情列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_PlanDetails_ViewModel> GetDutyPlanDetailsListRepository(RB_Duty_PlanDetails_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.Name,'') AS ShiftName,IFNULL(B.StartTime,'') AS ConfigStartTime,IFNULL(B.EndTime,'') AS ConfigEndTime
       ,C.Date AS DutyDate
FROM RB_Duty_PlanDetails AS A LEFT JOIN rb_duty_frequency AS B ON A.Shift=B.Id
     LEFT JOIN rb_duty_plan AS C ON A.PlanId=C.Id
WHERE 1=1
");
            if (query == null)
            {
                return new List<RB_Duty_PlanDetails_ViewModel>();
            }
            else
            {
                if (query.PlanId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_PlanDetails_ViewModel.PlanId), query.PlanId);
                }
                if (!string.IsNullOrEmpty(query.QPlanIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Duty_PlanDetails_ViewModel.PlanId), query.QPlanIds);
                }
                if (query.Shift > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_PlanDetails_ViewModel.Shift), query.Shift);
                }
                if (!string.IsNullOrEmpty(query.QDutyMan))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Duty_PlanDetails_ViewModel.DutyMan), query.QDutyMan);
                }
                if (query.QDutyStatus > -1)
                {
                    builder.AppendFormat(" AND A.Status={0} ", query.QDutyStatus);
                }
                //迟到
                if (query.QAttendanceStatus == 1)
                {
                    builder.AppendFormat(" AND A.CheckLateTime>0 ");
                }
                //早退
                if (query.QAttendanceStatus == 2)
                {
                    builder.AppendFormat(" AND A.FinishLateTime>0 ");
                }
                //缺勤
                if (query.QAttendanceStatus == 3)
                {
                    builder.AppendFormat(" AND A.Status=0 AND A.FinishLateTime=0 AND A.CheckLateTime=0 ");
                }
                //正常
                if (query.QAttendanceStatus == 4)
                {
                    builder.AppendFormat(" AND A.Status=2 AND A.FinishLateTime=0 AND A.CheckLateTime=0 ");
                }
                if (!string.IsNullOrEmpty(query.QStartDate))
                {
                    builder.AppendFormat(@" AND C.Date>'{0}' ", query.QStartDate);
                }
            }
            builder.AppendFormat(" ORDER BY B.{0} ASC ", nameof(RB_Duty_Frequency_ViewModel.StartTime));
            return Get<RB_Duty_PlanDetails_ViewModel>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 根据计划编号删除班次
        /// </summary>
        /// <param name="PlanId"></param>
        /// <returns></returns>
        public bool DeletePlanDetailsRepository(int PlanId)
        {
            bool flag = base.DeleteOne(new VT.FW.DB.WhereHelper(nameof(RB_Duty_PlanDetails_ViewModel.PlanId), PlanId));
            return flag;
        }
    }
}
