组织机构删除(组织或其下级组织存在用户则拒绝删除并返回错误信息)
This commit is contained in:
parent
ca1a0254b3
commit
25e66a64d5
@ -14,6 +14,8 @@ import com.yfd.platform.system.service.ISysOrganizationService;
|
||||
import com.yfd.platform.system.service.IUserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
@ -180,29 +182,95 @@ public class SysOrganizationController {
|
||||
/***********************************
|
||||
* 用途说明:根据id删除系统组织框架
|
||||
* 参数说明
|
||||
* id 系统组织框架id
|
||||
* 返回值说明: 是否删除成功
|
||||
* id 组织ID(支持逗号分隔批量)
|
||||
* 返回值说明: 成功返回 success;若组织或其下级组织存在用户则拒绝删除并返回错误信息(Fail-Fast:批量中只要有一个组织存在用户则全部不删除)
|
||||
***********************************/
|
||||
@Log(module = "系统组织框架", value = "根据ID删除企业或者部门!")
|
||||
@PostMapping("/deleteById")
|
||||
@Operation(summary = "根据id删除系统组织框架")
|
||||
@ResponseBody
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ResponseResult deleteById(@RequestParam String id) {
|
||||
if (StringUtils.isBlank(id)) {
|
||||
return ResponseResult.success();
|
||||
}
|
||||
String[] orgIds = id.split(",");
|
||||
List<String> uniqueOrgIds = new ArrayList<>();
|
||||
for (String orgId : orgIds) {
|
||||
if (StringUtils.isBlank(orgId)) {
|
||||
continue;
|
||||
}
|
||||
String trimmed = orgId.trim();
|
||||
if (StringUtils.isBlank(trimmed)) {
|
||||
continue;
|
||||
}
|
||||
if (!uniqueOrgIds.contains(trimmed)) {
|
||||
uniqueOrgIds.add(trimmed);
|
||||
}
|
||||
}
|
||||
if (uniqueOrgIds.isEmpty()) {
|
||||
return ResponseResult.success();
|
||||
}
|
||||
Map<String, List<String>> childIdsByOrgId = new LinkedHashMap<>();
|
||||
for (String orgId : uniqueOrgIds) {
|
||||
LambdaQueryWrapper<SysOrganization> queryWrapper =
|
||||
new LambdaQueryWrapper<>();
|
||||
List<SysOrganization> list =
|
||||
organizationService.list(queryWrapper.eq(SysOrganization::getParentid, orgId));
|
||||
List<String> ids =
|
||||
list.stream().map(SysOrganization::getId).collect(Collectors.toList());
|
||||
boolean isOk = organizationService.removeById(orgId);
|
||||
if (!isOk) {
|
||||
childIdsByOrgId.put(orgId, ids);
|
||||
}
|
||||
Map<String, Long> blocks = new LinkedHashMap<>();
|
||||
for (String orgId : uniqueOrgIds) {
|
||||
SysOrganization org = organizationService.getById(orgId);
|
||||
if (org == null) {
|
||||
continue;
|
||||
}
|
||||
List<String> scopeIds = new ArrayList<>();
|
||||
scopeIds.add(orgId);
|
||||
List<String> childIds = childIdsByOrgId.get(orgId);
|
||||
if (childIds != null && !childIds.isEmpty()) {
|
||||
scopeIds.addAll(childIds);
|
||||
}
|
||||
long userCount =
|
||||
userService.count(new LambdaQueryWrapper<SysUser>().in(
|
||||
SysUser::getOrgid, scopeIds));
|
||||
if (userCount > 0) {
|
||||
blocks.put(orgId, userCount);
|
||||
}
|
||||
}
|
||||
if (!blocks.isEmpty()) {
|
||||
StringBuilder message = new StringBuilder();
|
||||
message.append("操作无法完成:该组织及其下级组织有用户关联,请先解绑用户:");
|
||||
int i = 0;
|
||||
for (Map.Entry<String, Long> entry : blocks.entrySet()) {
|
||||
String orgId = entry.getKey();
|
||||
SysOrganization org = organizationService.getById(orgId);
|
||||
if (org == null) {
|
||||
continue;
|
||||
}
|
||||
if (i > 0) {
|
||||
message.append(";");
|
||||
}
|
||||
message.append(org.getOrgname()).append("(组织ID=").append(orgId)
|
||||
.append(") ").append(entry.getValue()).append("人");
|
||||
i++;
|
||||
}
|
||||
throw new RuntimeException(message.toString());
|
||||
}
|
||||
for (String orgId : uniqueOrgIds) {
|
||||
List<String> ids = childIdsByOrgId.get(orgId);
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
ids = Collections.emptyList();
|
||||
}
|
||||
for (String oid : ids) {
|
||||
organizationService.removeById(oid);
|
||||
}
|
||||
boolean isOk = organizationService.removeById(orgId);
|
||||
if (!isOk) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return ResponseResult.success();
|
||||
}
|
||||
|
||||
@ -116,7 +116,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
||||
roleMapper.countRoleUsersByRoleIds(roleIds);
|
||||
if (blocks != null && !blocks.isEmpty()) {
|
||||
StringBuilder message = new StringBuilder();
|
||||
message.append("以下角色仍绑定用户,无法物理删除,请先解绑或转移用户:");
|
||||
message.append("操作无法完成:以下角色仍绑定用户,请先解绑用户:");
|
||||
for (int i = 0; i < blocks.size(); i++) {
|
||||
Map<String, Object> block = blocks.get(i);
|
||||
Object roleIdObj = block.get("roleid");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user