using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edu.Model.Entity.Duty;
using Edu.Model.ViewModel.Duty;
using VT.FW.DB.Dapper;

namespace Edu.Repository.Duty
{
    /// <summary>
    /// 值班设置配置仓储层
    /// </summary>
    public class RB_Duty_ConfigRepository : BaseRepository<RB_Duty_Config>
    {
        /// <summary>
        /// 获取值班设置分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Config_ViewModel> GetDutyConfigPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Duty_Config_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" SELECT A.*,B.SName AS SchoolName  FROM RB_Duty_Config AS A LEFT JOIN rb_school AS B ON A.School_Id=B.SId WHERE 1=1 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.School_Id), query.School_Id);
                }
                if (query.Status >= 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.Status), (int)query.Status);
                }
            }
            return GetPage<RB_Duty_Config_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取值班设置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Config_ViewModel> GetDutyConfigRepository(RB_Duty_Config_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" SELECT A.*,(SELECT GROUP_CONCAT(`Name`)  from rb_duty_frequency where `Status`=0 and FIND_IN_SET(Id,a.Shifts) GROUP BY `Status`=0) as ShiftsName,B.SName AS SchoolName  FROM RB_Duty_Config AS A LEFT JOIN rb_school AS B ON A.School_Id=B.SId WHERE 1=1 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.Id), query.Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.School_Id), query.School_Id);
                }
                if (query.Status >= 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.Status), (int)query.Status);
                }
            }
            return Get<RB_Duty_Config_ViewModel>(builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 获取值班设置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Config_ViewModel> GetDutyConfigListRepository(RB_Duty_Config_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" SELECT A.*  FROM RB_Duty_Config AS A  WHERE 1=1 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.Id), query.Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.School_Id), query.School_Id);
                }
                if (query.Status >= 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.Status), (int)query.Status);
                }
                if (!string.IsNullOrWhiteSpace(query.MachineCode))
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.MachineCode), query.MachineCode);
                }
            }
            return Get<RB_Duty_Config_ViewModel>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 判断设置是否存在
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool CheckExistsConfigListRepository(RB_Duty_Config_ViewModel query)
        {
            int result = 0;
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" SELECT COUNT(1) FROM RB_Duty_Config AS A WHERE 1=1 AND Status=0 ");
            if (query.Id > 0)
            {
                builder.AppendFormat(" AND A.{0}<>{1} ", nameof(RB_Duty_Config_ViewModel.Id), query.Id);
            }
            if (query.School_Id >= 0)
            {
                builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.School_Id), query.School_Id);
            }
            if (query.Group_Id > 0)
            {
                builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Config_ViewModel.Group_Id), query.Group_Id);
            }
            var obj = base.ExecuteScalar(builder.ToString());
            if (obj != null)
            {
                Int32.TryParse(obj.ToString(), out result);
            }
            return result > 0;
        }
    }
}
