feat(企业安全管理): 新增唯一性校验,修复多记录查询问题并优化业务逻辑
- 修复角色查询方法,处理多条有效安全管理员记录场景,改为获取最新记录并添加警告日志 - 在安全管理人员新增/更新时新增唯一性校验,阻止同一企业下重复手机号和用户的有效记录 - 重构企业负责人业务逻辑,拆分通用方法,优化现有记录查找逻辑,新增旧负责人数据清理功
This commit is contained in:
@@ -171,15 +171,29 @@ public class SysCompanyRoleServiceImpl implements ISysCompanyRoleService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> selectRoleKeys(Long userId, Long companyId) {
|
public Set<String> selectRoleKeys(Long userId, Long companyId) {
|
||||||
// Query HotCompanySafetyManager to get role ID
|
// hot_company_safety_manager 可能存在重复有效数据,按最近一条记录兜底,避免登录鉴权直接失败
|
||||||
HotCompanySafetyManager manager = safetyManagerMapper.selectOne(Wrappers.<HotCompanySafetyManager>lambdaQuery()
|
List<HotCompanySafetyManager> managers = safetyManagerMapper.selectList(Wrappers.<HotCompanySafetyManager>lambdaQuery()
|
||||||
.select(HotCompanySafetyManager::getPermissionRoleId)
|
.select(HotCompanySafetyManager::getId, HotCompanySafetyManager::getPermissionRoleId,
|
||||||
|
HotCompanySafetyManager::getUpdateTime, HotCompanySafetyManager::getCreateTime)
|
||||||
.eq(HotCompanySafetyManager::getUserId, userId)
|
.eq(HotCompanySafetyManager::getUserId, userId)
|
||||||
.eq(HotCompanySafetyManager::getCompanyId, companyId)
|
.eq(HotCompanySafetyManager::getCompanyId, companyId)
|
||||||
.eq(HotCompanySafetyManager::getIsDeleted, 0)
|
.eq(HotCompanySafetyManager::getIsDeleted, 0)
|
||||||
.eq(HotCompanySafetyManager::getStatus, 1)
|
.eq(HotCompanySafetyManager::getStatus, 1)
|
||||||
.and(w -> w.eq(HotCompanySafetyManager::getIsResigned, 0).or().isNull(HotCompanySafetyManager::getIsResigned))
|
.and(w -> w.eq(HotCompanySafetyManager::getIsResigned, 0).or().isNull(HotCompanySafetyManager::getIsResigned))
|
||||||
);
|
.orderByDesc(HotCompanySafetyManager::getUpdateTime)
|
||||||
|
.orderByDesc(HotCompanySafetyManager::getCreateTime)
|
||||||
|
.orderByDesc(HotCompanySafetyManager::getId));
|
||||||
|
|
||||||
|
if (managers == null || managers.isEmpty()) {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (managers.size() > 1) {
|
||||||
|
log.warn("查询企业角色标识命中多条安全管理人员记录,按最新一条兜底返回 userId={}, companyId={}, count={}",
|
||||||
|
userId, companyId, managers.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
HotCompanySafetyManager manager = managers.get(0);
|
||||||
|
|
||||||
if (manager == null || manager.getPermissionRoleId() == null) {
|
if (manager == null || manager.getPermissionRoleId() == null) {
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
|
|||||||
@@ -378,40 +378,61 @@ public class SysCompanyServiceImpl implements ISysCompanyService {
|
|||||||
// 确保有企业管理员角色
|
// 确保有企业管理员角色
|
||||||
Long roleId = createEnterpriseAdminRole(companyId, userId, responsibleName);
|
Long roleId = createEnterpriseAdminRole(companyId, userId, responsibleName);
|
||||||
|
|
||||||
// 查找是否已存在该人员记录
|
HotCompanySafetyManager safetyManager = resolveResponsibleSafetyManager(companyId, userId, responsibleMobile);
|
||||||
LambdaQueryWrapper<HotCompanySafetyManager> smWrapper = new LambdaQueryWrapper<>();
|
|
||||||
smWrapper.eq(HotCompanySafetyManager::getCompanyId, companyId)
|
|
||||||
.eq(HotCompanySafetyManager::getPhone, responsibleMobile);
|
|
||||||
HotCompanySafetyManager safetyManager = hotCompanySafetyManagerMapper.selectOne(smWrapper);
|
|
||||||
|
|
||||||
if (safetyManager == null) {
|
if (safetyManager == null) {
|
||||||
safetyManager = new HotCompanySafetyManager();
|
safetyManager = new HotCompanySafetyManager();
|
||||||
|
fillResponsibleSafetyManager(safetyManager, companyId, responsibleName, responsibleMobile, userId, roleId);
|
||||||
|
hotCompanySafetyManagerMapper.insert(safetyManager);
|
||||||
|
log.info("[企业负责人] 创建安全管理人员记录 companyId={}, userId={}, managerId={}", companyId, userId, safetyManager.getId());
|
||||||
|
} else {
|
||||||
|
fillResponsibleSafetyManager(safetyManager, companyId, responsibleName, responsibleMobile, userId, roleId);
|
||||||
|
hotCompanySafetyManagerMapper.updateById(safetyManager);
|
||||||
|
log.info("[企业负责人] 更新安全管理人员记录 companyId={}, userId={}, managerId={}", companyId, userId, safetyManager.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private HotCompanySafetyManager resolveResponsibleSafetyManager(Long companyId, Long userId, String responsibleMobile) {
|
||||||
|
List<HotCompanySafetyManager> matches = hotCompanySafetyManagerMapper.selectList(Wrappers.<HotCompanySafetyManager>lambdaQuery()
|
||||||
|
.eq(HotCompanySafetyManager::getCompanyId, companyId)
|
||||||
|
.eq(HotCompanySafetyManager::getUserId, userId)
|
||||||
|
.eq(HotCompanySafetyManager::getIsDeleted, 0L)
|
||||||
|
.orderByDesc(HotCompanySafetyManager::getCompanyHead)
|
||||||
|
.orderByDesc(HotCompanySafetyManager::getUpdateTime)
|
||||||
|
.orderByDesc(HotCompanySafetyManager::getCreateTime)
|
||||||
|
.orderByDesc(HotCompanySafetyManager::getId));
|
||||||
|
if (CollUtil.isEmpty(matches) && StringUtils.isNotBlank(responsibleMobile)) {
|
||||||
|
matches = hotCompanySafetyManagerMapper.selectList(Wrappers.<HotCompanySafetyManager>lambdaQuery()
|
||||||
|
.eq(HotCompanySafetyManager::getCompanyId, companyId)
|
||||||
|
.eq(HotCompanySafetyManager::getPhone, responsibleMobile)
|
||||||
|
.eq(HotCompanySafetyManager::getIsDeleted, 0L)
|
||||||
|
.orderByDesc(HotCompanySafetyManager::getCompanyHead)
|
||||||
|
.orderByDesc(HotCompanySafetyManager::getUpdateTime)
|
||||||
|
.orderByDesc(HotCompanySafetyManager::getCreateTime)
|
||||||
|
.orderByDesc(HotCompanySafetyManager::getId));
|
||||||
|
}
|
||||||
|
if (CollUtil.isEmpty(matches)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (matches.size() > 1) {
|
||||||
|
log.warn("[企业负责人] 命中多条安全管理人员记录,按最新一条更新 companyId={}, userId={}, phone={}, count={}",
|
||||||
|
companyId, userId, responsibleMobile, matches.size());
|
||||||
|
}
|
||||||
|
return matches.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillResponsibleSafetyManager(HotCompanySafetyManager safetyManager, Long companyId, String responsibleName,
|
||||||
|
String responsibleMobile, Long userId, Long roleId) {
|
||||||
safetyManager.setCompanyId(companyId);
|
safetyManager.setCompanyId(companyId);
|
||||||
safetyManager.setName(responsibleName);
|
safetyManager.setName(responsibleName);
|
||||||
safetyManager.setPhone(responsibleMobile);
|
safetyManager.setPhone(responsibleMobile);
|
||||||
safetyManager.setUserId(userId);
|
safetyManager.setUserId(userId);
|
||||||
// 设置为企业负责人
|
|
||||||
safetyManager.setCompanyHead(1L);
|
|
||||||
// 企业负责人权限变更
|
|
||||||
safetyManager.setPermissionType("company_admin");
|
|
||||||
safetyManager.setPermissionRoleId(roleId);
|
|
||||||
|
|
||||||
safetyManager.setIsResigned(0L);
|
|
||||||
safetyManager.setStatus(1L);
|
|
||||||
hotCompanySafetyManagerMapper.insert(safetyManager);
|
|
||||||
log.info("[企业负责人] 创建安全管理人员记录 companyId={}, userId={}, managerId={}", companyId, userId, safetyManager.getId());
|
|
||||||
} else {
|
|
||||||
// 更新现有记录
|
|
||||||
safetyManager.setName(responsibleName);
|
|
||||||
safetyManager.setUserId(userId);
|
|
||||||
safetyManager.setCompanyHead(1L);
|
safetyManager.setCompanyHead(1L);
|
||||||
safetyManager.setPermissionType("company_admin");
|
safetyManager.setPermissionType("company_admin");
|
||||||
safetyManager.setPermissionRoleId(roleId);
|
safetyManager.setPermissionRoleId(roleId);
|
||||||
|
safetyManager.setIsDeleted(0L);
|
||||||
safetyManager.setIsResigned(0L);
|
safetyManager.setIsResigned(0L);
|
||||||
safetyManager.setStatus(1L);
|
safetyManager.setStatus(1L);
|
||||||
hotCompanySafetyManagerMapper.updateById(safetyManager);
|
|
||||||
log.info("[企业负责人] 更新安全管理人员记录 companyId={}, userId={}, managerId={}", companyId, userId, safetyManager.getId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long createEnterpriseAdminRole(Long companyId, Long userId, String username) {
|
public Long createEnterpriseAdminRole(Long companyId, Long userId, String username) {
|
||||||
@@ -486,23 +507,7 @@ public class SysCompanyServiceImpl implements ISysCompanyService {
|
|||||||
// 1. 处理旧负责人:移除相关权限,标记离职(仅当负责人手机号变更时)
|
// 1. 处理旧负责人:移除相关权限,标记离职(仅当负责人手机号变更时)
|
||||||
String oldMobile = oldCompany.getResponsibleMobile();
|
String oldMobile = oldCompany.getResponsibleMobile();
|
||||||
if (StrUtil.isNotBlank(oldMobile) && !StrUtil.equals(oldMobile, bo.getResponsibleMobile())) {
|
if (StrUtil.isNotBlank(oldMobile) && !StrUtil.equals(oldMobile, bo.getResponsibleMobile())) {
|
||||||
SysUser oldUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhonenumber, oldMobile));
|
cleanupOldResponsibleData(bo.getId(), oldMobile);
|
||||||
if (oldUser != null) {
|
|
||||||
Long oldUserId = oldUser.getUserId();
|
|
||||||
// 移除该用户在当前企业的角色关联
|
|
||||||
sysUserRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
|
|
||||||
.eq(SysUserRole::getUserId, oldUserId)
|
|
||||||
.eq(SysUserRole::getLoginPort, ENTERPRISE_PORT)
|
|
||||||
.eq(SysUserRole::getCompanyId, bo.getId()));
|
|
||||||
|
|
||||||
// 移除该用户与企业的关联关系
|
|
||||||
sysUserLoginPortMapper.delete(new LambdaQueryWrapper<SysUserLoginPort>()
|
|
||||||
.eq(SysUserLoginPort::getUserId, oldUserId)
|
|
||||||
.eq(SysUserLoginPort::getLoginPort, ENTERPRISE_PORT)
|
|
||||||
.eq(SysUserLoginPort::getCompanyId, bo.getId()));
|
|
||||||
|
|
||||||
hotCompanySafetyManagerMapper.deleteCompanyHeadByPhone(bo.getId(), oldMobile);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 添加新负责人:创建用户(如果不存在)、赋予权限、添加安全管理人员记录
|
// 2. 添加新负责人:创建用户(如果不存在)、赋予权限、添加安全管理人员记录
|
||||||
@@ -512,6 +517,39 @@ public class SysCompanyServiceImpl implements ISysCompanyService {
|
|||||||
return flag;
|
return flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void cleanupOldResponsibleData(Long companyId, String oldMobile) {
|
||||||
|
if (companyId == null || StrUtil.isBlank(oldMobile)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SysUser oldUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhonenumber, oldMobile));
|
||||||
|
Long oldUserId = oldUser != null ? oldUser.getUserId() : null;
|
||||||
|
if (oldUserId != null) {
|
||||||
|
sysUserRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
|
||||||
|
.eq(SysUserRole::getUserId, oldUserId)
|
||||||
|
.eq(SysUserRole::getLoginPort, ENTERPRISE_PORT)
|
||||||
|
.eq(SysUserRole::getCompanyId, companyId));
|
||||||
|
sysUserLoginPortMapper.delete(new LambdaQueryWrapper<SysUserLoginPort>()
|
||||||
|
.eq(SysUserLoginPort::getUserId, oldUserId)
|
||||||
|
.eq(SysUserLoginPort::getLoginPort, ENTERPRISE_PORT)
|
||||||
|
.eq(SysUserLoginPort::getCompanyId, companyId));
|
||||||
|
}
|
||||||
|
LambdaQueryWrapper<HotCompanySafetyManager> wrapper = Wrappers.<HotCompanySafetyManager>lambdaQuery()
|
||||||
|
.eq(HotCompanySafetyManager::getCompanyId, companyId)
|
||||||
|
.eq(HotCompanySafetyManager::getCompanyHead, 1L)
|
||||||
|
.eq(HotCompanySafetyManager::getIsDeleted, 0L)
|
||||||
|
.and(w -> {
|
||||||
|
w.eq(HotCompanySafetyManager::getPhone, oldMobile);
|
||||||
|
if (oldUserId != null) {
|
||||||
|
w.or().eq(HotCompanySafetyManager::getUserId, oldUserId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
int deleted = hotCompanySafetyManagerMapper.delete(wrapper);
|
||||||
|
if (deleted > 0) {
|
||||||
|
log.info("[企业负责人] 清理旧负责人安全管理人员记录 companyId={}, oldMobile={}, oldUserId={}, deleted={}",
|
||||||
|
companyId, oldMobile, oldUserId, deleted);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存前的数据校验
|
* 保存前的数据校验
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -451,6 +451,8 @@ public class HotCompanySafetyManagerServiceImpl implements IHotCompanySafetyMana
|
|||||||
userId = sysUser.getUserId();
|
userId = sysUser.getUserId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validateManagerUniqueness(add.getCompanyId(), add.getPhone(), userId, null);
|
||||||
|
|
||||||
// 初始化或更新子密码(特定企业的登录密码)
|
// 初始化或更新子密码(特定企业的登录密码)
|
||||||
String subPassword;
|
String subPassword;
|
||||||
if (StringUtils.isNotBlank(add.getLoginPassword())) {
|
if (StringUtils.isNotBlank(add.getLoginPassword())) {
|
||||||
@@ -521,6 +523,9 @@ public class HotCompanySafetyManagerServiceImpl implements IHotCompanySafetyMana
|
|||||||
if (before == null) {
|
if (before == null) {
|
||||||
throw new ServiceException("安全管理人员不存在");
|
throw new ServiceException("安全管理人员不存在");
|
||||||
}
|
}
|
||||||
|
Long finalCompanyId = update.getCompanyId() != null ? update.getCompanyId() : before.getCompanyId();
|
||||||
|
String finalPhone = StringUtils.isNotBlank(update.getPhone()) ? update.getPhone() : before.getPhone();
|
||||||
|
validateManagerUniqueness(finalCompanyId, finalPhone, before.getUserId(), update.getId());
|
||||||
// 参考新增:编辑时如果更换部门,且目标部门暂无负责人、当前人员又不是负责人,则不允许变更
|
// 参考新增:编辑时如果更换部门,且目标部门暂无负责人、当前人员又不是负责人,则不允许变更
|
||||||
boolean deptChanged = update.getDeptId() != null && !Objects.equals(update.getDeptId(), before.getDeptId());
|
boolean deptChanged = update.getDeptId() != null && !Objects.equals(update.getDeptId(), before.getDeptId());
|
||||||
Long finalIsDepartmentHead = update.getIsDepartmentHead() != null ? update.getIsDepartmentHead() : before.getIsDepartmentHead();
|
Long finalIsDepartmentHead = update.getIsDepartmentHead() != null ? update.getIsDepartmentHead() : before.getIsDepartmentHead();
|
||||||
@@ -575,6 +580,39 @@ public class HotCompanySafetyManagerServiceImpl implements IHotCompanySafetyMana
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validateManagerUniqueness(Long companyId, String phone, Long userId, Long excludeId) {
|
||||||
|
if (companyId == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotBlank(phone)) {
|
||||||
|
LambdaQueryWrapper<HotCompanySafetyManager> phoneWrapper = Wrappers.<HotCompanySafetyManager>lambdaQuery()
|
||||||
|
.eq(HotCompanySafetyManager::getCompanyId, companyId)
|
||||||
|
.eq(HotCompanySafetyManager::getPhone, phone)
|
||||||
|
.eq(HotCompanySafetyManager::getIsDeleted, 0L);
|
||||||
|
if (excludeId != null) {
|
||||||
|
phoneWrapper.ne(HotCompanySafetyManager::getId, excludeId);
|
||||||
|
}
|
||||||
|
if (baseMapper.selectCount(phoneWrapper) > 0) {
|
||||||
|
throw new ServiceException("同一公司下手机号不允许重复");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (userId == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LambdaQueryWrapper<HotCompanySafetyManager> userWrapper = Wrappers.<HotCompanySafetyManager>lambdaQuery()
|
||||||
|
.eq(HotCompanySafetyManager::getCompanyId, companyId)
|
||||||
|
.eq(HotCompanySafetyManager::getUserId, userId)
|
||||||
|
.eq(HotCompanySafetyManager::getIsDeleted, 0L)
|
||||||
|
.eq(HotCompanySafetyManager::getStatus, 1L)
|
||||||
|
.and(w -> w.eq(HotCompanySafetyManager::getIsResigned, 0L).or().isNull(HotCompanySafetyManager::getIsResigned));
|
||||||
|
if (excludeId != null) {
|
||||||
|
userWrapper.ne(HotCompanySafetyManager::getId, excludeId);
|
||||||
|
}
|
||||||
|
if (baseMapper.selectCount(userWrapper) > 0) {
|
||||||
|
throw new ServiceException("同一公司下该用户已存在有效的安全管理人员记录");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void syncUserAndRecordHistory(HotCompanySafetyManager update, HotCompanySafetyManager before, String changeReason, String beforeBusiness) {
|
private void syncUserAndRecordHistory(HotCompanySafetyManager update, HotCompanySafetyManager before, String changeReason, String beforeBusiness) {
|
||||||
SysUser sysUser = findUserByPhone(before.getPhone());
|
SysUser sysUser = findUserByPhone(before.getPhone());
|
||||||
if (sysUser == null) {
|
if (sysUser == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user