using Edu.AOP.CustomerAttribute;
using Edu.Common.Encrypt;
using Edu.Model.ViewModel.User;
using Edu.Repository.User;
using System;
using System.Collections.Generic;
using VT.FW.DB;

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


        /// <summary>
        /// 获取账户信息
        /// </summary>
        /// <param name="empId"></param>
        /// <returns></returns>
        public Employee_ViewModel GetEmployeeInfo(int empId) {
            return accountRepository.GetEmployeeInfo(empId);
        }

        /// <summary>
        /// 获取账号列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Account_ViewModel> GetAccountListModule(RB_Account_ViewModel query)
        {
            return accountRepository.GetAccountListRepository(query);
        }

        /// <summary>
        /// 获取指定人员的企业微信ID
        /// </summary>
        /// <param name="id">教师编号</param>
        /// <returns></returns>
        public string GetWorkUserIdModule(int id)
        {
            return accountRepository.GetWorkUserIdRepository(id);
        }

        /// <summary>
        /// 获取指定部门下员工的企业号ID
        /// </summary>
        /// <param name="departmentIds"></param>
        /// <returns></returns>
        public string GetWorkUserIdByDepartmentModule(string departmentIds)
        {
            return accountRepository.GetWorkUserIdByDepartmentRepository(departmentIds);
        }

        /// <summary>
        /// 获取账号列表(批量修改密码专用)
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Account_ViewModel> GetUpdateAccountListRepository(List<RB_Account_ViewModel> query)
        {
            return accountRepository.GetUpdateAccountListRepository(query);
        }

        /// <summary>
        /// 获取账号列表扩展列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Account_ViewModel> GetAccountListExtModule(RB_Account_ViewModel query)
        {
            return accountRepository.GetAccountListExtRepository(query);
        }

        /// <summary>
        /// 获取学员账号信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Account_ViewModel> GetStudentExt(RB_Account_ViewModel query)
        {
            return accountRepository.GetStudentExt(query);
        }


        /// <summary>
        /// 更新账户的UnionId
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateAccountUnionId(RB_Account_ViewModel model)
        {
            bool flag = false;
            if (model.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Account_ViewModel.OpenId),model.OpenId.Trim() },
                      {nameof(RB_Account_ViewModel.UnionId),model.UnionId.Trim() }
                };
                flag = accountRepository.Update(fileds, new WhereHelper(nameof(RB_Account_ViewModel.Id), model.Id));
            }
            return flag;
        }

        /// <summary>
        /// 添加修改账号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetAccountModule(RB_Account_ViewModel model)
        {
            bool flag;
            if (model.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Account_ViewModel.Account),model.Account.Trim() },
                    {nameof(RB_Account_ViewModel.UpdateBy),model.UpdateBy},
                    {nameof(RB_Account_ViewModel.UpdateTime),model.UpdateTime},
                    {nameof(RB_Account_ViewModel.DirectSupervisor),model.DirectSupervisor},
                    {nameof(RB_Account_ViewModel.School_Id),model.School_Id},
                };
                flag = accountRepository.Update(fileds, new WhereHelper(nameof(RB_Account_ViewModel.Id), model.Id));
            }
            else
            {
                var newId = accountRepository.Insert(model);
                model.Id = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 修改账号状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetAccountStatusModule(RB_Account_ViewModel model)
        {
            bool flag;
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Account_ViewModel.Status),(int)model.Status },
                {nameof(RB_Account_ViewModel.UpdateTime),model.UpdateTime },
            };
            List<WhereHelper> list = new List<WhereHelper>()
            {
                new WhereHelper(nameof(RB_Account_ViewModel.AccountId),model.AccountId),
                new WhereHelper(nameof(RB_Account_ViewModel.AccountType),(int)model.AccountType),
            };
            flag = accountRepository.Update(fileds, list);
            return flag;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public (bool reuslt, string newPass) SetResetPassword(RB_Account_ViewModel model)
        {
            bool flag;
            //生成随机新密码
            var newPwd = PwHelper.MakePassword(8);
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Account_ViewModel.Password), Common.DES.Encrypt(newPwd)}
            };
            var wheres = new List<WhereHelper>
            {
                new WhereHelper(nameof(RB_Account_ViewModel.AccountId), model.AccountId),
                new WhereHelper(nameof(RB_Account_ViewModel.AccountType), model.AccountType),
                new WhereHelper(nameof(RB_Account_ViewModel.Group_Id), model.Group_Id)
            };
            flag = accountRepository.Update(fileds, wheres);
            return (flag, newPwd);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetBatchResetPassword(List<RB_Account_ViewModel> list, string newPwd)
        {
            bool flag = false;
            foreach (var model in list)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Account_ViewModel.Password), Common.DES.Encrypt(newPwd)}
                };
                var wheres = new List<WhereHelper>
                {
                    new WhereHelper(nameof(RB_Account_ViewModel.AccountId), model.AccountId),
                    new WhereHelper(nameof(RB_Account_ViewModel.AccountType), model.AccountType),
                    new WhereHelper(nameof(RB_Account_ViewModel.Group_Id), model.Group_Id)
                };
                flag = accountRepository.Update(fileds, wheres);
            }
            return flag;
        }


        /// <summary>
        /// 修改密码以及更新激活状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetResetPwdAndAtatus(RB_Account_ViewModel model, string newPwd)
        {
            bool flag = false;
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Account_ViewModel.Password), newPwd},
                {nameof(RB_Account_ViewModel.ActivationStatus), model.ActivationStatus}
            };
            if (!string.IsNullOrWhiteSpace(model.OpenId))
            {
                fileds.Add(nameof(RB_Account_ViewModel.OpenId), model.OpenId);
            }
            var wheres = new List<WhereHelper>
            {
                new WhereHelper(nameof(RB_Account_ViewModel.AccountId), model.AccountId),
                new WhereHelper(nameof(RB_Account_ViewModel.AccountType), model.AccountType),
                new WhereHelper(nameof(RB_Account_ViewModel.Group_Id), model.Group_Id)
            };
            flag = accountRepository.Update(fileds, wheres);
            return flag;
        }

        /// <summary>
        /// 获取指定教师的企业微信ID
        /// </summary>
        /// <param name="tid">教师编号</param>
        /// <returns></returns>
        public string GetWorkUserIdByTIdModule(int tid)
        {
            return accountRepository.GetWorkUserIdByTidRepository(tid);
        }
    }
}