using Edu.AOP.CustomerAttribute;
using Edu.Common.Enum;
using Edu.Common.Enum.User;
using Edu.Common.Plugin;
using Edu.Model.CacheModel;
using Edu.Model.ViewModel.User;
using Edu.Module.Log;
using Edu.Repository.User;
using System;
using System.Collections.Generic;
using System.Linq;
using VT.FW.DB;

namespace Edu.Module.User
{
    /// <summary>
    /// 管理者处理类
    /// </summary>
    public class ManagerModule
    {
        /// <summary>
        /// 管理者仓储层对象
        /// </summary>
        private readonly RB_ManagerRepository managerRepository = new RB_ManagerRepository();

        /// <summary>
        /// 账号处理类
        /// </summary>
        private readonly AccountModule accountModule = new AccountModule();

        /// <summary>
        /// 岗位仓储层对象
        /// </summary>
        private readonly RB_PostRepository postRepository = new RB_PostRepository();

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

        /// <summary>
        /// 用户信息日志处理类对象
        /// </summary>
        private readonly UserChangeLogModule userChangeLogModule = new UserChangeLogModule();

        /// <summary>
        /// 获取管理者列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Manager_ViewModel> GetManagerListModule(RB_Manager_ViewModel query)
        {
            return managerRepository.GetManagerListRepository(query);
        }

