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_FrequencyRepository : BaseRepository<RB_Duty_Frequency>
    {
        /// <summary>
        /// 获取班次设置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Frequency_ViewModel> GetDutyFrequencyListRepository(RB_Duty_Frequency_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.School_Ids) GROUP BY `Status`=0) AS SchoolName 
FROM RB_Duty_Frequency AS A   
WHERE 1=1 AND A.Status=0 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Frequency_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Frequency_ViewModel.Id), query.Id);
                }
                if (query.Status >= 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Frequency_ViewModel.Status), (int)query.Status);
                }
                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_Ids)>0 ", query.SchoolIdList[i]);
                            }
                            else
                            {
                                str += string.Format(" OR FIND_IN_SET('{0}', A.School_Ids)>0 ", query.SchoolIdList[i]);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(str))
                    {
                        builder.AppendFormat("  AND ({0}) ", str);
                    }
                }
            }
            return Get<RB_Duty_Frequency_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_Frequency_ViewModel> GetDutyFrequencyPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Duty_Frequency_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.School_Ids) GROUP BY `Status`=0) AS SchoolName 
FROM RB_Duty_Frequency AS A   
WHERE 1=1 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Frequency_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Frequency_ViewModel.Id), query.Id);
                }
                if (query.Status >= 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Duty_Frequency_ViewModel.Status), (int)query.Status);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_Duty_Frequency_ViewModel.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
                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_Ids)>0 ", query.SchoolIdList[i]);
                            }
                            else
                            {
                                str += string.Format(" OR FIND_IN_SET('{0}', A.School_Ids)>0 ", query.SchoolIdList[i]);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(str))
                    {
                        builder.AppendFormat("  AND ({0}) ", str);
                    }
                }
            }
            return GetPage<RB_Duty_Frequency_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取班次设置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Frequency_ViewModel> GetDutyFrequencyBySchoolIdsRepository(int Group_Id, string SchoolIds)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" SELECT * FROM rb_duty_frequency WHERE `Status`=0");
            if (Group_Id > 0)
            {
                builder.AppendFormat(" AND {0}={1} ", nameof(RB_Duty_Frequency_ViewModel.Group_Id), Group_Id);
            }
            if (!string.IsNullOrWhiteSpace(SchoolIds))
            {
                foreach (var item in SchoolIds.Split(","))
                {
                    builder.AppendFormat($" and  FIND_IN_SET({item},School_Ids) ");
                }
            }
            return Get<RB_Duty_Frequency_ViewModel>(builder.ToString()).ToList();
        }
    }
}
