Commit 61c14e2c authored by 施翔轲's avatar 施翔轲

新增组织管理删除部门则逻辑删除该部门下所有子用户和子部门

parent a9189dd4
package com.dsk.web.controller.system;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.dsk.common.annotation.Log;
import com.dsk.common.constant.UserConstants;
......@@ -9,8 +10,10 @@ import com.dsk.common.core.domain.R;
import com.dsk.system.domain.SysDept;
import com.dsk.common.enums.BusinessType;
import com.dsk.common.utils.StringUtils;
import com.dsk.system.domain.vo.SysDeptVo;
import com.dsk.system.service.ISysDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
......@@ -34,9 +37,13 @@ public class SysDeptController extends BaseController {
*/
@SaCheckPermission("system:dept:list")
@GetMapping("/list")
public R<List<SysDept>> list(SysDept dept) {
public R<List<SysDeptVo>> list(SysDept dept) {
List<SysDept> depts = deptService.selectDeptList(dept);
return R.ok(depts);
List<SysDeptVo> deptVos = BeanUtil.copyToList(depts, SysDeptVo.class);
deptVos.forEach(sysDeptVo -> {
sysDeptVo.setExistUsers(deptService.hasChildByDeptId(sysDeptVo.getDeptId()));
});
return R.ok(deptVos);
}
/**
......@@ -106,13 +113,8 @@ public class SysDeptController extends BaseController {
@SaCheckPermission("system:dept:remove")
@Log(title = "部门管理", businessType = BusinessType.DELETE)
@DeleteMapping("/{deptId}")
@Transactional
public R<Void> remove(@PathVariable Long deptId) {
if (deptService.hasChildByDeptId(deptId)) {
return R.warn("存在下级部门,不允许删除");
}
if (deptService.checkDeptExistUser(deptId)) {
return R.warn("部门存在用户,不允许删除");
}
deptService.checkDeptDataScope(deptId);
return toAjax(deptService.deleteDeptById(deptId));
}
......
package com.dsk.system.domain.vo;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.dsk.common.annotation.ExcelDictFormat;
import com.dsk.common.convert.ExcelDictConvert;
import com.dsk.common.tenant.core.TenantEntity;
import com.dsk.system.domain.SysDept;
import com.dsk.system.domain.SysTenantPackage;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
/**
* 部门表 sys_dept
*
* @author Lion Li
*/
@Data
@ExcelIgnoreUnannotated
@AutoMapper(target = SysDept.class)
public class SysDeptVo implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 部门ID
*/
@ExcelProperty(value = "部门id")
private Long deptId;
/**
* 父部门ID
*/
@ExcelProperty(value = "父部门id")
private Long parentId;
/**
* 部门名称
*/
@ExcelProperty(value = "部门名称")
private String deptName;
/**
* 显示顺序
*/
@ExcelProperty(value = "显示顺序")
private Integer orderNum;
/**
* 负责人
*/
@ExcelProperty(value = "负责人")
private String leader;
/**
* 联系电话
*/
@ExcelProperty(value = "联系电话")
private String phone;
/**
* 邮箱
*/
@ExcelProperty(value = "邮箱")
private String email;
/**
* 部门状态:0正常,1停用
*/
@ExcelProperty(value = "部门状态", converter = ExcelDictConvert.class)
@ExcelDictFormat(readConverterExp = "0=正常,1=停用")
private String status;
/**
* 删除标志(0代表存在 2代表删除)
*/
@ExcelProperty(value = "删除标志", converter = ExcelDictConvert.class)
@ExcelDictFormat(readConverterExp = "0=存在,2=删除")
private String delFlag;
/**
* 祖级列表
*/
@ExcelProperty(value = "祖级列表")
private String ancestors;
private String parentName;
/**
* 该部门是否存在用户(true代表存在 false代表不存在)
*/
@ExcelProperty(value = "该部门是否存在用户", converter = ExcelDictConvert.class)
@ExcelDictFormat(readConverterExp = "true=存在,false=删除")
private Boolean existUsers;
}
......@@ -31,6 +31,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* 部门管理 服务实现
......@@ -55,12 +56,12 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
public List<SysDept> selectDeptList(SysDept dept) {
LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<>();
lqw.eq(SysDept::getDelFlag, "0")
.eq(ObjectUtil.isNotNull(dept.getDeptId()), SysDept::getDeptId, dept.getDeptId())
.eq(ObjectUtil.isNotNull(dept.getParentId()), SysDept::getParentId, dept.getParentId())
.like(StringUtils.isNotBlank(dept.getDeptName()), SysDept::getDeptName, dept.getDeptName())
.eq(StringUtils.isNotBlank(dept.getStatus()), SysDept::getStatus, dept.getStatus())
.orderByAsc(SysDept::getParentId)
.orderByAsc(SysDept::getOrderNum);
.eq(ObjectUtil.isNotNull(dept.getDeptId()), SysDept::getDeptId, dept.getDeptId())
.eq(ObjectUtil.isNotNull(dept.getParentId()), SysDept::getParentId, dept.getParentId())
.like(StringUtils.isNotBlank(dept.getDeptName()), SysDept::getDeptName, dept.getDeptName())
.eq(StringUtils.isNotBlank(dept.getStatus()), SysDept::getStatus, dept.getStatus())
.orderByAsc(SysDept::getParentId)
.orderByAsc(SysDept::getOrderNum);
return baseMapper.selectDeptList(lqw);
}
......@@ -88,10 +89,10 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
return CollUtil.newArrayList();
}
return TreeBuildUtils.build(depts, (dept, tree) ->
tree.setId(dept.getDeptId())
.setParentId(dept.getParentId())
.setName(dept.getDeptName())
.setWeight(dept.getOrderNum()));
tree.setId(dept.getDeptId())
.setParentId(dept.getParentId())
.setName(dept.getDeptName())
.setWeight(dept.getOrderNum()));
}
/**
......@@ -120,7 +121,7 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
return null;
}
SysDept parentDept = baseMapper.selectOne(new LambdaQueryWrapper<SysDept>()
.select(SysDept::getDeptName).eq(SysDept::getDeptId, dept.getParentId()));
.select(SysDept::getDeptName).eq(SysDept::getDeptId, dept.getParentId()));
dept.setParentName(ObjectUtil.isNotNull(parentDept) ? parentDept.getDeptName() : null);
return dept;
}
......@@ -152,8 +153,8 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
@Override
public long selectNormalChildrenDeptById(Long deptId) {
return baseMapper.selectCount(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getStatus, UserConstants.DEPT_NORMAL)
.apply(DataBaseHelper.findInSet(deptId, "ancestors")));
.eq(SysDept::getStatus, UserConstants.DEPT_NORMAL)
.apply(DataBaseHelper.findInSet(deptId, "ancestors")));
}
/**
......@@ -165,7 +166,7 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
@Override
public boolean hasChildByDeptId(Long deptId) {
return baseMapper.exists(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getParentId, deptId));
.eq(SysDept::getParentId, deptId));
}
/**
......@@ -177,7 +178,7 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
@Override
public boolean checkDeptExistUser(Long deptId) {
return userMapper.exists(new LambdaQueryWrapper<SysUser>()
.eq(SysUser::getDeptId, deptId));
.eq(SysUser::getDeptId, deptId));
}
/**
......@@ -189,9 +190,9 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
@Override
public boolean checkDeptNameUnique(SysDept dept) {
boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getDeptName, dept.getDeptName())
.eq(SysDept::getParentId, dept.getParentId())
.ne(ObjectUtil.isNotNull(dept.getDeptId()), SysDept::getDeptId, dept.getDeptId()));
.eq(SysDept::getDeptName, dept.getDeptName())
.eq(SysDept::getParentId, dept.getParentId())
.ne(ObjectUtil.isNotNull(dept.getDeptId()), SysDept::getDeptId, dept.getDeptId()));
return !exist;
}
......@@ -248,7 +249,7 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
}
int result = baseMapper.updateById(dept);
if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
&& !StringUtils.equals(UserConstants.DEPT_NORMAL, dept.getAncestors())) {
&& !StringUtils.equals(UserConstants.DEPT_NORMAL, dept.getAncestors())) {
// 如果该部门是启用状态,则启用该部门的所有上级部门
updateParentDeptStatusNormal(dept);
}
......@@ -264,8 +265,8 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
String ancestors = dept.getAncestors();
Long[] deptIds = Convert.toLongArray(ancestors);
baseMapper.update(null, new LambdaUpdateWrapper<SysDept>()
.set(SysDept::getStatus, UserConstants.DEPT_NORMAL)
.in(SysDept::getDeptId, Arrays.asList(deptIds)));
.set(SysDept::getStatus, UserConstants.DEPT_NORMAL)
.in(SysDept::getDeptId, Arrays.asList(deptIds)));
}
/**
......@@ -277,7 +278,7 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
*/
public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
List<SysDept> children = baseMapper.selectList(new LambdaQueryWrapper<SysDept>()
.apply(DataBaseHelper.findInSet(deptId, "ancestors")));
.apply(DataBaseHelper.findInSet(deptId, "ancestors")));
List<SysDept> list = new ArrayList<>();
for (SysDept child : children) {
SysDept dept = new SysDept();
......@@ -301,7 +302,38 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
@CacheEvict(cacheNames = CacheNames.SYS_DEPT, key = "#deptId")
@Override
public int deleteDeptById(Long deptId) {
//如果有子部门,递归删除部门和用户
if (hasChildByDeptId(deptId)) {
List<SysDept> childDepts = baseMapper.selectDeptList(new LambdaQueryWrapper<SysDept>()
.eq(SysDept::getParentId, deptId));
childDepts.forEach(childDept -> {
//如果该子部门有用户,则删除所有用户
checkDelUsers(childDept.getDeptId());
//递归调用
deleteDeptById(childDept.getDeptId());
});
}
//如果该部门有用户,则删除所有用户
checkDelUsers(deptId);
//删除该部门
return baseMapper.deleteById(deptId);
}
/**
* 判断该部门是否有用户,有则删除所有用户
*
* @param deptId 部门ID
*/
private int checkDelUsers(Long deptId) {
//如果该部门有用户
if (checkDeptExistUser(deptId)) {
List<SysUser> childUsers = userMapper.selectList(new LambdaQueryWrapper<SysUser>()
.eq(true, SysUser::getDeptId, deptId));
List<Long> childUsersIds = childUsers.stream().map(SysUser::getUserId).collect(Collectors.toList());
//删除该子部门下所有用户
return userMapper.deleteBatchIds(childUsersIds);
}
return -1;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment