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

namespace Edu.Module.User
{
    /// <summary>
    /// 学校校区处理类
    /// </summary>
    public class SchoolModule
    {
        /// <summary>
        /// 学校仓储层对象
        /// </summary>
        private readonly RB_SchoolRepository schoolRepository = new RB_SchoolRepository();

        /// <summary>
        /// 部门仓储层对象
        /// </summary>
        private readonly RB_DepartmentRepository departmentRepository = new RB_DepartmentRepository();

        /// <summary>
        /// 账号管理仓储层对象
        /// </summary>
        private readonly RB_AccountRepository accountRepository = new RB_AccountRepository();

        /// <summary>
        /// 获取学校列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_School_ViewModel> GetSchoolListModule(RB_School_ViewModel query)
        {
            return schoolRepository.GetSchoolListRepository(query); ;
        }

        /// <summary>
        /// 获取学校分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_School_ViewModel> GetSchoolPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_School_ViewModel query)
        {
            var list= schoolRepository.GetSchoolPageListRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count > 0)
            {
                List<RB_Department_ViewModel> deptList = new List<RB_Department_ViewModel>();
                List<Employee_ViewModel> empList = new List<Employee_ViewModel>();
                string deptIds = string.Join(",", list.Where(qitem => qitem.Dept_Id > 0).Select(qitem => qitem.Dept_Id));
                string empIds = string.Join(",", list.Where(qitem => qitem.ManagerId > 0).Select(qitem => qitem.ManagerId));
                if (!string.IsNullOrEmpty(deptIds))
                {
                    deptList = departmentRepository.GetDepartmentListRepository(new RB_Department_ViewModel()
                    {
                        QDeptIds = deptIds
                    });
                }
                if (!string.IsNullOrEmpty(empIds))
                {
                    empList= accountRepository.GetEmployeeListRepository(new Employee_ViewModel() { QIds = empIds });
                }
                foreach (var item in list)
                {
                    item.DeptName = deptList?.Where(qitem => qitem.DeptId == item.Dept_Id)?.FirstOrDefault()?.DeptName ?? "";
                    item.ManagerName = empList?.Where(qitem => qitem.Id == item.ManagerId)?.FirstOrDefault()?.EmployeeName ?? "";
                }
            }
            return list;
        }

        /// <summary>
        /// 获取学校实体类
        /// </summary>
        /// <param name="GId"></param>
        /// <returns></returns>
        public RB_School_ViewModel GetSchoolEntityModule(int SId)
        {
            return GetSchoolListModule(new RB_School_ViewModel() { SId = SId })?.FirstOrDefault() ?? new RB_School_ViewModel();
        }

        /// <summary>
        /// 新增修改学校
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual bool SetSchoolModule(RB_School_ViewModel model)
        {
            bool flag;
            if (model.SId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_School_ViewModel.SName),model.SName.Trim() },
                    {nameof(RB_School_ViewModel.SAddress),model.SAddress},
                    {nameof(RB_School_ViewModel.SLinkTel),model.SLinkTel.Trim() },
                    {nameof(RB_School_ViewModel.SDomain),model.SDomain },
                    {nameof(RB_School_ViewModel.SLogo),model.SLogo },
                    {nameof(RB_School_ViewModel.Remark),model.Remark },
                    {nameof(RB_School_ViewModel.UpdateBy),model.UpdateBy },
                    {nameof(RB_School_ViewModel.UpdateTime),model.UpdateTime },
                    {nameof(RB_School_ViewModel.Status),model.Status },
                    {nameof(RB_School_ViewModel.Dept_Id),model.Dept_Id },
                    {nameof(RB_School_ViewModel.ManagerId),model.ManagerId },
                    {nameof(RB_School_ViewModel.ContractTitle),model.ContractTitle },
                };
                flag = schoolRepository.Update(fileds, new WhereHelper(nameof(RB_School_ViewModel.SId), model.SId));
            }
            else
            {
                var newId = schoolRepository.Insert(model);
                model.SId = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 判断学校是否存在
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool ExistsSchoolModule(RB_School_ViewModel query)
        {
            List<WhereHelper> wheres = new List<WhereHelper>()
            {
                new WhereHelper(nameof(RB_School_ViewModel.SName),query.SName.Trim()),
                new WhereHelper(nameof(RB_School_ViewModel.Group_Id),query.Group_Id),
            };
            if (query.SId > 0)
            {
                wheres.Add(new WhereHelper(nameof(RB_School_ViewModel.SId), query.SId, OperatorEnum.NotEqual));
            }
            return schoolRepository.Exists(wheres);
        }

        /// <summary>
        /// 删除学校
        /// </summary>
        /// <param name="SId">学校编号</param>
        /// <returns></returns>
        public virtual bool RemoveSchoolModule(object SId)
        {
            bool flag;
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_School_ViewModel.Status),(int)DateStateEnum.Delete },
            };
            flag = schoolRepository.Update(fileds, new WhereHelper(nameof(RB_School_ViewModel.SId), SId));
            return flag;
        }
    }
}