using System;
using System.Collections.Generic;
using System.Linq;
using Edu.Model.ViewModel.Duty;
using Edu.Model.ViewModel.User;
using Edu.Repository.Duty;
using VT.FW.DB;

namespace Edu.Module.Duty
{
    /// <summary>
    /// 值班配置处理类
    /// </summary>
    public class DutyModule
    {
        /// <summary>
        /// 值班设置配置仓储对象
        /// </summary>
        private readonly RB_Duty_ConfigRepository dutyConfigRepository = new RB_Duty_ConfigRepository();

        /// <summary>
        /// 班次仓储层对象
        /// </summary>
        private readonly RB_Duty_FrequencyRepository dutyFrequencyRepository = new RB_Duty_FrequencyRepository();

        /// <summary>
        /// 值班事项仓储层对象
        /// </summary>
        private readonly RB_Duty_ItemRepository dutyItemRepository = new RB_Duty_ItemRepository();

        /// <summary>
        /// 账号管理处理类对象
        /// </summary>
        private readonly Repository.User.RB_AccountRepository accountRepository = new Repository.User.RB_AccountRepository();

        #region 值班事项管理
        /// <summary>
        /// 获取值班事项设置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Item_ViewModel> GetDutyItemListExtModule(RB_Duty_Item_ViewModel query)
        {
            return dutyItemRepository.GetDutyItemListExtRepository(query);
        }

