@Override @Transactional(rollbackFor = Exception.class) public void…
- User
@Override @Transactional(rollbackFor = Exception.class) public void saveSubAccountTenantBinding(SaveSubAccountTenantBindingParam param) { if (param == null || param.getUserId() == null || param.getMasterUserId() == null) { throw new UserSideException("用户ID不能为空"); } Long userId = param.getUserId(); Long masterUserId = param.getMasterUserId(); List tenantIds = param.getTenantIds(); Boolean allTenantVisible = param.getAllTenantVisible(); if (userId.equals(masterUserId)) { throw new UserSideException("主账号和子账号不能为同一个账号"); } // 若非全选租户,且租户列表仍为null,什么都不改变 if (tenantIds == null && Boolean.FALSE.equals(allTenantVisible)) { log.info("未指定 tenantIds,保持子账号绑定关系不变。userId={}, masterUserId={}", userId, masterUserId); return; } // 可加入对userId和masterUserId的userInfo的AccountType是否为子/主账号和UserAccount之间是否存在后缀关联CHECK CustomerSideUserBasicInfoDTO userInfo = userService.getCustomerSideUserBasicInfo(userId);
List<PlatAccountBindTenant> existingBindings = platAccountBindTenantRepository.getListByUserId(userId); Map<Long, PlatAccountBindTenant> existingBindingMap = existingBindings.stream() .collect(Collectors.toMap(PlatAccountBindTenant::getTenantId, item -> item)); // 子账号只能绑定主账号已绑定的租户 List<PlatAccountBindTenant> masterBindings = platAccountBindTenantRepository.getListByUserId(masterUserId); Map<Long, PlatAccountBindTenant> masterBindingMap = masterBindings.stream() .collect(Collectors.toMap(PlatAccountBindTenant::getTenantId, item -> item)); if (masterBindingMap.isEmpty()) { log.warn("主账号无绑定租户,无法进行子账号绑定,masterUserId={}", masterUserId); throw new UserSideException("主账号未绑定任何租户,无法进行子账号绑定"); } List<PlatAccountBindTenant> addList = new ArrayList<>(); // 如果是全选,则使用主账号的所有租户ID if (Boolean.TRUE.equals(allTenantVisible)) { log.info("全选租户,将绑定主账号的所有租户关系。userId={}, masterUserId={}", userId, masterUserId); tenantIds = new ArrayList<>(masterBindingMap.keySet()); } if (CollectionUtils.isNotEmpty(tenantIds)) { for (Long tenantId : tenantIds) { if (!masterBindingMap.containsKey(tenantId)) { log.warn("主账号未绑定租户: {}, 跳过此租户", tenantId); continue; } // 子账号已有此租户的绑定关系,无视 if (existingBindingMap.containsKey(tenantId)) { existingBindingMap.remove(tenantId); } else { // 子账号没有此租户的绑定关系,则新增 PlatAccountBindTenant masterBinding = masterBindingMap.get(tenantId); PlatAccountBindTenant newBinding = new PlatAccountBindTenant(); newBinding.setUserId(userId); newBinding.setUserAccount(userInfo.getUserAccount()); newBinding.setTenantId(masterBinding.getTenantId()); newBinding.setTenantName(masterBinding.getTenantName()); newBinding.setTenantCode(masterBinding.getTenantCode()); newBinding.setCustomerId(masterBinding.getCustomerId()); newBinding.setCustomerName(masterBinding.getCustomerName()); newBinding.setCustomerCode(masterBinding.getCustomerCode()); addList.add(newBinding); } } } // 批量保存新增 if (CollectionUtils.isNotEmpty(addList)) { platAccountBindTenantRepository.saveList(addList); } // 剩下的不是已有也不是新增,则删除 if (!existingBindingMap.isEmpty()) { List<Long> deleteIds = existingBindingMap.values().stream() .map(PlatAccountBindTenant::getId) .collect(Collectors.toList()); platAccountBindTenantRepository.removeListByIds(deleteIds); } }你能帮我检查一下我的代码的逻辑吗
Assistant
当然可以!让我们一步步检查你的代码逻辑,确保它符合预期的功能。
代码逻辑检查
-
参数检查
- 检查
param是否为null或userId和masterUserId是否为null。如果是,则抛出UserSideException。 - 检查
userId和masterUserId是否相等。如果相等,则抛出UserSideException。
- 检查
-
租户ID和全选标志检查
- 如果
tenantIds为null且allTenantVisible为false,则记录日志并返回,不做任何修改。
- 如果
-
获取用户信息和现有绑定关系
- 获取
userId对应的用户基本信息。 - 获取
userId和masterUserId对应的现有绑定关系,并将它们存储在Map中。
- 获取
-
主账号绑定关系检查
- 检查主账号是否有绑定的租户。如果没有,则抛出
UserSideException。
- 检查主账号是否有绑定的租户。如果没有,则抛出
-
处理绑定关系
- 如果
allTenantVisible为true,则使用主账号的所有租户ID。 - 遍历
tenantIds,检查每个租户ID是否在主账号的绑定关系中。如果不在,则记录日志并跳过。如果在,则检查子账号是否已有该租户的绑定关系。如果没有,则创建新的绑定关系并添加到addList中。
- 如果
-
保存新增绑定关系
- 如果
addList不为空,则批量保存新增的绑定关系。
- 如果
-
删除不再需要的绑定关系
- 如果
existingBindingMap不为空,则删除不再需要的绑定关系。
- 如果
代码改进建议
-
异常处理
- 可以在方法开头添加更详细的参数检查,以确保所有必要的参数都已提供。
-
日志记录
- 可以在关键步骤添加更多的日志,以便于调试和追踪。
-
代码重构
- 可以将一些重复的代码提取到单独的方法中,以提高代码的可读性和可维护性。
改进后的代码示例
@Override @Transactional(rollbackFor = Exception.class) public void saveSubAccountTenantBinding(SaveSubAccountTenantBindingParam param) { if (param == null) { throw new UserSideException("参数不能为空"); } Long userId = param.getUserId(); Long masterUserId = param.getMasterUserId(); if (userId == null || masterUserId == null) { throw new UserSideException("用户ID不能为空"); } if (userId.equals(masterUserId)) { throw new UserSideException("主账号和子账号不能为同一个账号"); } List<Long> tenantIds = param.getTenantIds(); Boolean allTenantVisible = param.getAllTenantVisible(); if (tenantIds == null && Boolean.FALSE.equals(allTenantVisible)) { log.info("未指定 tenantIds,保持子账号绑定关系不变。userId={}, masterUserId={}", userId, masterUserId); return; } CustomerSideUserBasicInfoDTO userInfo = userService.getCustomerSideUserBasicInfo(userId); if (userInfo == null) { throw new UserSideException("用户信息不存在"); } List<PlatAccountBindTenant> existingBindings = platAccountBindTenantRepository.getListByUserId(userId); Map<Long, PlatAccountBindTenant> existingBindingMap = existingBindings.stream() .collect(Collectors.toMap(PlatAccountBindTenant::getTenantId, item -> item)); List<PlatAccountBindTenant> masterBindings = platAccountBindTenantRepository.getListByUserId(masterUserId); Map<Long, PlatAccountBindTenant> masterBindingMap = masterBindings.stream() .collect(Collectors.toMap(PlatAccountBindTenant::getTenantId, item -> item)); if (masterBindingMap.isEmpty()) { log.warn("主账号无绑定租户,无法进行子账号绑定,masterUserId={}", masterUserId); throw new UserSideException("主账号未绑定任何租户,无法进行子账号绑定"); } List<PlatAccountBindTenant> addList = new ArrayList<>(); if (Boolean.TRUE.equals(allTenantVisible)) { log.info("全选租户,将绑定主账号的所有租户关系。userId={}, masterUserId={}", userId, masterUserId); tenantIds = new ArrayList<>(masterBindingMap.keySet()); } if (CollectionUtils.isNotEmpty(tenantIds)) { for (Long tenantId : tenantIds) { if (!masterBindingMap.containsKey(tenantId)) { log.warn("主账号未绑定租户: {}, 跳过此租户", tenantId); continue; } if (existingBindingMap.containsKey(tenantId)) { existingBindingMap.remove(tenantId); } else { PlatAccountBindTenant masterBinding = masterBindingMap.get(tenantId); PlatAccountBindTenant newBinding = createNewBinding(userId, userInfo, masterBinding); addList.add(newBinding); } } } if (CollectionUtils.isNotEmpty(addList)) { platAccountBindTenantRepository.saveList(addList); } if (!existingBindingMap.isEmpty()) { List<Long> deleteIds = existingBindingMap.values().stream() .map(PlatAccountBindTenant::getId) .collect(Collectors.toList()); platAccountBindTenantRepository.removeListByIds(deleteIds); } } private PlatAccountBindTenant createNewBinding(Long userId, CustomerSideUserBasicInfoDTO userInfo, PlatAccountBindTenant masterBinding) { PlatAccountBindTenant newBinding = new PlatAccountBindTenant(); newBinding.setUserId(userId); newBinding.setUserAccount(userInfo.getUserAccount()); newBinding.setTenantId(masterBinding.getTenantId()); newBinding.setTenantName(masterBinding.getTenantName()); newBinding.setTenantCode(masterBinding.getTenantCode()); newBinding.setCustomerId(masterBinding.getCustomerId()); newBinding.setCustomerName(masterBinding.getCustomerName()); newBinding.setCustomerCode(masterBinding.getCustomerCode()); return newBinding; }总结
这段代码的逻辑基本正确,但可以通过添加更多的异常处理和日志记录来提高代码的健壮性和可维护性。同时,将一些重复的代码提取到单独的方法中可以提高代码的可读性。
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.