using Edu.Model.ViewModel.User;
using Edu.Repository.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Module.User
{
    /// <summary>
    /// 员工处理类
    /// </summary>
    public class EmployeeModule
    {
        /// <summary>
        /// 账号管理处理类对象
        /// </summary>
        private readonly RB_AccountRepository accountRepository = new RB_AccountRepository();

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

        /// <summary>
        /// 管理者处理类对象
        /// </summary>
        private readonly ManagerModule managerModule = new ManagerModule();

        /// <summary>
        /// 教程处理类对象
        /// </summary>
        private readonly TeacherModule teacherModule = new TeacherModule();

        /// <summary>
        /// 助理处理类对象
        /// </summary>
        private readonly AssistModule assistModule = new AssistModule();

        /// <summary>
        /// 获取员工列表【管理者、讲师、助教】
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<Employee_ViewModel> GetEmployeeListModule(Employee_ViewModel query)
        {
            return accountRepository.GetEmployeeListRepository(query);
        }

        /// <summary>
        /// 获取员工分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<Employee_ViewModel> GetEmployeePageListModule(int pageIndex, int pageSize, out long rowsCount, Employee_ViewModel query)
        {
            return accountRepository.GetEmployeePageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 根据员工部门获取员工列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<Employee_ViewModel> GetEmployeeByDeptTierModule(Employee_ViewModel query)
        {
            List<Employee_ViewModel> employeeList = new List<Employee_ViewModel>();
            //获取当前部门所有的上级部门
            if (query.DeptTier == 1)
            {
                var currentDeptModel = departmentRepository.GetEntity<RB_Department_ViewModel>(query.Dept_Id);
                if (currentDeptModel != null && !string.IsNullOrEmpty(currentDeptModel.ManagerIds))
                {
                    employeeList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel()
                    {
                        QIds = currentDeptModel.ManagerIds
                    });
                }
            }
            else
            {
                var allDeptList = departmentRepository.GetAllSuperiorDepartmentListRepository(query.Dept_Id);
                var subList = allDeptList.Where(qitem => qitem.DeptTier == (query.DeptTier - 1));
                if (subList != null && subList.Count() > 0)
                {
                    string managerIds = "0";
                    foreach (var item in subList)
                    {
                        if (!string.IsNullOrEmpty(item.ManagerIds))
                        {
                            managerIds += "," + item.ManagerIds;
                        }
                    }
                    if (!string.IsNullOrEmpty(managerIds))
                    {
                        employeeList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel()
                        {
                            QIds = managerIds
                        });
                    }
                }
            }
            return employeeList;
        }

        /// <summary>
        /// 根据编号获取员工信息
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="AccountId">具体表主键编号</param>
        /// <param name="AccountType">类型</param>
        /// <returns></returns>
        public Employee_ViewModel GetEmployeeModule(int Id, int AccountId, int AccountType)
        {
            return accountRepository.GetEmployeeListRepository(new Employee_ViewModel() { Id = Id, AccountId = AccountId, AccountType = (Common.Enum.User.AccountTypeEnum)AccountType })?.FirstOrDefault();
        }

        /// <summary>
        /// 新增修改员工信息
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public virtual bool SetEmployeeModule(Employee_ViewModel extModel)
        {
            bool flag = false;
            switch (extModel.AccountType)
            {
                case Common.Enum.User.AccountTypeEnum.Admin:
                    flag = managerModule.SetManagerModule(new RB_Manager_ViewModel()
                    {
                        MId = extModel.AccountId,
                        MName = extModel.EmployeeName,
                        MTel = extModel.EmployeeTel,
                        MHead = extModel.UserIcon,
                        Group_Id = extModel.Group_Id,
                        School_Id = extModel.School_Id,
                        CreateBy = extModel.CreateBy,
                        CreateTime = extModel.CreateTime,
                        UpdateBy = extModel.UpdateBy,
                        UpdateTime = extModel.UpdateTime,
                        Status = extModel.Status,
                        Dept_Id = extModel.Dept_Id,
                        Post_Id = extModel.Post_Id,
                        IDCard = extModel.IDCard,
                        Sex = extModel.Sex,
                        EntryTime = extModel.EntryTime,
                        Address = extModel.Address,
                        BirthDate = extModel.BirthDate,
                        LeaveStatus = extModel.LeaveStatus,
                        LeaveTime = extModel.LeaveTime,
                        Education = extModel.Education
                    });
                    break;
                case Common.Enum.User.AccountTypeEnum.Teacher:
                    flag = teacherModule.SetTeacherModule(new RB_Teacher_ViewModel()
                    {
                        TId=extModel.AccountId,
                        School_Id=extModel.School_Id,
                        TeacherName=extModel.EmployeeName,
                        TeacherTel=extModel.EmployeeTel,
                        TeacherHead=extModel.UserIcon,
                        Status=extModel.Status,
                        CreateBy=extModel.CreateBy,
                        CreateTime=extModel.CreateTime,
                        UpdateBy=extModel.UpdateBy,
                        UpdateTime=extModel.UpdateTime,
                        Group_Id=extModel.Group_Id,
                        Dept_Id=extModel.Dept_Id,
                        Post_Id=extModel.Post_Id,
                        IDCard=extModel.IDCard,
                        Sex=extModel.Sex,
                        EntryTime=extModel.EntryTime,
                        Address = extModel.Address,
                        BirthDate=extModel.BirthDate,
                        LeaveStatus=extModel.LeaveStatus,
                        LeaveTime=extModel.LeaveTime,
                        Education=extModel.Education,
                    });
                    break;
                case Common.Enum.User.AccountTypeEnum.Assist:
                    flag = assistModule.SetAssistModule(new Model.Entity.User.RB_Assist()
                    {
                        AId = extModel.AccountId,
                        School_Id = extModel.School_Id,
                        AssistName = extModel.EmployeeName,
                        AssistTel = extModel.EmployeeTel,
                        AssistIcon = extModel.UserIcon,
                        Group_Id = extModel.Group_Id,
                        CreateBy = extModel.CreateBy,
                        CreateTime = extModel.CreateTime,
                        UpdateBy = extModel.UpdateBy,
                        UpdateTime = extModel.UpdateTime,
                        Dept_Id = extModel.Dept_Id,
                        Post_Id = extModel.Post_Id,
                        IDCard = extModel.IDCard,
                        Sex = extModel.Sex,
                        EntryTime = extModel.EntryTime,
                        Address = extModel.Address,
                        BirthDate = extModel.BirthDate,
                        LeaveStatus = extModel.LeaveStatus,
                        LeaveTime = extModel.LeaveTime,
                        Education = extModel.Education
                    });
                    break;
            }
            return flag;
        }
    }
}