904 lines
31 KiB
C#
Raw Normal View History

2025-08-13 11:14:39 +08:00
using Learun.Cache.Base;
using Learun.Cache.Factory;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using Learun.Application.WeChat;
using System.Web;
using System.ComponentModel.Design;
namespace Learun.Application.Organization
{
/// <summary>
/// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
/// Copyright (c) 2013-2018 Hexagon PPM
/// 创建人:研发部
/// 日 期2017.03.06
/// 描 述:用户模块业务类
/// </summary>
public class UserBLL : UserIBLL
{
#region
private UserService userService = new UserService();
private DepartmentIBLL departmentIBLL = new DepartmentBLL();
#endregion
#region
private ICache cache = CacheFactory.CaChe();
private string cacheKeyAll = "_lr_base_user_";
private string cacheKey = "learun_adms_user_"; // +公司主键
private string cacheKeyAccount = "learun_adms_user_account_";// +用户账号(账号不允许改动)
private string cacheKeyId = "learun_adms_user_Id_";// +用户账号(账号不允许改动)
private static string LoginUserMarkKeyNew = "Learun_ADMS_V7_MarkNew_";
#endregion
#region
/// <summary>
/// 用户列表(根据公司主键)
/// </summary>
/// <param name="companyId">公司主键</param>
/// <returns></returns>
public List<UserEntity> GetList(string companyId)
{
try
{
if (string.IsNullOrEmpty(companyId))
{
return new List<UserEntity>();
}
List<UserEntity> list = cache.Read<List<UserEntity>>(cacheKey + companyId, CacheId.user);
if (list == null)
{
list = userService.GetList(companyId);
cache.Write<List<UserEntity>>(cacheKey + companyId, list, CacheId.user);
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 用户列表(根据公司主键,部门主键)
/// </summary>
/// <param name="companyId">公司主键</param>
/// <param name="departmentId">部门主键</param>
/// <param name="keyword">查询关键词</param>
/// <returns></returns>
public List<UserEntity> GetList(string companyId, string departmentId, string keyword)
{
try
{
List<UserEntity> list = GetList(companyId);
if (!string.IsNullOrEmpty(departmentId))
{
list = list.FindAll(t => t.F_DepartmentId.ContainsEx(departmentId));
}
if (!string.IsNullOrEmpty(keyword))
{
list = list.FindAll(t => t.F_RealName.ContainsEx(keyword) || t.F_Account.ContainsEx(keyword));
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 用户列表(全部)
/// </summary>
/// <returns></returns>
public List<UserEntity> GetAllList()
{
try
{
var list = cache.GetRedisOrDBData(cacheKeyAll, "", () => userService.GetAllList());
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 用户列表(根据部门主键)
/// </summary>
/// <param name="departmentId">部门主键</param>
/// <returns></returns>
public List<UserEntity> GetListByDepartmentId(string departmentId)
{
try
{
if (string.IsNullOrEmpty(departmentId))
{
return new List<UserEntity>();
}
DepartmentEntity departmentEntity = departmentIBLL.GetEntity(departmentId);
if (departmentEntity == null)
{
return new List<UserEntity>();
}
return GetList(departmentEntity.F_CompanyId, departmentId, "");
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 获取分页数据
/// </summary>
/// <param name="companyId">公司主键</param>
/// <param name="departmentId">部门主键</param>
/// <param name="pagination">分页参数</param>
/// <param name="keyword">查询关键词</param>
/// <returns></returns>
public List<UserEntity> GetPageList(string companyId, string departmentId, Pagination pagination, string keyword)
{
try
{
return userService.GetPageList(companyId, departmentId, pagination, keyword);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 用户列表导出Excel
/// </summary>
/// <returns></returns>
public void GetExportList()
{
try
{
//取出数据源
DataTable exportTable = userService.GetExportList();
//设置导出格式
ExcelConfig excelconfig = new ExcelConfig();
excelconfig.Title = "用户导出";
excelconfig.TitleFont = "微软雅黑";
excelconfig.TitlePoint = 25;
excelconfig.FileName = "用户导出.xls";
excelconfig.IsAllSizeColumn = true;
//每一列的设置,没有设置的列信息系统将按datatable中的列名导出
excelconfig.ColumnEntity = new List<ColumnModel>();
excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_account", ExcelColumn = "账户" });
excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_realname", ExcelColumn = "姓名" });
excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_gender", ExcelColumn = "性别" });
excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_mobile", ExcelColumn = "手机" });
excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_description", ExcelColumn = "说明" });
excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_createdate", ExcelColumn = "创建日期" });
excelconfig.ColumnEntity.Add(new ColumnModel() { Column = "f_createusername", ExcelColumn = "创建人" });
//调用导出方法
ExcelHelper.ExcelDownload(exportTable, excelconfig);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 获取实体,通过用户账号。redis介入
/// </summary>
/// <param name="account">用户账号</param>
/// <returns></returns>
public UserEntity GetEntityByAccount(string account)
{
try
{
string userId = cache.Read<string>(cacheKeyAccount + account, CacheId.user);
UserEntity userEntity;
if (string.IsNullOrEmpty(userId))
{
userEntity = userService.GetEntityByAccount(account);
if (userEntity != null)
{
cache.Write<string>(cacheKeyAccount + userEntity.F_Account, userEntity.F_UserId, CacheId.user);
cache.Write<UserEntity>(cacheKeyId + userEntity.F_UserId, userEntity, CacheId.user);//redis没有就写入redis
}
}
else
{
userEntity = cache.Read<UserEntity>(cacheKeyId + userId, CacheId.user);//redis中有就直接拿不从数据库拿
if (userEntity == null)
{
userEntity = userService.GetEntityByAccount(account);
if (userEntity != null)
{
cache.Write<UserEntity>(cacheKeyId + userEntity.F_UserId, userEntity, CacheId.user);
}
}
}
return userEntity;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 获取用户数据
/// </summary>
/// <param name="userId">用户主键</param>
/// <returns></returns>
public UserEntity GetEntityByUserId(string userId)
{
try
{
UserEntity userEntity = userEntity = userService.GetEntity(userId);
return userEntity;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 获取用户列表数据
/// </summary>
/// <param name="userIds">用户主键串</param>
/// <returns></returns>
public List<UserEntity> GetListByUserIds(string userIds)
{
try
{
if (string.IsNullOrEmpty(userIds))
{
return null;
}
List<UserEntity> list = new List<UserEntity>();
string[] userList = userIds.Split(',');
foreach (string userId in userList)
{
UserEntity userEntity = userService.GetEntity(userId);
if (userEntity != null)
{
list.Add(userEntity);
}
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 获取映射数据
/// </summary>
/// <returns></returns>
public Dictionary<string, UserModel> GetModelMap()
{
try
{
Dictionary<string, UserModel> dic = cache.Read<Dictionary<string, UserModel>>(cacheKey + "dic", CacheId.user);
if (dic == null)
{
dic = new Dictionary<string, UserModel>();
var list = userService.GetAllList();
foreach (var item in list)
{
UserModel model = new UserModel()
{
companyId = item.F_CompanyId,
departmentId = item.F_DepartmentId,
name = item.F_RealName,
};
string img = "";
if (!string.IsNullOrEmpty(item.F_HeadIcon))
{
string fileHeadImg = Config.GetValue("fileHeadImg");
string fileImg = string.Format("{0}/{1}{2}", fileHeadImg, item.F_UserId, item.F_HeadIcon);
if (DirFileHelper.IsExistFile(fileImg))
{
img = item.F_HeadIcon;
}
}
if (string.IsNullOrEmpty(img))
{
if (item.F_Gender == 0)
{
img = "0";
}
else
{
img = "1";
}
}
model.img = img;
dic.Add(item.F_UserId, model);
}
cache.Write(cacheKey + "dic", dic, CacheId.user);
}
return dic;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 获取超级管理员用户列表
/// </summary>
/// <returns></returns>
public IEnumerable<UserEntity> GetAdminList()
{
try
{
return userService.GetAdminList();
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 用户实体
/// </summary>
/// <param name="keyValue">主键值</param>
/// <returns></returns>
public UserEntity GetEntity(string keyValue)
{
try
{
return userService.GetEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
#region
/// <summary>
/// 虚拟删除
/// </summary>
/// <param name="keyValue">主键</param>
public void Delete(string keyValue)
{
try
{
UserEntity userEntity = GetEntityByUserId(keyValue);
cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
cache.Remove(cacheKeyId + keyValue, CacheId.user);
cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
cache.Remove(cacheKeyAll);
Dictionary<string, UserModel> dic = GetModelMap();
dic.Remove(keyValue);
cache.Write(cacheKey + "dic", dic, CacheId.department);
userService.DeleteUser(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 保存用户表单(新增、修改)
/// </summary>
/// <param name="keyValue">主键值</param>
/// <param name="userEntity">用户实体</param>
/// <returns></returns>
public void SaveEntity(string keyValue, UserEntity userEntity)
{
try
{
if (string.IsNullOrEmpty(keyValue))
{
userEntity.F_Password = Md5Helper.Encrypt(GetInitPassword(userEntity), 32).ToLower(); //新建用户时,给配置文件里的默认密码
}
cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
cache.Remove(cacheKeyId + keyValue, CacheId.user);
cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
cache.Remove(cacheKey + "dic", CacheId.user);
cache.Remove(cacheKeyAll);
// 账号不允许改动
//if (!string.IsNullOrEmpty(keyValue))
//{
// userEntity.F_Account = null;
//}
#region
if (string.IsNullOrEmpty(keyValue))
{
userService.SaveEntity(keyValue, userEntity);
}
else
{
var existObj = userService.GetEntity(keyValue);
if (existObj == null)
{
throw new Exception("数据库中找不到该用户。请联系管理员。");
}
else
{
existObj.F_Account = userEntity.F_Account;
existObj.F_CompanyId = userEntity.F_CompanyId;
existObj.F_Description = userEntity.F_Description;
existObj.F_Gender = userEntity.F_Gender;
existObj.F_Mobile = userEntity.F_Mobile;
existObj.F_RealName = userEntity.F_RealName;
existObj.F_NickName = userEntity.F_NickName;
existObj.Modify(existObj.F_UserId);
userService.SaveEntity(keyValue, existObj);
}
}
#endregion
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 修改用户登录密码
/// </summary>
/// <param name="newPassword">新密码MD5 小写)</param>
/// <param name="oldPassword">旧密码MD5 小写)</param>
public bool RevisePassword(string newPassword, string oldPassword)
{
try
{
UserInfo userInfo = LoginUserInfo.Get();
cache.Remove(cacheKeyId + userInfo.userId, CacheId.user);
cache.Remove(cacheKeyAccount + userInfo.account, CacheId.user);
string oldPasswordByEncrypt = Md5Helper.Encrypt(DESEncrypt.Encrypt(oldPassword, userInfo.secretkey).ToLower(), 32).ToLower();
if (oldPasswordByEncrypt == userInfo.password)
{
//update by chenkai 20210322
userService.RevisePassword(userInfo.userId, newPassword, false);
}
else
{
return false;
}
return true;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 重置密码(000000)
/// </summary>
/// <param name="keyValue">账号主键</param>
public void ResetPassword(string keyValue)
{
//update by chenkai 20210322
try
{
UserEntity userEntity = GetEntityByUserId(keyValue);
cache.Remove(cacheKeyId + keyValue, CacheId.user);
cache.Remove("loginerrortime" + LoginUserMarkKeyNew + userEntity.F_Account.ToUpper(), CacheId.loginInfo);
string password = Md5Helper.Encrypt(GetInitPassword(userEntity), 32).ToLower();
userService.RevisePassword(keyValue, password, true);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
public string GetInitPassword(UserEntity user)
{
var initPasswordInXml = Config.GetValue("InitialPassword", "000000").ToString();
return initPasswordInXml;
}
/// <summary>
/// 修改用户状态
/// </summary>
/// <param name="keyValue">主键值</param>
/// <param name="state">状态1-启动0-禁用</param>
public void UpdateState(string keyValue, int state)
{
try
{
UserEntity userEntity = GetEntityByUserId(keyValue);
cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
cache.Remove(cacheKeyId + keyValue, CacheId.user);
cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
cache.Remove(cacheKey + "dic", CacheId.user);
cache.Remove("loginerrortime" + LoginUserMarkKeyNew + userEntity.F_Account.ToUpper(), CacheId.loginInfo);
userService.UpdateState(keyValue, state);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 修改用户登录密码
/// </summary>
///<param name="entity">用户实体对象</param>
/// <param name="newPassword">新密码MD5 小写)</param>
/// <param name="oldPassword">旧密码MD5 小写)</param>
public bool RevisePasswordByUserId(UserEntity entity, string newPassword, string oldPassword)
{
try
{
cache.Remove(cacheKeyId + entity.F_UserId, CacheId.user);
cache.Remove(cacheKeyAccount + entity.F_Account, CacheId.user);
string passwordMd5 = Md5Helper.Encrypt(oldPassword, 32);
string oldPasswordByEncrypt = Md5Helper.Encrypt(DESEncrypt.Encrypt(oldPassword, entity.F_Secretkey).ToLower(), 32).ToLower();
if (oldPasswordByEncrypt == entity.F_Password)
{
userService.RevisePassword(entity.F_UserId, newPassword, false);
}
else
{
return false;
}
return true;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 修改用户OpenId
/// </summary>
/// <param name="keyValue">主键值</param>
/// <param name="openId">openId</param>
public void UpdateOpenId(string keyValue, string openId)
{
try
{
UserEntity userEntity = GetEntityByUserId(keyValue);
cache.Remove(cacheKey + userEntity.F_CompanyId, CacheId.user);
cache.Remove(cacheKeyId + keyValue, CacheId.user);
cache.Remove(cacheKeyAccount + userEntity.F_Account, CacheId.user);
cache.Remove(cacheKey + "dic", CacheId.user);
cache.Remove("loginerrortime" + LoginUserMarkKeyNew + userEntity.F_Account.ToUpper(), CacheId.loginInfo);
userService.UpdateOpenId(keyValue, openId);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
#region
/// <summary>
/// 账户不能重复
/// </summary>
/// <param name="account">账户值</param>
/// <param name="keyValue">主键</param>
/// <returns></returns>
public bool ExistAccount(string account, string keyValue)
{
try
{
return userService.ExistAccount(account, keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
#region
/// <summary>
/// 验证登录
/// </summary>
/// <param name="account">账号</param>
/// <param name="password">密码 MD5 32位 小写</param>
/// <returns></returns>
public UserEntity CheckLogin(string account, string password)
{
try
{
UserEntity userEntity = GetEntityByAccount(account);
if (userEntity == null)
{
userEntity = new UserEntity()
{
LoginMsg = "密码或账户名错误!",
LoginOk = false
};
return userEntity;
}
userEntity.LoginOk = false;
if (userEntity.F_EnabledMark == 1)
{
string dbPassword = Md5Helper.Encrypt(DESEncrypt.Encrypt(password.ToLower(), userEntity.F_Secretkey).ToLower(), 32).ToLower();
if (dbPassword == userEntity.F_Password)
{
userEntity.LoginOk = true;
}
else
{
userEntity.LoginMsg = "密码或账户名错误!";
}
}
else
{
userEntity.LoginMsg = "账户被系统锁定,请联系管理员!";
}
return userEntity;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
/// <summary>
/// 获取用户头像
/// </summary>
/// <param name="userId">用户ID</param>
public void GetImg(string userId)
{
UserEntity entity = GetEntityByUserId(userId);
string img = "";
string fileHeadImg = Config.GetValue("fileHeadImg");
if (entity != null)
{
if (!string.IsNullOrEmpty(entity.F_HeadIcon))
{
string fileImg = string.Format("{0}/{1}{2}", fileHeadImg, entity.F_UserId, entity.F_HeadIcon);
if (DirFileHelper.IsExistFile(fileImg))
{
img = fileImg;
}
}
}
else
{
img = string.Format("{0}Content/images/head/{1}", System.AppDomain.CurrentDomain.BaseDirectory, "on-boy.jpg");
}
if (string.IsNullOrEmpty(img))
{
if (entity.F_Gender == 0)
{
img = string.Format("{0}Content/images/head/{1}", System.AppDomain.CurrentDomain.BaseDirectory, "on-girl.jpg");
}
else
{
img = string.Format("{0}Content/images/head/{1}", System.AppDomain.CurrentDomain.BaseDirectory, "on-boy.jpg");
}
}
FileDownHelper.DownLoadnew(img);
}
/// <summary>
/// 获取通用的人员选择
/// </summary>
/// <returns></returns>
public IEnumerable<UserEntity> GetCommonSelectUsers(Pagination pagination, string queryJson)
{
return userService.GetCommonSelectUsers(pagination, queryJson);
}
/// <summary>
/// 验证登录OpenId
/// </summary>
/// <param name="OpenId">OpenId</param>
/// <returns></returns>
public UserEntity CheckLoginByOpenId(string openId)
{
try
{
UserEntity userEntity = null;
if (string.IsNullOrEmpty(openId))
{
userEntity = new UserEntity()
{
LoginMsg = "openId参数异常!",
LoginOk = false
};
return userEntity;
}
userEntity = userService.GetEntityByOpenId(openId);
if (userEntity == null)
{
userEntity = new UserEntity()
{
LoginMsg = "用户未绑定微信",
LoginOk = false
};
return userEntity;
}
userEntity.LoginOk = false;
if (userEntity.F_EnabledMark == 1)
{
userEntity.LoginOk = true;
}
else
{
userEntity.LoginMsg = "账户被系统锁定,请联系管理员!";
}
return userEntity;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
}
}