|
|
@@ -7,13 +7,18 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import shop.alien.entity.result.R;
|
|
|
+import shop.alien.entity.store.LifeSys;
|
|
|
import shop.alien.entity.store.LifeSysDept;
|
|
|
import shop.alien.entity.store.dto.LifeSysDeptAddDto;
|
|
|
import shop.alien.entity.store.dto.LifeSysDeptSortDto;
|
|
|
import shop.alien.entity.store.dto.LifeSysDeptUpdateDto;
|
|
|
import shop.alien.entity.store.vo.LifeSysDeptVo;
|
|
|
+import shop.alien.entity.store.vo.LifeSysVo;
|
|
|
import shop.alien.mapper.LifeSysDeptMapper;
|
|
|
+import shop.alien.mapper.LifeSysMapper;
|
|
|
import shop.alien.store.service.LifeSysDeptService;
|
|
|
|
|
|
import java.util.*;
|
|
|
@@ -32,6 +37,8 @@ public class LifeSysDeptServiceImpl implements LifeSysDeptService {
|
|
|
|
|
|
private final LifeSysDeptMapper lifeSysDeptMapper;
|
|
|
|
|
|
+ private final LifeSysMapper lifeSysMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public R<LifeSysDept> addDept(LifeSysDeptAddDto addDto) {
|
|
|
log.info("LifeSysDeptServiceImpl.addDept?addDto={}", addDto);
|
|
|
@@ -496,5 +503,97 @@ public class LifeSysDeptServiceImpl implements LifeSysDeptService {
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<List<LifeSysVo>> viewMembers(Integer pageNum, Integer pageSize, Long deptId) {
|
|
|
+ log.info("LifeSysDeptServiceImpl.viewMembers?pageNum={}, pageSize={}, deptId={}", pageNum, pageSize, deptId);
|
|
|
+
|
|
|
+ // 参数校验
|
|
|
+ if (deptId == null) {
|
|
|
+ log.warn("查看部门成员失败:部门ID为空");
|
|
|
+ return R.fail("部门ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验部门是否存在
|
|
|
+ LifeSysDept dept = lifeSysDeptMapper.selectById(deptId);
|
|
|
+ if (dept == null) {
|
|
|
+ return R.fail("部门不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查部门是否已被删除
|
|
|
+ if (!"0".equals(dept.getDelFlag())) {
|
|
|
+ return R.fail("该部门已被删除");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 收集当前部门ID和所有子部门ID
|
|
|
+ Set<Long> deptIdSet = new HashSet<>();
|
|
|
+ deptIdSet.add(deptId);
|
|
|
+
|
|
|
+ // 递归收集所有子部门ID
|
|
|
+ collectChildDeptIds(deptId, deptIdSet);
|
|
|
+
|
|
|
+ log.debug("查询部门成员,部门ID={},包含子部门数量={}", deptId, deptIdSet.size() - 1);
|
|
|
+
|
|
|
+ // 如果没有任何部门(理论上不会发生),直接返回空列表
|
|
|
+ if (deptIdSet.isEmpty()) {
|
|
|
+ return R.data(new ArrayList<>(), "查询成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建查询条件:查询当前部门及所有子部门下的所有用户
|
|
|
+ QueryWrapper<LifeSys> voQueryWrapper = new QueryWrapper<>();
|
|
|
+ // 将部门ID集合转换为String集合,因为department_id是String类型
|
|
|
+ List<String> deptIdStrList = new ArrayList<>();
|
|
|
+ for (Long id : deptIdSet) {
|
|
|
+ deptIdStrList.add(String.valueOf(id));
|
|
|
+ }
|
|
|
+ voQueryWrapper.in("ls.department_id", deptIdStrList);
|
|
|
+ voQueryWrapper.eq("ls.delete_flag", 0);
|
|
|
+ voQueryWrapper.eq("ls.status", 1);
|
|
|
+ voQueryWrapper.orderByDesc("ls.created_time");
|
|
|
+
|
|
|
+ // 先查询用户数量,用于分页
|
|
|
+ IPage<LifeSysVo> voList = lifeSysMapper.selectPageAndRoleName(new Page<>(pageNum, pageSize), voQueryWrapper);
|
|
|
+ if (voList == null || voList.getRecords().isEmpty()) {
|
|
|
+ log.info("查看部门成员成功,部门ID={}, 部门名称={}, 成员数量=0", deptId, dept.getDeptName());
|
|
|
+ return R.data(new ArrayList<>(), "查询成功");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return R.data(voList.getRecords(), "查询成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("查看部门成员失败,部门ID={}", deptId, e);
|
|
|
+ return R.fail("查询部门成员失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归收集所有子部门ID
|
|
|
+ *
|
|
|
+ * @param parentDeptId 父部门ID
|
|
|
+ * @param deptIdSet 部门ID集合(用于收集结果)
|
|
|
+ */
|
|
|
+ private void collectChildDeptIds(Long parentDeptId, Set<Long> deptIdSet) {
|
|
|
+ if (parentDeptId == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询所有子部门
|
|
|
+ QueryWrapper<LifeSysDept> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("parent_id", parentDeptId);
|
|
|
+ queryWrapper.eq("del_flag", "0"); // 只查询未删除的部门
|
|
|
+ List<LifeSysDept> childDepts = lifeSysDeptMapper.selectList(queryWrapper);
|
|
|
+
|
|
|
+ if (childDepts != null && !childDepts.isEmpty()) {
|
|
|
+ for (LifeSysDept childDept : childDepts) {
|
|
|
+ Long childDeptId = childDept.getDeptId();
|
|
|
+ if (childDeptId != null && !deptIdSet.contains(childDeptId)) {
|
|
|
+ deptIdSet.add(childDeptId);
|
|
|
+ // 递归查询子部门的子部门
|
|
|
+ collectChildDeptIds(childDeptId, deptIdSet);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|