|
@@ -1,20 +1,144 @@
|
|
|
package com.xy.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.lang.tree.Tree;
|
|
|
+import cn.hutool.core.lang.tree.TreeNode;
|
|
|
+import cn.hutool.core.lang.tree.TreeUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xy.dto.SysDeptDto;
|
|
|
import com.xy.entity.SysDept;
|
|
|
+import com.xy.entity.SysDeptRelation;
|
|
|
import com.xy.mapper.SysDeptMapper;
|
|
|
+import com.xy.service.SysDeptRelationService;
|
|
|
import com.xy.service.SysDeptService;
|
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xy.utils.R;
|
|
|
+import com.xy.utils.SaTokenUtils;
|
|
|
+import com.xy.vo.SysDeptVo;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
-/**
|
|
|
- * <p>
|
|
|
- * 部门表; 服务实现类
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @author lijin
|
|
|
- * @since 2022-12-09
|
|
|
+import static com.xy.utils.Beans.copy;
|
|
|
+
|
|
|
+/***
|
|
|
+ * 部门管理实现类
|
|
|
+ * @author 谭斌
|
|
|
+ * @date 2022/12/13 14:01
|
|
|
*/
|
|
|
+@Api(tags = "部门管理")
|
|
|
@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
|
|
|
|
|
|
+ private final SysDeptRelationService sysDeptRelationService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public R<Boolean> saveOrUpdate(SysDeptDto sysDeptDto) {
|
|
|
+ Long id = sysDeptDto.getId();
|
|
|
+ if (SaTokenUtils.getId() == null) {
|
|
|
+ return R.fail("获取用户信息失败");
|
|
|
+ }
|
|
|
+ boolean isAdd = true;
|
|
|
+ long currentUser = Long.parseLong(SaTokenUtils.getId().toString());
|
|
|
+ LambdaQueryWrapper<SysDept> lqw = new LambdaQueryWrapper<SysDept>()
|
|
|
+ .eq(SysDept::getName, sysDeptDto.getCode())
|
|
|
+ .eq(SysDept::getSysId, sysDeptDto.getSysId());
|
|
|
+ if (id != null) {
|
|
|
+ isAdd = false;
|
|
|
+ lqw.ne(SysDept::getId, id);
|
|
|
+ }
|
|
|
+
|
|
|
+ long count = count(lqw);
|
|
|
+ if (count > 0) {
|
|
|
+ return R.fail("此部门已存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ SysDept sysDept = copy(SysDept.class, sysDeptDto).saveOrUpdate(currentUser, sysDeptDto.getSysId());
|
|
|
+
|
|
|
+ if (isAdd) {
|
|
|
+ //新建部门关系
|
|
|
+ sysDeptRelationService.saveDeptRelation(sysDept);
|
|
|
+ } else {
|
|
|
+ // 更新部门关系
|
|
|
+ SysDeptRelation relation = new SysDeptRelation();
|
|
|
+ relation.setAncestor(sysDept.getParentId());
|
|
|
+ relation.setDescendant(sysDept.getId());
|
|
|
+ sysDeptRelationService.updateDeptRelation(relation);
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.ok(saveOrUpdate(sysDept));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<List<Tree<Long>>> deptTree(Long sysId) {
|
|
|
+ List<Tree<Long>> deptTree = getDeptTree(this.list(Wrappers.<SysDept>lambdaQuery().eq(SysDept::getSysId, sysId)), 0L);
|
|
|
+ return R.ok(deptTree);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<SysDeptVo> getById(Long id) {
|
|
|
+ SysDept sysDept = super.getById(id);
|
|
|
+ SysDeptVo sysRoleVo = copy(SysDeptVo.class, sysDept);
|
|
|
+ return R.ok(sysRoleVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<Boolean> deleteById(Long id) {
|
|
|
+ // 级联删除部门
|
|
|
+ List<Long> idList = sysDeptRelationService
|
|
|
+ .list(Wrappers.<SysDeptRelation>query().lambda().eq(SysDeptRelation::getAncestor, id)).stream()
|
|
|
+ .map(SysDeptRelation::getDescendant).collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (CollUtil.isNotEmpty(idList)) {
|
|
|
+ this.removeByIds(idList);
|
|
|
+ }
|
|
|
+ // 删除部门级联关系
|
|
|
+ sysDeptRelationService.removeDeptRelationById(id);
|
|
|
+ return R.ok(Boolean.TRUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<List<Tree<Long>>> getDeptTreeById(Long deptId) {
|
|
|
+ List<Long> descendantIdList = sysDeptRelationService
|
|
|
+ .list(Wrappers.<SysDeptRelation>query().lambda().eq(SysDeptRelation::getAncestor, deptId)).stream()
|
|
|
+ .map(SysDeptRelation::getDescendant).collect(Collectors.toList());
|
|
|
+ List<SysDept> deptList = baseMapper.selectBatchIds(descendantIdList);
|
|
|
+ Optional<SysDept> dept = deptList.stream().filter(item -> item.getId() == deptId).findFirst();
|
|
|
+ return R.ok(getDeptTree(deptList, dept.isPresent() ? dept.get().getParentId() : 0L));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得部门树
|
|
|
+ *
|
|
|
+ * @param deptList 部门集合
|
|
|
+ * @param parentId 父级id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<Tree<Long>> getDeptTree(List<SysDept> deptList, Long parentId) {
|
|
|
+ List<TreeNode<Long>> collect = deptList.stream().filter(dept -> dept.getId() != dept.getParentId())
|
|
|
+ .sorted(Comparator.comparingInt(SysDept::getSortNum)).map(dept -> {
|
|
|
+ TreeNode<Long> treeNode = new TreeNode();
|
|
|
+ treeNode.setId(dept.getId());
|
|
|
+ treeNode.setParentId(dept.getParentId());
|
|
|
+ treeNode.setName(dept.getName());
|
|
|
+ treeNode.setWeight(dept.getSortNum());
|
|
|
+ // 扩展属性
|
|
|
+ Map<String, Object> extra = new HashMap<>(16);
|
|
|
+ extra.put("createTime", dept.getCreateTime());
|
|
|
+ extra.put("code", dept.getCode());
|
|
|
+ treeNode.setExtra(extra);
|
|
|
+ return treeNode;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ return TreeUtil.build(collect, parentId);
|
|
|
+ }
|
|
|
+
|
|
|
}
|