        /// <summary>
        /// 新增修改管理者
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetManagerModule(RB_Manager_ViewModel model)
        {
            bool flag;
            if (model.MId > 0)
            {
                var oldModel = GetManagerModule(model.MId);
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Manager_ViewModel.MName),model.MName.Trim()},
                    { nameof(RB_Manager_ViewModel.MTel),model.MTel.Trim()},
                    { nameof(RB_Manager_ViewModel.MHead),model.MHead },
                    { nameof(RB_Manager_ViewModel.UpdateBy),model.UpdateBy },
                    { nameof(RB_Manager_ViewModel.UpdateTime),model.UpdateTime },
                    { nameof(RB_Manager_ViewModel.Dept_Id),model.Dept_Id },
                    { nameof(RB_Manager_ViewModel.Post_Id),model.Post_Id },
                    { nameof(RB_Manager_ViewModel.IDCard),model.IDCard },
                    { nameof(RB_Manager_ViewModel.Sex),model.Sex },
                    { nameof(RB_Manager_ViewModel.EntryTime),model.EntryTime },
                    { nameof(RB_Manager_ViewModel.Address),model.Address },
                    { nameof(RB_Manager_ViewModel.BirthDate),model.BirthDate },
                    { nameof(RB_Manager_ViewModel.LeaveStatus),model.LeaveStatus },
                    { nameof(RB_Manager_ViewModel.LeaveTime),model.LeaveTime },
                    { nameof(RB_Manager_ViewModel.Education),model.Education },
                };
                string logContent = "";
                if (model.MName != oldModel.MName)
                {
                    logContent += string.Format(",将姓名由【{0}】修改为【{1}】。", oldModel.MName, oldModel.MName);
                }
                if (model.MTel != oldModel.MTel)
                {
                    logContent += string.Format(",将电话由【{0}】修改为【{1}】。", oldModel.MTel, oldModel.MTel);
                }
                if (model.Dept_Id != oldModel.Dept_Id)
                {
                    var deptList= departmentRepository.GetDepartmentListRepository(new RB_Department_ViewModel() { QDeptIds = model.Dept_Id + "," + oldModel.Dept_Id });
                    logContent += string.Format(",将部门由【{0}】修改为【{1}】。", (deptList.Where(qitem => qitem.DeptId == oldModel.Dept_Id)?.FirstOrDefault()?.DeptName ?? ""), (deptList.Where(qitem => qitem.DeptId == model.Dept_Id)?.FirstOrDefault()?.DeptName ?? ""));
                }
                if (model.Post_Id != oldModel.Post_Id)
                {
                    var postList = postRepository.GetPostListRepository( new RB_Post_ViewModel () { QPostIds = model.Post_Id + "," + oldModel.Post_Id });
                    logContent += string.Format(",将岗位由【{0}】修改为【{1}】。", (postList.Where(qitem => qitem.PostId == oldModel.Post_Id)?.FirstOrDefault()?.PostName ?? ""), (postList.Where(qitem => qitem.PostId == model.Post_Id)?.FirstOrDefault()?.PostName ?? ""));
                }
                if (model.IDCard != oldModel.IDCard)
                {
                    logContent += string.Format(",将身份证由【{0}】修改为【{1}】。", oldModel.IDCard, model.IDCard);
                }
                if (model.Sex != oldModel.Sex)
                {
                    logContent += string.Format(",将性别由【{0}】修改为【{1}】。", oldModel.Sex, model.Sex);
                }
                if (Common.ConvertHelper.FormatDate(model.EntryTime) != Common.ConvertHelper.FormatDate(oldModel.EntryTime))
                {
                    logContent += string.Format(",将入职时间由【{0}】修改为【{1}】。", Common.ConvertHelper.FormatDate(oldModel.EntryTime), Common.ConvertHelper.FormatDate(model.EntryTime));
                }
                if (model.Address != oldModel.Address)
                {
                    logContent += string.Format(",将地址由【{0}】修改为【{1}】。", oldModel.Address, model.Address);
                }
                if (Common.ConvertHelper.FormatDate(model.BirthDate) != Common.ConvertHelper.FormatDate(oldModel.BirthDate))
                {
                    logContent += string.Format(",将生日由【{0}】修改为【{1}】。", Common.ConvertHelper.FormatDate(oldModel.BirthDate),Common.ConvertHelper.FormatDate(model.BirthDate));
                }
                if (model.LeaveStatus != oldModel.LeaveStatus)
                {
                    logContent += string.Format(",将在职状态由【{0}】修改为【{1}】。", oldModel.LeaveStatus.ToName(),  model.LeaveStatus.ToName());
                }
                if (model.LeaveTime != oldModel.LeaveTime)
                {
                    logContent += string.Format(",将离职时间由【{0}】修改为【{1}】。", oldModel?.LeaveTime, model.LeaveTime);
                }
                if (model.Education != oldModel.Education)
                {
                    logContent += string.Format(",将学历由【{0}】修改为【{1}】。", oldModel.Education.ToName(), model.Education.ToName());
                }
                if (!string.IsNullOrEmpty(logContent))
                {
                    //新增日志
                    userChangeLogModule.SetUserChangeLogModule(model.CreateBy, model.Group_Id, model.School_Id, logContent);
                }
                flag = managerRepository.Update(fileds, new WhereHelper(nameof(RB_Manager_ViewModel.MId), model.MId));
            }
            else
            {
                var newId = managerRepository.Insert(model);
                model.MId = newId;
                flag = newId > 0;
                userChangeLogModule.SetUserChangeLogModule(model.CreateBy, model.Group_Id, model.School_Id, "新建用户");
            }
            return flag;
        }

        /// <summary>
        /// 根据编号获取管理者实体
        /// </summary>
        /// <param name="MId"></param>
        /// <returns></returns>
        public RB_Manager_ViewModel GetManagerModule(int MId)
        {
            var extModel = managerRepository.GetEntity<RB_Manager_ViewModel>(MId);
            return extModel;
        }

        /// <summary>
        /// 修改管理者状态
        /// </summary>
        /// <param name="MId"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool RemoveManagerModule(int MId, int Status)
        {
            bool flag = false;
            var model = GetManagerModule(MId);
            if (model != null && model.MId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Manager_ViewModel.Status), Status}
                };
                flag = managerRepository.Update(fileds, new WhereHelper(nameof(RB_Manager_ViewModel.MId), MId));
                var accountList = accountModule.GetAccountListExtModule(new RB_Account_ViewModel()
                {
                    AccountId = model.MId,
                    Account = model.MTel,
                    AccountType = AccountTypeEnum.Admin
                });
                if (accountList != null && accountList.Count > 0)
                {
                    flag = accountModule.SetAccountStatusModule(new RB_Account_ViewModel()
                    {
                        AccountType = AccountTypeEnum.Admin,
                        AccountId = model.MId,
                        UpdateTime = DateTime.Now,
                        Status = (DateStateEnum)Status
                    });
                }
            }
            return flag;
        }

        /// <summary>
        /// 创建管理者账号
        /// </summary>
        /// <param name="MId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public bool CreateManagerAccountModule(int MId, UserInfo user)
        {
            bool flag = false;
            var model = GetManagerModule(MId);
            if (model != null && model.MId > 0)
            {
                var accountList = accountModule.GetAccountListExtModule(new RB_Account_ViewModel()
                {
                    Account = model.MTel,
                    AccountType = AccountTypeEnum.Admin
                });
                if (accountList == null || (accountList != null && accountList.Count == 0))
                {
                    flag = accountModule.SetAccountModule(new RB_Account_ViewModel()
                    {
                        Account = model.MTel,
                        Password = Common.DES.Encrypt(Common.Config.DefaultPwd),
                        AccountType = AccountTypeEnum.Admin,
                        AccountId = model.MId,
                        CreateBy = user.Id,
                        UpdateBy = user.Id,
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        Group_Id = model.Group_Id,
                        School_Id = model.School_Id,
                        Status = model.Status,
                    });
                }
            }
            return flag;
        }
    }
}