feat(department): 增加部门搜索显示所有选项及级联禁用功能

- 为部门搜索接口添加showAll参数,支持显示全部部门包括禁用部门
- 部门服务层searchSimple方法新增showAll参数,支持过滤禁用状态
- 在部门启用状态更新时,新增逻辑批量禁用所有子部门及其关联用户
- 部门数据访问层实现中,添加disableWithChildren方法实现对子部门和用户的级联禁用
- 部门树形结构查询和简单信息查询中,增加对showAll参数的支持,控制是否过滤禁用数据
- 对方法调用链进行参数调整,确保新增showAll参数正确传递和处理
- 保持部门列表及子部门查询结果按ID升序排序,提高结果稳定性
This commit is contained in:
曹鹏飞 2026-05-27 09:32:37 +08:00
parent 73c2ca6440
commit a64278010a
4 changed files with 86 additions and 14 deletions

View File

@ -84,9 +84,11 @@ public class DepartmentController extends BaseController {
/**
* 搜索部门返回基本信息
* @param key 部门名称或代码
* @param showAll 是否显示所有部门默认false不显示禁用的部门
*/
@GetMapping("searchSimple")
public ApiResult<List<DepartmentSimpleVO>> searchSimple(@RequestParam(required = false) String key) {
return ApiResult.success(departmentControllerService.searchSimple(key));
public ApiResult<List<DepartmentSimpleVO>> searchSimple(@RequestParam(required = false) String key
, @RequestParam(required = false,defaultValue = "false") Boolean showAll) {
return ApiResult.success(departmentControllerService.searchSimple(key,showAll));
}
}

View File

@ -48,6 +48,7 @@ public class DepartmentControllerService {
deptService.add(request);
}
@Transactional
public void update(DepartmentUpdateQO request) {
Department department = deptService.getById(request.getId());
VUtil.trueThrowBusinessError(Objects.isNull(department)).throwMessage("部门不存在");
@ -118,18 +119,24 @@ public class DepartmentControllerService {
}
}
public List<DepartmentSimpleVO> searchSimple(String key) {
return deptService.searchSimple(key);
public List<DepartmentSimpleVO> searchSimple(String key,Boolean showAll) {
return deptService.searchSimple(key,showAll);
}
@Transactional
public void enable(@Valid EnableQO request) {
Department department = deptService.getById(request.getId());
VUtil.trueThrowBusinessError(Objects.isNull(department)).throwMessage("部门不存在");
VUtil.trueThrowBusinessError(Objects.equals(department.getSource(), 1)).throwMessage("不能禁用启用从AD域同步的部门信息");
deptService.updateById(new Department()
.setId(request.getId())
.setEnable(request.getEnable())
.setUpdateBy(UserUtil.getUserName())
.setUpdateTime(LocalDateTime.now()));
// 如果修改为禁用状态级联禁用所有子部门及对应用户
if (Boolean.FALSE.equals(request.getEnable())) {
deptService.disableWithChildren(request.getId());
}
}
public PageData<DepartmentVO> search(DepartmentSearchQO request) {

View File

@ -32,9 +32,11 @@ public interface IDepartmentService extends IService<Department> {
void delete(Long id);
List<DepartmentSimpleVO> searchSimple(String key);
List<DepartmentSimpleVO> searchSimple(String key,Boolean showAll);
PageData<DepartmentVO> search(DepartmentSearchQO request);
Set<Long> getWithChildren(Long deptId);
void disableWithChildren(Long deptId);
}

View File

@ -11,12 +11,17 @@ import com.nflg.wms.common.pojo.qo.DepartmentSearchQO;
import com.nflg.wms.common.pojo.qo.DepartmentUpdateQO;
import com.nflg.wms.common.pojo.vo.DepartmentSimpleVO;
import com.nflg.wms.common.pojo.vo.DepartmentVO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.nflg.wms.common.util.UserUtil;
import com.nflg.wms.common.util.VUtil;
import com.nflg.wms.repository.entity.Department;
import com.nflg.wms.repository.entity.User;
import com.nflg.wms.repository.entity.UserInterior;
import com.nflg.wms.repository.mapper.DepartmentMapper;
import com.nflg.wms.repository.service.IAuditLogService;
import com.nflg.wms.repository.service.IDepartmentService;
import com.nflg.wms.repository.service.IUserInteriorService;
import com.nflg.wms.repository.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -39,6 +44,12 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
@Resource
private IAuditLogService auditLogService;
@Resource
private IUserService userService;
@Resource
private IUserInteriorService userInteriorService;
@Transactional
@Override
public void add(DepartmentAddQO request) {
@ -84,6 +95,11 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
// .setUpdateTime(LocalDateTime.now());
updateById(dept2);
auditLogService.addUpdate(Department.class, dept1, dept2, UserUtil.getUserName());
// 如果修改为禁用状态级联禁用所有子部门及对应用户
if (Boolean.FALSE.equals(request.getEnable())) {
disableWithChildren(dept1.getId());
}
}
@Override
@ -103,11 +119,11 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
}
@Override
public List<DepartmentSimpleVO> searchSimple(String key) {
public List<DepartmentSimpleVO> searchSimple(String key, Boolean showAll) {
if (StrUtil.isBlank(key)) {
return getAllSimple();
return getAllSimple(showAll);
} else {
return searchSimpleByKey(key);
return searchSimpleByKey(key, showAll);
}
}
@ -124,6 +140,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
List<Department> departments = lambdaQuery()
.like(StrUtil.isNotBlank(request.getName()), Department::getName, request.getName())
.like(StrUtil.isNotBlank(request.getNo()), Department::getSourceId, request.getNo())
.orderByAsc(Department::getId)
.list();
if (CollectionUtil.isEmpty(departments)) {
return new PageData<>();
@ -139,6 +156,42 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
.setItems(datas);
}
@Transactional
@Override
public void disableWithChildren(Long deptId) {
// 获取当前部门及所有子部门ID
Set<Long> allDeptIds = getWithChildren(deptId);
Set<Long> childDeptIds = new HashSet<>(allDeptIds);
childDeptIds.remove(deptId);
// 批量禁用子部门
if (CollectionUtil.isNotEmpty(childDeptIds)) {
lambdaUpdate()
.set(Department::getEnable, false)
.in(Department::getId, childDeptIds)
.update();
}
// 查询所有相关部门下的用户ID
List<Long> userIds = userInteriorService.lambdaQuery()
.select(UserInterior::getUserId)
.in(UserInterior::getDeptId, allDeptIds)
.list()
.stream()
.map(UserInterior::getUserId)
.distinct()
.collect(Collectors.toList());
// 批量禁用用户
if (CollectionUtil.isNotEmpty(userIds)) {
userService.update(
new LambdaUpdateWrapper<User>()
.set(User::getState, 2)
.in(User::getId, userIds)
);
}
}
@Override
public Set<Long> getWithChildren(Long deptId) {
Set<Long> ids = lambdaQuery()
@ -155,9 +208,10 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
return ids;
}
private List<DepartmentSimpleVO> searchSimpleByKey(String key) {
private List<DepartmentSimpleVO> searchSimpleByKey(String key, Boolean showAll) {
List<Department> departments = lambdaQuery()
.select(Department::getId, Department::getParentId, Department::getName)
.eq(!Objects.equals(showAll, true), Department::getEnable, true)
.like(Department::getName, key)
.list();
if (CollectionUtil.isEmpty(departments)) {
@ -213,6 +267,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
List<Department> departments = lambdaQuery()
.eq(Objects.isNull(request.getDeptId()), Department::getParentId, 0L)
.eq(Objects.nonNull(request.getDeptId()), Department::getId, request.getDeptId())
.orderByAsc(Department::getId)
.list();
List<DepartmentVO> datas = Convert.toList(DepartmentVO.class, departments);
datas = datas.stream().skip((long) (request.getPage() - 1) * request.getPageSize()).limit(request.getPageSize()).toList();
@ -229,6 +284,7 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
datas.forEach(dept -> {
List<Department> departments = lambdaQuery()
.eq(Department::getParentId, dept.getId())
.orderByAsc(Department::getId)
.list();
if (CollectionUtil.isNotEmpty(departments)) {
List<DepartmentVO> children = Convert.toList(DepartmentVO.class, departments);
@ -238,24 +294,29 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
});
}
private List<DepartmentSimpleVO> getAllSimple() {
private List<DepartmentSimpleVO> getAllSimple(Boolean showAll) {
List<Department> departments = lambdaQuery()
.eq(Department::getParentId, 0L)
.eq(Department::getEnable, true)
.eq(!Objects.equals(showAll, true), Department::getEnable, true)
.orderByAsc(Department::getId)
.list();
List<DepartmentSimpleVO> datas = Convert.toList(DepartmentSimpleVO.class, departments);
bindSimpleChildren(datas);
bindSimpleChildren(datas, showAll);
return datas;
}
private void bindSimpleChildren(List<DepartmentSimpleVO> datas) {
private void bindSimpleChildren(List<DepartmentSimpleVO> datas,Boolean showAll) {
datas.forEach(dept -> {
List<Department> departments = lambdaQuery()
.eq(Department::getParentId, dept.getId())
.eq(!Objects.equals(showAll, true), Department::getEnable, true)
.orderByAsc(Department::getId)
.list();
if (CollectionUtil.isNotEmpty(departments)) {
List<DepartmentSimpleVO> children = Convert.toList(DepartmentSimpleVO.class, departments);
dept.setChildren(children);
this.bindSimpleChildren(children);
this.bindSimpleChildren(children, showAll);
}
});
}