        /// <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> GetDutyItemPageModule(int pageIndex, int pageSize, out long rowsCount, RB_Duty_Item_ViewModel query)
        {
            return dutyItemRepository.GetDutyItemPageRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 删除值班事项
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public bool RemoveItemModule(object Id)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Duty_Item_ViewModel.Status),1},
            };
            return dutyItemRepository.Update(fileds, new WhereHelper(nameof(RB_Duty_Item_ViewModel.Id), Id));
        }

        /// <summary>
        /// 新增/修改值班事项信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetDutyItemModule(RB_Duty_Item_ViewModel model)
        {
            if (model.DutyCategory == 1)
            { 

            }
            else if (model.DutyCategory == 2)
            {
                model.DutyType = 1;
                model.ItemType = 0;
            }
            else if (model.DutyCategory == 3)
            {
                model.DutyType = 1;
                model.ItemType = 0;
            }
            if (model.Id == 0)
            {
                return dutyItemRepository.Insert(model) > 0;
            }
            else
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Duty_Item_ViewModel.ItemName),model.ItemName},
                    { nameof(RB_Duty_Item_ViewModel.ItemType),model.ItemType},
                    { nameof(RB_Duty_Item_ViewModel.Shifts),model.Shifts},
                    { nameof(RB_Duty_Item_ViewModel.ItemSchools),model.ItemSchools},
                    { nameof(RB_Duty_Item_ViewModel.UpdateBy),model.UpdateBy},
                    { nameof(RB_Duty_Item_ViewModel.UpdateTime),model.UpdateTime},
                    { nameof(RB_Duty_Item_ViewModel.ItemImg),model.ItemImg},
                    { nameof(RB_Duty_Item_ViewModel.DutyCategory),model.DutyCategory},
                    { nameof(RB_Duty_Item_ViewModel.DutyType),model.DutyType},
                };
                return dutyItemRepository.Update(fileds, new WhereHelper(nameof(RB_Duty_Item_ViewModel.Id), model.Id));
            }
        }

        /// <summary>
        /// 根据编号获取值班事项信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_Duty_Item_ViewModel GetDutyItemModule(int Id)
        {
            var model = dutyItemRepository.GetEntity<RB_Duty_Item_ViewModel>(Id);
            return model;
        }

        #endregion

        #region 值班设置

        /// <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)
        {
            return dutyConfigRepository.GetDutyConfigPageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 获取值班设置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Config_ViewModel> GetDutyConfigModule(RB_Duty_Config_ViewModel query)
        {
            var list = dutyConfigRepository.GetDutyConfigRepository(query);
            if (list != null && list.Any())
            {
                string uids = string.Join(",", list.Select(x => x.EmployeeIds));
                string sendIds = string.Join(",", list.Select(x => x.SendEmployeeIds));
                List<int> userIdList = new List<int>();
                userIdList.AddRange(Common.ConvertHelper.StringToList(uids));
                userIdList.AddRange(Common.ConvertHelper.StringToList(sendIds));
                var userList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel { Group_Id = query.Group_Id, QIds = string.Join(",", userIdList) });

                foreach (var item in list)
                {
                    string userNames = "";
                    string sendNames = "";
                    if (!string.IsNullOrEmpty(item.EmployeeIds))
                    {
                        foreach (var itemUser in item.EmployeeIds.Split(","))
                        {
                            userNames += userList.Where(x => x.Id.ToString() == itemUser).FirstOrDefault()?.EmployeeName + "|";
                        }
                    }
                    if (!string.IsNullOrEmpty(item.SendEmployeeIds))
                    {
                        foreach (var sUser in item.SendEmployeeIds.Split(","))
                        {
                            sendNames += userList.Where(x => x.Id.ToString() == sUser).FirstOrDefault()?.EmployeeName + "|";
                        }
                    }
                    item.EmployeeName = userNames.TrimEnd('|');
                    item.SendName = sendNames.TrimEnd('|');
                }
            }
            return list;
        }

        /// <summary>
        /// 获取值班设置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Config_ViewModel> GetDutyConfigListModule(RB_Duty_Config_ViewModel query)
        {
            return dutyConfigRepository.GetDutyConfigListRepository(query);
        }

        /// <summary>
        /// 删除值班设置
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public bool RemoveConfigModule(object Id)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Duty_Config_ViewModel.Status),1},
            };
            return dutyConfigRepository.Update(fileds, new WhereHelper(nameof(RB_Duty_Config_ViewModel.Id), Id));
        }

        /// <summary>
        /// 检查值班设置是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool CheckDutyConfigModule(RB_Duty_Config_ViewModel model)
        {
             return dutyConfigRepository.CheckExistsConfigListRepository(model);
        }

        /// <summary>
        /// 新增/修改班次信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetDutyConfigModule(RB_Duty_Config_ViewModel model)
        {
            if (model.Id == 0)
            {
                return dutyConfigRepository.Insert(model) > 0;
            }
            else
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Duty_Config_ViewModel.Shifts),model.Shifts},
                    { nameof(RB_Duty_Config_ViewModel.EmployeeIds),model.EmployeeIds},
                    { nameof(RB_Duty_Config_ViewModel.MachineCode),model.MachineCode},
                    { nameof(RB_Duty_Config_ViewModel.School_Id),model.School_Id},
                    { nameof(RB_Duty_Config_ViewModel.UpdateBy),model.UpdateBy},
                    { nameof(RB_Duty_Config_ViewModel.UpdateTime),model.UpdateTime},
                    { nameof(RB_Duty_Config_ViewModel.SendEmployeeIds),model.SendEmployeeIds},
                };
                return dutyConfigRepository.Update(fileds, new WhereHelper(nameof(RB_Duty_Config_ViewModel.Id), model.Id));
            }
        }
        #endregion

        #region 班次设置
        /// <summary>
        /// 获取班次设置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Frequency_ViewModel> GetDutyFrequencyListModule(RB_Duty_Frequency_ViewModel query)
        {
            return dutyFrequencyRepository.GetDutyFrequencyListRepository(query);
        }

        /// <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> GetDutyFrequencyPageModule(int pageIndex, int pageSize, out long rowsCount, RB_Duty_Frequency_ViewModel query)
        {
            return dutyFrequencyRepository.GetDutyFrequencyPageRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 获取班次设置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Frequency_ViewModel> GetDutyFrequencyBySchoolIdsModule(int Group_Id, string SchoolIds)
        {
            return dutyFrequencyRepository.GetDutyFrequencyBySchoolIdsRepository(Group_Id, SchoolIds);
        }

        /// <summary>
        /// 删除班次
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public bool RemoveFrequencyModule(object Id)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Duty_Frequency_ViewModel.Status),1},
            };
            return dutyFrequencyRepository.Update(fileds, new WhereHelper(nameof(RB_Duty_Frequency_ViewModel.Id), Id));
        }

        /// <summary>
        /// 新增/修改班次信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetFrequencyModule(RB_Duty_Frequency_ViewModel model)
        {
            if (model.Id == 0)
            {
                return dutyFrequencyRepository.Insert(model) > 0;
            }
            else
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Duty_Frequency_ViewModel.Name),model.Name},
                    { nameof(RB_Duty_Frequency_ViewModel.StartTime),model.StartTime},
                    { nameof(RB_Duty_Frequency_ViewModel.EndTime),model.EndTime},
                    { nameof(RB_Duty_Frequency_ViewModel.School_Ids),model.School_Ids},
                    { nameof(RB_Duty_Frequency_ViewModel.UpdateBy),model.UpdateBy},
                    { nameof(RB_Duty_Frequency_ViewModel.UpdateTime),model.UpdateTime},
                };
                return dutyFrequencyRepository.Update(fileds, new WhereHelper(nameof(RB_Duty_Frequency_ViewModel.Id), model.Id));
            }
        }
        #endregion
    }
}