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

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)
        {
            Employee_ViewModel model = new Employee_ViewModel();
            var list = accountRepository.GetEmployeeListRepository(new Employee_ViewModel() { Id = Id, AccountId = AccountId, AccountType = (Common.Enum.User.AccountTypeEnum)AccountType });
            if (list != null && list.Any())
            {
                model = list.Where(x => (int)x.AccountType == AccountType)?.FirstOrDefault();
            }
            return model;
        }

        /// <summary>
        /// 新增修改员工信息
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public virtual bool SetEmployeeModule(Employee_ViewModel extModel)
        {
            if (extModel.LeaveStatus != Common.Enum.User.LeaveStatusEnum.Departure)
            {
                extModel.LeaveTime = null;
            }
            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,
                        Email=extModel.Email,
                        DirectSupervisor = extModel.DirectSupervisor,
                        ManagerAccount=extModel.Account,
                        Password=extModel.Password,
                    });
                    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,
                        Email=extModel.Email,
                        DirectSupervisor = extModel.DirectSupervisor,
                        TeacherAccount=extModel.Account,
                        Password=extModel.Password,
                    });
                    break;
                case Common.Enum.User.AccountTypeEnum.Assist:
                    flag = assistModule.SetAssistModule(new RB_Assist_ViewModel()
                    {
                        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,
                        Email=extModel.Email,
                        DirectSupervisor=extModel.DirectSupervisor,
                        AssistAccount=extModel.Account,
                        Password=extModel.Password,
                    });
                    break;
            }
            return flag;
        }

        /// <summary>
        /// 判断手机号码是否存在
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public virtual bool ExistPhone(Employee_ViewModel extModel)
        {
            bool flag = false;

            switch (extModel.AccountType)
            {
                case Common.Enum.User.AccountTypeEnum.Admin:
                    var list = managerModule.GetManagerListModule(new RB_Manager_ViewModel { Group_Id = extModel.Group_Id, MTel = extModel.Account });
                    if (extModel.AccountId == 0)
                    {
                        flag = list.Count() > 0;
                    }
                    else
                    {
                        flag = list.Where(x => x.MId != extModel.AccountId).Count() > 0;
                    }
                    break;
                case Common.Enum.User.AccountTypeEnum.Teacher:
                    var teacherList = teacherModule.GetTeacherListModule(new RB_Teacher_ViewModel { Group_Id = extModel.Group_Id, TeacherTel = extModel.Account });
                    if (extModel.AccountId == 0)
                    {
                        flag = teacherList.Count() > 0;
                    }
                    else
                    {
                        flag = teacherList.Where(x => x.TId != extModel.AccountId).Count() > 0;
                    }
                    break;
                case Common.Enum.User.AccountTypeEnum.Assist:
                    var assistList = assistModule.GetAssistListModule(new RB_Assist_ViewModel { Group_Id = extModel.Group_Id, AssistTel = extModel.Account });
                    if (extModel.AccountId == 0)
                    {
                        flag = assistList.Count() > 0;
                    }
                    else
                    {
                        flag = assistList.Where(x => x.AId != extModel.AccountId).Count() > 0;
                    }
                    break;
            }
            return flag;
        }


        /// <summary>
        /// 新增修改员工部门信息
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public virtual bool SetEmployeeDeptModule(Employee_ViewModel extModel)
        {
            bool flag = false;
            switch (extModel.AccountType)
            {
                case Common.Enum.User.AccountTypeEnum.Admin:
                    flag = managerModule.SetManagerDeptModule(new RB_Manager_ViewModel()
                    {
                        MId = extModel.AccountId,
                        CreateBy = extModel.CreateBy,
                        Group_Id = extModel.Group_Id,
                        School_Id = extModel.School_Id,
                        UpdateBy = extModel.UpdateBy,
                        UpdateTime = extModel.UpdateTime,
                        Dept_Id = extModel.Dept_Id
                    });
                    break;
                case Common.Enum.User.AccountTypeEnum.Teacher:
                    flag = teacherModule.SetTeacherDeptModule(new RB_Teacher_ViewModel()
                    {
                        TId = extModel.AccountId,
                        CreateBy = extModel.CreateBy,
                        Group_Id = extModel.Group_Id,
                        School_Id = extModel.School_Id,
                        UpdateBy = extModel.UpdateBy,
                        UpdateTime = extModel.UpdateTime,
                        Dept_Id = extModel.Dept_Id
                    });
                    break;
                case Common.Enum.User.AccountTypeEnum.Assist:
                    flag = assistModule.SetAssistDeptModule(new Model.Entity.User.RB_Assist()
                    {
                        AId = extModel.AccountId,
                        CreateBy = extModel.CreateBy,
                        Group_Id = extModel.Group_Id,
                        School_Id = extModel.School_Id,
                        UpdateBy = extModel.UpdateBy,
                        UpdateTime = extModel.UpdateTime,
                        Dept_Id = extModel.Dept_Id
                    });
                    break;
            }
            return flag;
        }

        /// <summary>
        /// 新增修改员工岗位信息
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public virtual bool SetEmployeePostModule(Employee_ViewModel extModel)
        {
            bool flag = false;
            switch (extModel.AccountType)
            {
                case Common.Enum.User.AccountTypeEnum.Admin:
                    flag = managerModule.SetManagerPostModule(new RB_Manager_ViewModel()
                    {
                        MId = extModel.AccountId,
                        CreateBy = extModel.CreateBy,
                        Group_Id = extModel.Group_Id,
                        School_Id = extModel.School_Id,
                        UpdateBy = extModel.UpdateBy,
                        UpdateTime = extModel.UpdateTime,

                        Post_Id = extModel.Post_Id,

                    });
                    break;
                case Common.Enum.User.AccountTypeEnum.Teacher:
                    flag = teacherModule.SetTeacherPostModule(new RB_Teacher_ViewModel()
                    {
                        TId = extModel.AccountId,
                        CreateBy = extModel.CreateBy,
                        Group_Id = extModel.Group_Id,
                        School_Id = extModel.School_Id,
                        UpdateBy = extModel.UpdateBy,
                        UpdateTime = extModel.UpdateTime,
                        Post_Id = extModel.Post_Id,

                    });
                    break;
                case Common.Enum.User.AccountTypeEnum.Assist:
                    flag = assistModule.SetAssistPostModule(new Model.Entity.User.RB_Assist()
                    {
                        AId = extModel.AccountId,
                        CreateBy = extModel.CreateBy,
                        Group_Id = extModel.Group_Id,
                        School_Id = extModel.School_Id,
                        UpdateBy = extModel.UpdateBy,
                        UpdateTime = extModel.UpdateTime,
                        Post_Id = extModel.Post_Id,

                    });
                    break;
            }
            return flag;
        }


        /// <summary>
        /// 新增修改员工离职信息
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public virtual bool SetEmployeeLeaveTimeModule(List<Employee_ViewModel> List)
        {
            bool flag = false;
            foreach (var extModel in List)
            {
                switch (extModel.AccountType)
                {
                    case Common.Enum.User.AccountTypeEnum.Admin:
                        flag = managerModule.SetManagerLeaveTimeModule(new RB_Manager_ViewModel()
                        {
                            MId = extModel.AccountId,
                            CreateBy = extModel.CreateBy,
                            Group_Id = extModel.Group_Id,
                            School_Id = extModel.School_Id,
                            UpdateBy = extModel.UpdateBy,
                            UpdateTime = extModel.UpdateTime,
                            LeaveStatus = extModel.LeaveStatus,
                            LeaveTime = extModel.LeaveTime,
                        });
                        break;
                    case Common.Enum.User.AccountTypeEnum.Teacher:
                        flag = teacherModule.SetTeacherLeaveTimeModule(new RB_Teacher_ViewModel()
                        {
                            TId = extModel.AccountId,
                            CreateBy = extModel.CreateBy,
                            Group_Id = extModel.Group_Id,
                            School_Id = extModel.School_Id,
                            UpdateBy = extModel.UpdateBy,
                            UpdateTime = extModel.UpdateTime,
                            LeaveStatus = extModel.LeaveStatus,
                            LeaveTime = extModel.LeaveTime
                        });
                        break;
                    case Common.Enum.User.AccountTypeEnum.Assist:
                        flag = assistModule.SetAssistLeaveTimeModule(new Model.Entity.User.RB_Assist()
                        {
                            AId = extModel.AccountId,
                            CreateBy = extModel.CreateBy,
                            Group_Id = extModel.Group_Id,
                            School_Id = extModel.School_Id,
                            UpdateBy = extModel.UpdateBy,
                            UpdateTime = extModel.UpdateTime,
                            LeaveStatus = extModel.LeaveStatus,
                            LeaveTime = extModel.LeaveTime
                        });
                        break;
                }
            }

            return flag;
        }

        /// <summary>
        /// 设置用户OKR直属上级
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="directSupervisor"></param>
        /// <returns></returns>
        public bool SetEmployeeDirectSupervisor(int userId, int directSupervisor)
        {
            Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                { nameof(RB_Account_ViewModel.DirectSupervisor),directSupervisor}
            };
            List<WhereHelper> wheres = new List<WhereHelper>() {
                new WhereHelper(){
                     FiledName=nameof(RB_Account_ViewModel.Id),
                      FiledValue=userId,
                       OperatorEnum=OperatorEnum.Equal
                }
            };
            bool flag = accountRepository.Update(keyValues, wheres);
            return flag;
        }
    }
}