diff --git a/src/main/java/com/hotwj/platform/resourceManagement/company/service/impl/SysCompanyRoleServiceImpl.java b/src/main/java/com/hotwj/platform/resourceManagement/company/service/impl/SysCompanyRoleServiceImpl.java index 73577a2..e491b52 100644 --- a/src/main/java/com/hotwj/platform/resourceManagement/company/service/impl/SysCompanyRoleServiceImpl.java +++ b/src/main/java/com/hotwj/platform/resourceManagement/company/service/impl/SysCompanyRoleServiceImpl.java @@ -171,15 +171,29 @@ public class SysCompanyRoleServiceImpl implements ISysCompanyRoleService { @Override public Set selectRoleKeys(Long userId, Long companyId) { - // Query HotCompanySafetyManager to get role ID - HotCompanySafetyManager manager = safetyManagerMapper.selectOne(Wrappers.lambdaQuery() - .select(HotCompanySafetyManager::getPermissionRoleId) + // hot_company_safety_manager 可能存在重复有效数据,按最近一条记录兜底,避免登录鉴权直接失败 + List managers = safetyManagerMapper.selectList(Wrappers.lambdaQuery() + .select(HotCompanySafetyManager::getId, HotCompanySafetyManager::getPermissionRoleId, + HotCompanySafetyManager::getUpdateTime, HotCompanySafetyManager::getCreateTime) .eq(HotCompanySafetyManager::getUserId, userId) .eq(HotCompanySafetyManager::getCompanyId, companyId) .eq(HotCompanySafetyManager::getIsDeleted, 0) .eq(HotCompanySafetyManager::getStatus, 1) .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) { return Collections.emptySet(); diff --git a/src/main/java/com/hotwj/platform/resourceManagement/company/service/impl/SysCompanyServiceImpl.java b/src/main/java/com/hotwj/platform/resourceManagement/company/service/impl/SysCompanyServiceImpl.java index 7303d1d..6a9588e 100644 --- a/src/main/java/com/hotwj/platform/resourceManagement/company/service/impl/SysCompanyServiceImpl.java +++ b/src/main/java/com/hotwj/platform/resourceManagement/company/service/impl/SysCompanyServiceImpl.java @@ -378,42 +378,63 @@ public class SysCompanyServiceImpl implements ISysCompanyService { // 确保有企业管理员角色 Long roleId = createEnterpriseAdminRole(companyId, userId, responsibleName); - // 查找是否已存在该人员记录 - LambdaQueryWrapper smWrapper = new LambdaQueryWrapper<>(); - smWrapper.eq(HotCompanySafetyManager::getCompanyId, companyId) - .eq(HotCompanySafetyManager::getPhone, responsibleMobile); - HotCompanySafetyManager safetyManager = hotCompanySafetyManagerMapper.selectOne(smWrapper); + HotCompanySafetyManager safetyManager = resolveResponsibleSafetyManager(companyId, userId, responsibleMobile); if (safetyManager == null) { safetyManager = new HotCompanySafetyManager(); - safetyManager.setCompanyId(companyId); - safetyManager.setName(responsibleName); - safetyManager.setPhone(responsibleMobile); - safetyManager.setUserId(userId); - // 设置为企业负责人 - safetyManager.setCompanyHead(1L); - // 企业负责人权限变更 - safetyManager.setPermissionType("company_admin"); - safetyManager.setPermissionRoleId(roleId); - - safetyManager.setIsResigned(0L); - safetyManager.setStatus(1L); + fillResponsibleSafetyManager(safetyManager, companyId, responsibleName, responsibleMobile, userId, roleId); hotCompanySafetyManagerMapper.insert(safetyManager); log.info("[企业负责人] 创建安全管理人员记录 companyId={}, userId={}, managerId={}", companyId, userId, safetyManager.getId()); } else { - // 更新现有记录 - safetyManager.setName(responsibleName); - safetyManager.setUserId(userId); - safetyManager.setCompanyHead(1L); - safetyManager.setPermissionType("company_admin"); - safetyManager.setPermissionRoleId(roleId); - safetyManager.setIsResigned(0L); - safetyManager.setStatus(1L); + 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 matches = hotCompanySafetyManagerMapper.selectList(Wrappers.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.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.setName(responsibleName); + safetyManager.setPhone(responsibleMobile); + safetyManager.setUserId(userId); + safetyManager.setCompanyHead(1L); + safetyManager.setPermissionType("company_admin"); + safetyManager.setPermissionRoleId(roleId); + safetyManager.setIsDeleted(0L); + safetyManager.setIsResigned(0L); + safetyManager.setStatus(1L); + } + public Long createEnterpriseAdminRole(Long companyId, Long userId, String username) { // 1. Check/Create SysCompanyRole LambdaQueryWrapper roleWrapper = new LambdaQueryWrapper<>(); @@ -486,23 +507,7 @@ public class SysCompanyServiceImpl implements ISysCompanyService { // 1. 处理旧负责人:移除相关权限,标记离职(仅当负责人手机号变更时) String oldMobile = oldCompany.getResponsibleMobile(); if (StrUtil.isNotBlank(oldMobile) && !StrUtil.equals(oldMobile, bo.getResponsibleMobile())) { - SysUser oldUser = sysUserMapper.selectOne(new LambdaQueryWrapper().eq(SysUser::getPhonenumber, oldMobile)); - if (oldUser != null) { - Long oldUserId = oldUser.getUserId(); - // 移除该用户在当前企业的角色关联 - sysUserRoleMapper.delete(new LambdaQueryWrapper() - .eq(SysUserRole::getUserId, oldUserId) - .eq(SysUserRole::getLoginPort, ENTERPRISE_PORT) - .eq(SysUserRole::getCompanyId, bo.getId())); - - // 移除该用户与企业的关联关系 - sysUserLoginPortMapper.delete(new LambdaQueryWrapper() - .eq(SysUserLoginPort::getUserId, oldUserId) - .eq(SysUserLoginPort::getLoginPort, ENTERPRISE_PORT) - .eq(SysUserLoginPort::getCompanyId, bo.getId())); - - hotCompanySafetyManagerMapper.deleteCompanyHeadByPhone(bo.getId(), oldMobile); - } + cleanupOldResponsibleData(bo.getId(), oldMobile); } // 2. 添加新负责人:创建用户(如果不存在)、赋予权限、添加安全管理人员记录 @@ -512,6 +517,39 @@ public class SysCompanyServiceImpl implements ISysCompanyService { return flag; } + private void cleanupOldResponsibleData(Long companyId, String oldMobile) { + if (companyId == null || StrUtil.isBlank(oldMobile)) { + return; + } + SysUser oldUser = sysUserMapper.selectOne(new LambdaQueryWrapper().eq(SysUser::getPhonenumber, oldMobile)); + Long oldUserId = oldUser != null ? oldUser.getUserId() : null; + if (oldUserId != null) { + sysUserRoleMapper.delete(new LambdaQueryWrapper() + .eq(SysUserRole::getUserId, oldUserId) + .eq(SysUserRole::getLoginPort, ENTERPRISE_PORT) + .eq(SysUserRole::getCompanyId, companyId)); + sysUserLoginPortMapper.delete(new LambdaQueryWrapper() + .eq(SysUserLoginPort::getUserId, oldUserId) + .eq(SysUserLoginPort::getLoginPort, ENTERPRISE_PORT) + .eq(SysUserLoginPort::getCompanyId, companyId)); + } + LambdaQueryWrapper wrapper = Wrappers.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); + } + } + /** * 保存前的数据校验 */ diff --git a/src/main/java/com/hotwj/platform/resourceManagement/companySafetyManager/service/impl/HotCompanySafetyManagerServiceImpl.java b/src/main/java/com/hotwj/platform/resourceManagement/companySafetyManager/service/impl/HotCompanySafetyManagerServiceImpl.java index 105a3ae..518ba7f 100644 --- a/src/main/java/com/hotwj/platform/resourceManagement/companySafetyManager/service/impl/HotCompanySafetyManagerServiceImpl.java +++ b/src/main/java/com/hotwj/platform/resourceManagement/companySafetyManager/service/impl/HotCompanySafetyManagerServiceImpl.java @@ -451,6 +451,8 @@ public class HotCompanySafetyManagerServiceImpl implements IHotCompanySafetyMana userId = sysUser.getUserId(); } + validateManagerUniqueness(add.getCompanyId(), add.getPhone(), userId, null); + // 初始化或更新子密码(特定企业的登录密码) String subPassword; if (StringUtils.isNotBlank(add.getLoginPassword())) { @@ -521,6 +523,9 @@ public class HotCompanySafetyManagerServiceImpl implements IHotCompanySafetyMana if (before == null) { 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()); 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 phoneWrapper = Wrappers.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 userWrapper = Wrappers.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) { SysUser sysUser = findUserByPhone(before.getPhone()); if (sysUser == null) { @@ -689,7 +727,7 @@ public class HotCompanySafetyManagerServiceImpl implements IHotCompanySafetyMana userService.resetUserPwd(sysUser.getUserId(), BCrypt.hashpw(newPwd), before.getCompanyId()); // 不需要单独更新 sysUser 密码,resetUserPwd 已经处理 - return false; + return false; } private boolean handlePhoneChange(HotCompanySafetyManager update, HotCompanySafetyManager before, SysUser sysUser) {