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_ItemRepository : BaseRepository<RB_Duty_Item>
    {
        /// <summary>
        /// 获取值班事项设置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Item_ViewModel> GetDutyItemListExtRepository(RB_Duty_Item_ViewModel query)
        {

            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,(SELECT GROUP_CONCAT(SName)  FROM rb_school WHERE `Status`=0 AND FIND_IN_SET(SId,a.ItemSchools) GROUP BY `Status`=0) AS SchoolName
          ,(SELECT GROUP_CONCAT(Name)   FROM rb_duty_frequency WHERE `Status`=0 AND FIND_IN_SET(Id,a.Shifts) GROUP BY `Status`=0) AS ShiftsName
FROM RB_Duty_Item AS A  
WHERE 1=1 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Item_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Item_ViewModel.Id), query.Id);
                }
                if (query.Status >= 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Item_ViewModel.Status), (int)query.Status);
                }
            }
            return Get<RB_Duty_Item_ViewModel>(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_Item_ViewModel> GetDutyItemPageRepository(int pageIndex,int pageSize,out long rowsCount, RB_Duty_Item_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,(SELECT GROUP_CONCAT(SName)  FROM rb_school WHERE `Status`=0 AND FIND_IN_SET(SId,a.ItemSchools) GROUP BY `Status`=0) AS SchoolName
          ,(SELECT GROUP_CONCAT(Name)   FROM rb_duty_frequency WHERE `Status`=0 AND FIND_IN_SET(Id,a.Shifts) GROUP BY `Status`=0) AS ShiftsName
FROM RB_Duty_Item AS A  
WHERE 1=1 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Item_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Item_ViewModel.Id), query.Id);
                }
                if (query.Status >= 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Item_ViewModel.Status), (int)query.Status);
                }
                if (!string.IsNullOrEmpty(query.ItemName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @ItemName ", nameof(RB_Duty_Item_ViewModel.ItemName));
                    parameters.Add("ItemName", "%" + query.ItemName.Trim() + "%");
                }
                if (query.DutyCategory > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Item_ViewModel.DutyCategory), query.DutyCategory);
                }
                if (query.ItemSchoolList != null && query.ItemSchoolList.Count > 0)
                {
                    string str = "";
                    if (query.ItemSchoolList != null && query.ItemSchoolList.Count > 0)
                    {
                        for (var i = 0; i < query.ItemSchoolList.Count; i++)
                        {
                            if (i == 0)
                            {
                                str += string.Format(" FIND_IN_SET('{0}', A.ItemSchools)>0 ", query.ItemSchoolList[i]);
                            }
                            else
                            {
                                str += string.Format(" OR FIND_IN_SET('{0}', A.ItemSchools)>0 ", query.ItemSchoolList[i]);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(str))
                    {
                        builder.AppendFormat("  AND ({0}) ", str);
                    }
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Duty_Item_ViewModel.Id));
            return GetPage<RB_Duty_Item_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取班次工作清单
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Item_ViewModel> GetDutyItemListRepository(RB_Duty_Item_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* 
FROM RB_Duty_Item AS A  
WHERE 1=1 ");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Item_ViewModel.Status), 0);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Item_ViewModel.Group_Id), query.Group_Id);
                }
                //班次
                if (!string.IsNullOrEmpty(query.Shifts))
                {
                    List<int> shiftList = Common.ConvertHelper.StringToList(query.Shifts);
                    string str = "";
                    if (shiftList != null && shiftList.Count > 0)
                    {
                        for (var i = 0; i < shiftList.Count; i++)
                        {
                            if (i == 0)
                            {
                                str += string.Format(" FIND_IN_SET('{0}', A.Shifts)>0 ", shiftList[i]);
                            }
                            else
                            {
                                str += string.Format(" OR FIND_IN_SET('{0}', A.Shifts)>0 ", shiftList[i]);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(str))
                    {
                        builder.AppendFormat("  AND ({0}) ", str);
                    }
                }
                //校区
                if (!string.IsNullOrEmpty(query.ItemSchools))
                {
                    List<int> schoolList = Common.ConvertHelper.StringToList(query.ItemSchools);
                    string str = "";
                    if (schoolList != null && schoolList.Count > 0)
                    {
                        for (var i = 0; i < schoolList.Count; i++)
                        {
                            if (i == 0)
                            {
                                str += string.Format(" FIND_IN_SET('{0}', A.ItemSchools)>0 ", schoolList[i]);
                            }
                            else
                            {
                                str += string.Format(" OR FIND_IN_SET('{0}', A.ItemSchools)>0 ", schoolList[i]);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(str))
                    {
                        builder.AppendFormat("  AND ({0}) ", str);
                    }
                }
            }
            return Get<RB_Duty_Item_ViewModel>(builder.ToString()).ToList();
        }
    }
}
