using System;
using System.Collections.Generic;
using System.Linq;
using Edu.AOP;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum.User;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.User;
using Edu.Module.User;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Edu.WebApi.Controllers.User
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class UserController : BaseController
    {
        /// <summary>
        /// 助教处理类对象
        /// </summary>
        private readonly AssistModule assistModule = AOPHelper.CreateAOPObject<AssistModule>();

        /// <summary>
        /// 集团处理类对象
        /// </summary>
        private readonly GroupModule groupModule = AOPHelper.CreateAOPObject<GroupModule>();

        /// <summary>
        /// 学校处理类对象
        /// </summary>
        private readonly SchoolModule schoolModule = AOPHelper.CreateAOPObject<SchoolModule>();

        /// <summary>
        /// 学生处理类对象
        /// </summary>
        private readonly StudentModule studentModule = AOPHelper.CreateAOPObject<StudentModule>();

        /// <summary>
        /// 讲师处理类对象
        /// </summary>
        private readonly TeacherModule teacherModule = AOPHelper.CreateAOPObject<TeacherModule>();

        /// <summary>
        /// 系统管理人员处理类对象
        /// </summary>
        private readonly ManagerModule managerModule = AOPHelper.CreateAOPObject<ManagerModule>();

        /// <summary>
        /// 账户相关处理类对象
        /// </summary>
        private readonly AccountModule accountModule = AOPHelper.CreateAOPObject<AccountModule>();

        /// <summary>
        /// 岗位处理类对象
        /// </summary>
        private readonly PostModule postModule = AOPHelper.CreateAOPObject<PostModule>();

        /// <summary>
        /// 部门处理类对象
        /// </summary>
        private readonly DepartmentModule departmentModule = AOPHelper.CreateAOPObject<DepartmentModule>();

        /// <summary>
        /// 员工处理类对象
        /// </summary>
        private readonly EmployeeModule employeeModule = new EmployeeModule();


        #region 用户账户管理

        /// <summary>
        /// 重置用户密码信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult ResetUserPassword()
        {
            RB_Account_ViewModel viewModel = JsonConvert.DeserializeObject<RB_Account_ViewModel>(RequestParm.Msg.ToString());
            if (viewModel != null && viewModel.AccountType > 0 && viewModel.AccountId > 0)
            {
                if (int.TryParse(RequestParm.Uid, out int uid))
                {
                    viewModel.Group_Id = UserReidsCache.GetUserLoginInfo(uid).Group_Id;
                    var (reuslt, newPass) = accountModule.SetResetPassword(viewModel);
                    if (reuslt)
                    {
                        return ApiResult.Success($"密码重置成功，用户新密码为【{newPass}】,请复制保存");
                    }
                    else
                    {
                        return ApiResult.Failed(message: "密码重置失败");
                    }
                }
                else
                {
                    return ApiResult.Failed(message: "身份信息不合法，请尝试重新登录");
                }
            }
            else
            {
                return ApiResult.Failed(message: "参数不合法，请刷新页面重试");
            }
        }

        #endregion

        #region 集团管理

        /// <summary>
        /// 获取集团列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetGroupList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Group_ViewModel>(RequestParm.Msg.ToString());
            var list = groupModule.GetGroupListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取集团分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetGroupPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Group_ViewModel>(RequestParm.Msg.ToString());
            var list = groupModule.GetGroupPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 新增修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetGroup()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Group_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateBy = base.UserInfo.Id;
            extModel.CreateTime = DateTime.Now;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            bool flag = groupModule.SetGroupModule(extModel);
            return flag ? ApiResult.Success(data: extModel) : ApiResult.Failed();
        }

        /// <summary>
        /// 获取集团列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetGroup()
        {
            var GId = base.ParmJObj.GetInt("GId", 0);
            var extModel = groupModule.GetGroupEntityModule(GId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除集团
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveGroup()
        {
            var GId = base.ParmJObj.GetInt("GId", 0);
            var flag = groupModule.RemoveGroupModule(GId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        #endregion

        #region 学校管理

        /// <summary>
        /// 获取学校列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSchoolList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_School_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = schoolModule.GetSchoolListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取学校分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSchoolPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_School_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = schoolModule.GetSchoolPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 添加修改学校
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetSchool()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_School_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateBy = base.UserInfo.Id;
            extModel.CreateTime = DateTime.Now;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = base.UserInfo.Group_Id;
            bool flag = schoolModule.SetSchoolModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取学校实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSchool()
        {
            var SId = base.ParmJObj.GetInt("SId", 0);
            var extModel = schoolModule.GetSchoolEntityModule(SId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除学校
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveSchool()
        {
            var SId = base.ParmJObj.GetInt("SId", 0);
            var flag = schoolModule.RemoveSchoolModule(SId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 讲师管理

        /// <summary>
        /// 获取讲师分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetTeacherPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Teacher_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = teacherModule.GetTeacherPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取讲师列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetTeacherList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Teacher_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = teacherModule.GetTeacherListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改讲师
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetTeacher()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Teacher_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateBy = base.UserInfo.Id;
            extModel.CreateTime = DateTime.Now;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.AuditStatus = Common.Enum.User.AccountStatusEnum.Normal;
            extModel.Group_Id = base.UserInfo.Group_Id;
            bool flag = teacherModule.SetTeacherModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取讲师实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetTeacher()
        {
            var TId = base.ParmJObj.GetInt("TId", 0);
            var extModel = teacherModule.GetTeacherModule(TId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除讲师
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveTeacher()
        {
            var TId = base.ParmJObj.GetInt("TId", 0);
            var flag = teacherModule.RemoveTeacherModule(TId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 讲师审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult AuditTeacher()
        {
            var TId = base.ParmJObj.GetInt("TId", 0);
            int AuditStatus = base.ParmJObj.GetInt("AuditStatus", 1);
            string AuditRemark = base.ParmJObj.GetStringValue("AuditRemark");
            var flag = teacherModule.AuditTeacherModule(TId, AuditStatus, AuditRemark, base.UserInfo);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 讲师重新申请审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult ReApplyTeacher()
        {
            var TId = base.ParmJObj.GetInt("TId", 0);
            var flag = teacherModule.ReApplyTeacherModule(TId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 助教管理

        /// <summary>
        /// 获取助教分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetAssistPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Assist_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = assistModule.GetAssistPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取助教列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetAssistList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Assist_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = assistModule.GetAssistListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改助教
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetAssist()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Assist_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateBy = base.UserInfo.Id;
            extModel.CreateTime = DateTime.Now;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = base.UserInfo.Group_Id;
            extModel.AuditStatus = Common.Enum.User.AccountStatusEnum.Normal;
            bool flag = assistModule.SetAssistModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取助教实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetAssist()
        {
            var AId = base.ParmJObj.GetInt("AId", 0);
            var extModel = assistModule.GetAssistModule(AId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除讲师
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveAssist()
        {
            var AId = base.ParmJObj.GetInt("AId", 0);
            var flag = assistModule.RemoveAssistModule(AId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 助教重新申请
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult ReApplyAssist()
        {
            var AId = base.ParmJObj.GetInt("AId", 0);
            var flag = assistModule.ReApplyAssistModule(AId);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 助教审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult AuditAssist()
        {
            var AId = base.ParmJObj.GetInt("AId", 0);
            int AuditStatus = base.ParmJObj.GetInt("AuditStatus", 1);
            string AuditRemark = base.ParmJObj.GetStringValue("AuditRemark");
            var flag = assistModule.AuditAssistModule(AId, AuditStatus, AuditRemark, base.UserInfo);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 学生管理

        /// <summary>
        /// 获取学生分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudentPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Student_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            //query.School_Id = base.UserInfo.School_Id;
            var list = studentModule.GetStudentPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取学生列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudentList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Student_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = studentModule.GetStudentListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改学生
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetStudent()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Student_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateBy = base.UserInfo.Id;
            extModel.CreateTime = DateTime.Now;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = base.UserInfo.Group_Id;
            extModel.IsDisable = 1;
            bool flag = studentModule.SetStudentModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取学生实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudent()
        {
            var StuId = base.ParmJObj.GetInt("StuId", 0);
            var extModel = studentModule.GetStudentModule(StuId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 更新学生状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveStudent()
        {
            var StuId = base.ParmJObj.GetInt("StuId", 0);
            int Status = base.ParmJObj.GetInt("Status");
            var flag = studentModule.RemoveStudentModule(StuId, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 更新学生禁用或启用
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult UpdateStudentIsDisable()
        {
            var StuId = base.ParmJObj.GetInt("StuId", 0);
            int IsDisable = base.ParmJObj.GetInt("IsDisable", 0);
            var flag = studentModule.UpdateStudentIsDisableModule(StuId, IsDisable);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 生成学生账号
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult CreateStudentAccount()
        {
            var StuId = base.ParmJObj.GetInt("StuId", 0);
            var flag = studentModule.CreateStudentAccountModule(StuId, base.UserInfo);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        #endregion

        #region 管理者管理

        /// <summary>
        /// 获取系统管理者分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetManagerPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<Employee_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = employeeModule.GetEmployeePageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = rowsCount;
            pageModel.PageData = list.Select(qitem => new
            {
                qitem.Id,
                qitem.UserIcon,
                qitem.EmployeeName,
                qitem.SchoolName,
                qitem.DeptName,
                qitem.PostName,
                Sex = qitem.Sex == 0 ? "男" : "女",
                BirthDate = Common.ConvertHelper.FormatDate(qitem.BirthDate),
                Education = qitem.Education.ToName(),
                LeaveStatus = qitem.LeaveStatus.ToName(),
                LeaveStatusValue= qitem.LeaveStatus,
                qitem.Account,
                qitem.AccountType,
                qitem.IsBirth,
                qitem.AccountId,
            });
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取系统管理者列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetManagerList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Manager_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            query.School_Id = base.UserInfo.School_Id;
            var list = managerModule.GetManagerListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改管理者 SetManager
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetEmployee()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<Employee_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateBy = base.UserInfo.Id;
            extModel.CreateTime = DateTime.Now;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = base.UserInfo.Group_Id;
            extModel.School_Id = base.UserInfo.School_Id;
            bool flag = employeeModule.SetEmployeeModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取管理者实体
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetEmployee()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var AccountId = base.ParmJObj.GetInt("AccountId", 0);
            var AccountType = base.ParmJObj.GetInt("AccountType", 0);
            var extModel = employeeModule.GetEmployeeModule(Id, AccountId, AccountType);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 删除管理者
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveManager()
        {
            var MId = base.ParmJObj.GetInt("MId", 0);
            int Status = base.ParmJObj.GetInt("Status");
            var flag = managerModule.RemoveManagerModule(MId, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 创建管理者账号
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult CreateManagerAccount()
        {
            var MId = base.ParmJObj.GetInt("MId", 0);
            var flag = managerModule.CreateManagerAccountModule(MId, base.UserInfo);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 部门管理

        /// <summary>
        /// 获取部门分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetDeptPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Department_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = departmentModule.GetDepartmentPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetDeptList()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Department_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = departmentModule.GetDepartmentListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取当前部门和当前部门所有的下级部门列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetChildDepartment()
        {
            int DeptId = base.ParmJObj.GetInt("DeptId");
            if (DeptId <= 0)
            {
                return ApiResult.ParamIsNull(message: "请选择部门编号!");
            }
            var groupId = base.UserInfo.Group_Id;
            var list = departmentModule.GetCurrentAndChildDepartmentListModule(DeptId);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取部门树形列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetDeptTree()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Department_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = departmentModule.GetDepartmentTreeModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取组织机构图
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOrganizationChart()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Department_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var rootModel = groupModule.GetGroupEntityModule(query.Group_Id);
            var obj = departmentModule.GetOrganizationChartModule(rootModel, query);
            return ApiResult.Success(data: obj);
        }

        /// <summary>
        /// 添加修改部门
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetDept()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Department_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateBy = base.UserInfo.Id;
            extModel.CreateTime = DateTime.Now;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = base.UserInfo.Group_Id;
            extModel.Status = Common.Enum.DateStateEnum.Normal;
            bool flag = departmentModule.SetDepartmentModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取部门实体类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetDept()
        {
            var DeptId = base.ParmJObj.GetInt("DeptId", 0);
            var extModel = departmentModule.GetDepartmentModule(DeptId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 修改部门状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveDept()
        {
            var DeptId = base.ParmJObj.GetInt("DeptId", 0);
            int Status = base.ParmJObj.GetInt("Status");
            var flag = departmentModule.RemoveDepartmentModule(DeptId, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 岗位管理

        /// <summary>
        /// 获取岗位分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetPostPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Post_ViewModel>(RequestParm.Msg.ToString());
            query.Group_Id = base.UserInfo.Group_Id;
            var list = postModule.GetPostPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取岗位列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetPostList()
        {
            var query = new RB_Post_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                RB_Dept_Id=base.ParmJObj.GetInt("RB_Dept_Id"),
            };
            var list = postModule.GetPostListModule(query);
            return ApiResult.Success(data: list);
        }


        /// <summary>
        /// 添加修改岗位
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetPost()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Post_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateBy = base.UserInfo.Id;
            extModel.CreateTime = DateTime.Now;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = base.UserInfo.Group_Id;
            extModel.Status = Common.Enum.DateStateEnum.Normal;
            bool flag = postModule.SetPostModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取部门实体类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetPost()
        {
            var PostId = base.ParmJObj.GetInt("PostId", 0);
            var extModel = postModule.GetPostModule(PostId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 修改岗位状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemovePost()
        {
            var PostId = base.ParmJObj.GetInt("PostId", 0);
            int Status = base.ParmJObj.GetInt("Status");
            var flag = postModule.RemovePostModule(PostId, Status);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
        #endregion

        #region 员工查询

        /// <summary>
        /// 获取岗位列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetEmployeeList()
        {
            var query = new Employee_ViewModel()
            {
                School_Id = base.ParmJObj.GetInt("School_Id"),
                Account = base.ParmJObj.GetStringValue("Account"),
                AccountType = (AccountTypeEnum)base.ParmJObj.GetInt("AccountType"),
                Id = base.ParmJObj.GetInt("Id"),
                AccountId = base.ParmJObj.GetInt("AccountId"),
                Post_Id = base.ParmJObj.GetInt("Post_Id"),
                QPostIds = base.ParmJObj.GetStringValue("QPostIds"),
                Dept_Id = base.ParmJObj.GetInt("Dept_Id"),
                QDeptIds = base.ParmJObj.GetStringValue("QDeptIds"),
                EmployeeName = base.ParmJObj.GetStringValue("EmployeeName"),//姓名
                AccountTypeStr = base.ParmJObj.GetStringValue("AccountTypeStr"),//人员类型【逗号分割】
            };
            query.Group_Id = base.UserInfo.Group_Id;
            var list = employeeModule.GetEmployeeListModule(query);
            return ApiResult.Success(data: list?.Select(qitem => new
            {
                qitem.Id,
                qitem.Account,
                qitem.Group_Id,
                qitem.GroupName,
                qitem.School_Id,
                qitem.SchoolName,
                qitem.EmployeeName,
                qitem.UserIcon,
                qitem.Dept_Id,
                qitem.DeptName,
                qitem.Post_Id,
                qitem.PostName
            }));
        }
        
        /// <summary>
        /// 获取部门员工树形列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetEmployeeAddrBook()
        {
            var query= new RB_Department_ViewModel()
            {
                Group_Id=base.ParmJObj.GetInt("Group_Id"),
            };
            var list = departmentModule.GetDepartmentTreeModule(query, isQueryEmployee: true);
            return ApiResult.Success(data: list);
        }
        #endregion

        #region 枚举相列表

        /// <summary>
        /// 获取学历列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetEducationList()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(EducationEnum));
            list.Insert(0, new EnumItem()
            {
                Id = 0,
                Name = "不限"
            });
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 获取离职状态列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetLeaveStatus()
        {
            var list = Common.Plugin.EnumHelper.EnumToList(typeof(LeaveStatusEnum));
            list.Insert(0, new EnumItem()
            {
                Id = 0,
                Name = "不限"
            });
            return ApiResult.Success(data: list);
        }
        #endregion
    }
}