浏览代码

Merge remote-tracking branch 'origin/master'

李进 2 年之前
父节点
当前提交
4c50a86bc2

+ 9 - 4
authorize-api-service/src/main/java/com/xy/entity/SysDept.java

@@ -21,9 +21,10 @@ public class SysDept extends BaseEntity2<SysDept> {
     private static final long serialVersionUID = 1L;
 
     /**
-     * 父id;多个,号分隔
+     * 父id
      */
-    private String paterId;
+    private Long parentId;
+
 
     /**
      * 系统id
@@ -46,8 +47,12 @@ public class SysDept extends BaseEntity2<SysDept> {
     private String code;
 
     /**
-     * 状态
+     * 状态(true启用,false)
+     */
+    private Boolean status;
+    /**
+     * 排序
      */
-    private Integer status;
+    private Integer sortNum;
 
 }

+ 30 - 0
authorize-api-service/src/main/java/com/xy/entity/SysDeptRelation.java

@@ -0,0 +1,30 @@
+package com.xy.entity;
+
+import com.baomidou.mybatisplus.extension.activerecord.Model;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+
+/***
+ * 部门关系表
+ * @author 谭斌
+ * @date 2022/12/13 15:05
+ */
+@Data
+@Accessors(chain = true)
+@EqualsAndHashCode(callSuper = true)
+public class SysDeptRelation extends Model<SysDeptRelation> {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 祖先节点
+     */
+    private Long ancestor;
+
+    /**
+     * 后代节点
+     */
+    private Long descendant;
+
+}

+ 36 - 0
authorize-api-service/src/main/java/com/xy/mapper/SysDeptRelationMapper.java

@@ -0,0 +1,36 @@
+package com.xy.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.xy.entity.SysDeptRelation;
+import org.apache.ibatis.annotations.Mapper;
+
+/***
+ * 部门关系 mapper
+ * @author 谭斌
+ * @date 2022/12/13 15:05
+ */
+@Mapper
+public interface SysDeptRelationMapper extends BaseMapper<SysDeptRelation> {
+
+    /**
+     * 删除部门节点关系
+     *
+     * @param deptRelation 待删除的某一个部门节点
+     */
+    void deleteDeptRelations(SysDeptRelation deptRelation);
+
+    /**
+     * 删除部门节点关系,同时删除所有关联此部门子节点的部门关系
+     *
+     * @param id 待删除的部门节点ID
+     */
+    void deleteDeptRelationsById(Long id);
+
+    /**
+     * 新增部门节点关系
+     *
+     * @param deptRelation 待新增的部门节点关系
+     */
+    void insertDeptRelations(SysDeptRelation deptRelation);
+
+}

+ 36 - 0
authorize-api-service/src/main/java/com/xy/service/SysDeptRelationService.java

@@ -0,0 +1,36 @@
+package com.xy.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.xy.entity.SysDept;
+import com.xy.entity.SysDeptRelation;
+
+
+/***
+ * 部门关系服务类
+ * @author 谭斌
+ * @date 2022/12/13 15:14
+ */
+public interface SysDeptRelationService extends IService<SysDeptRelation> {
+
+    /**
+     * 新建部门关系
+     *
+     * @param sysDept
+     */
+    void saveDeptRelation(SysDept sysDept);
+
+    /**
+     * 通过ID删除部门关系
+     *
+     * @param id
+     */
+    void removeDeptRelationById(Long id);
+
+    /**
+     * 更新部门关系
+     *
+     * @param relation
+     */
+    void updateDeptRelation(SysDeptRelation relation);
+
+}

+ 0 - 16
authorize-api-service/src/main/java/com/xy/service/SysDeptService.java

@@ -1,16 +0,0 @@
-package com.xy.service;
-
-import com.xy.annotate.RestMappingController;
-
-/**
- * <p>
- * 部门表; 服务类
- * </p>
- *
- * @author lijin
- * @since 2022-12-09
- */
-@RestMappingController("sys-dept")
-public interface SysDeptService {
-
-}

+ 77 - 0
authorize-api-service/src/main/java/com/xy/service/impl/SysDeptRelationServiceImpl.java

@@ -0,0 +1,77 @@
+package com.xy.service.impl;
+
+import cn.hutool.core.collection.CollUtil;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.xy.entity.SysDept;
+import com.xy.entity.SysDeptRelation;
+import com.xy.mapper.SysDeptRelationMapper;
+import com.xy.service.SysDeptRelationService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/***
+ * 部门关系服务实现类
+ * @author 谭斌
+ * @date 2022/12/13 15:15
+ */
+@Service
+@RequiredArgsConstructor
+public class SysDeptRelationServiceImpl extends ServiceImpl<SysDeptRelationMapper, SysDeptRelation>
+        implements SysDeptRelationService {
+
+    private final SysDeptRelationMapper sysDeptRelationMapper;
+
+    /**
+     * 维护部门关系
+     *
+     * @param sysDept
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void saveDeptRelation(SysDept sysDept) {
+        // 增加部门关系表
+        List<SysDeptRelation> relationList = sysDeptRelationMapper.selectList(
+                        Wrappers.<SysDeptRelation>query().lambda().eq(SysDeptRelation::getDescendant, sysDept.getParentId()))
+                .stream().map(relation -> {
+                    relation.setDescendant(sysDept.getId());
+                    return relation;
+                }).collect(Collectors.toList());
+        if (CollUtil.isNotEmpty(relationList)) {
+            this.saveBatch(relationList);
+        }
+
+        // 自己也要维护到关系表中
+        SysDeptRelation own = new SysDeptRelation();
+        own.setDescendant(sysDept.getId());
+        own.setAncestor(sysDept.getId());
+        sysDeptRelationMapper.insert(own);
+    }
+
+    /**
+     * 通过ID删除部门关系
+     *
+     * @param id
+     */
+    @Override
+    public void removeDeptRelationById(Long id) {
+        baseMapper.deleteDeptRelationsById(id);
+    }
+
+    /**
+     * 更新部门关系
+     *
+     * @param relation
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void updateDeptRelation(SysDeptRelation relation) {
+        baseMapper.deleteDeptRelations(relation);
+        baseMapper.insertDeptRelations(relation);
+    }
+
+}

+ 132 - 8
authorize-api-service/src/main/java/com/xy/service/impl/SysDeptServiceImpl.java

@@ -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);
+    }
+
 }

+ 67 - 0
authorize-api/src/main/java/com/xy/dto/SysDeptDto.java

@@ -0,0 +1,67 @@
+package com.xy.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+@Data
+@ApiModel("部门表DTO")
+@Accessors(chain = true)
+public class SysDeptDto {
+    /**
+     * 部门ID
+     */
+    @ApiModelProperty("部门ID")
+    private Long id;
+
+    /**
+     * 系统ID
+     */
+    @NotNull(message = "所属系统不可为空")
+    @ApiModelProperty(value = "系统", required = true)
+    private Long sysId;
+
+
+    /**
+     * 父id
+     */
+    @ApiModelProperty(value = "系统", required = false)
+    private Long parentId;
+
+
+    /**
+     * 角色id;多个,号分隔
+     */
+    private String roleId;
+
+    /**
+     * 名称
+     */
+    @NotNull(message = "部门名称不能为空")
+    @Size(max = 64, min = 2, message = "部门名称长度需要在2-64之间")
+    @ApiModelProperty(value = "部门名称", required = true)
+    private String name;
+
+    /**
+     * 标识
+     */
+    @ApiModelProperty(value = "部门编码", required = false)
+    private String code;
+
+    /**
+     * 状态(true启用,false)
+     */
+    @ApiModelProperty(value = "状态", required = false)
+    private Boolean status;
+    /**
+     * 排序
+     */
+    @ApiModelProperty(value = "排序", required = false)
+    private Integer sortNum;
+
+
+}

+ 4 - 4
authorize-api/src/main/java/com/xy/dto/SysRoleDto.java

@@ -13,7 +13,7 @@ import javax.validation.constraints.Size;
 @Accessors(chain = true)
 public class SysRoleDto {
 
-    @ApiModelProperty("角色ID")
+    @ApiModelProperty(value = "角色ID", required = false)
     private Long id;
 
     @NotNull(message = "所属系统不可为空")
@@ -22,12 +22,12 @@ public class SysRoleDto {
 
 
     @NotNull(message = "角色标识不可为空")
-    @Size(max = 64, min = 1, message = "角色标识长度需要在2-64之间")
+    @Size(max = 64, min = 2, message = "角色标识长度需要在2-64之间")
     @ApiModelProperty(value = "角色标识", required = true)
     private String code;
 
     @NotNull(message = "角色名称不能为空")
-    @Size(max = 64, min = 1, message = "角色名称长度需要在2-64之间")
+    @Size(max = 64, min = 2, message = "角色名称长度需要在2-64之间")
     @ApiModelProperty(value = "角色名称", required = true)
     private String name;
 
@@ -43,6 +43,6 @@ public class SysRoleDto {
 
     @ApiModelProperty("是否预留角色;true=是 false=否(默认)")
     private Boolean isLeave;
- 
+
 
 }

+ 43 - 0
authorize-api/src/main/java/com/xy/service/SysDeptService.java

@@ -0,0 +1,43 @@
+package com.xy.service;
+
+import cn.hutool.core.lang.tree.Tree;
+import com.xy.annotate.RestMappingController;
+import com.xy.dto.SysDeptDto;
+import com.xy.utils.R;
+import com.xy.vo.SysDeptVo;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+import java.util.List;
+
+/***
+ * 部门管理服务类
+ * @author 谭斌
+ * @date 2022/12/12 16:37
+ */
+@RestMappingController("sysDept")
+public interface SysDeptService {
+
+    @ApiOperation(value = "新增、更新部门信息", notes = "新增、更新部门信息")
+    @PostMapping("saveOrUpdate")
+    R<Boolean> saveOrUpdate(@Valid @RequestBody SysDeptDto sysDeptDto);
+
+    @ApiOperation(value = "系统部门树", notes = "系统部门树")
+    @GetMapping("tree/{sysId}")
+    R<List<Tree<Long>>> deptTree(@PathVariable("sysId") Long sysId);
+
+    @ApiOperation(value = "根据ID获取部门信息", notes = "根据ID获取部门信息")
+    @GetMapping("{id}")
+    R<SysDeptVo> getById(@PathVariable("id") Long id);
+
+
+    @ApiOperation(value = "删除部门", notes = "删除部门")
+    @DeleteMapping("{id}")
+    R<Boolean> deleteById(@PathVariable("id") Long id);
+
+    @ApiOperation(value = "获取指定部门部门树", notes = "获取指定部门部门树")
+    @GetMapping("deptTree/{id}")
+    R<List<Tree<Long>>> getDeptTreeById(@PathVariable("id") Long id);
+
+}

+ 1 - 1
authorize-api/src/main/java/com/xy/service/SysRoleService.java

@@ -49,7 +49,7 @@ public interface SysRoleService {
      */
     @ApiOperation(value = "获取角色详情", notes = "获取角色详情")
     @GetMapping("{id}")
-    R<SysRoleVo> getById(@PathVariable Long id);
+    R<SysRoleVo> getById(@PathVariable("id") Long id);
 
     /**
      * 分页查询角色信息

+ 57 - 0
authorize-api/src/main/java/com/xy/vo/SysDeptVo.java

@@ -0,0 +1,57 @@
+package com.xy.vo;
+
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/***
+ * 部门信息
+ * @author 谭斌
+ * @date 2022/12/13 14:18
+ */
+@Data
+@ApiModel("部门信息")
+@Accessors(chain = true)
+public class SysDeptVo {
+
+    /**
+     * id
+     */
+    private Long id;
+
+    /**
+     * 父id
+     */
+    private Long parentId;
+
+
+    /**
+     * 系统id
+     */
+    private Long sysId;
+
+    /**
+     * 角色id;多个,号分隔
+     */
+    private String roleId;
+
+    /**
+     * 名称
+     */
+    private String name;
+
+    /**
+     * 标识
+     */
+    private String code;
+
+    /**
+     * 状态(true启用,false)
+     */
+    private Boolean status;
+    /**
+     * 排序
+     */
+    private Integer sortNum;
+
+}

+ 40 - 0
authorize-start/src/main/resources/mapper/SysDeptRelationMapper.xml

@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.xy.mapper.SysDeptRelationMapper">
+    <!-- 删除部门节点关系	-->
+    <delete id="deleteDeptRelations">
+        DELETE
+        FROM sys_dept_relation
+        WHERE descendant IN (SELECT temp.descendant
+        FROM (SELECT descendant FROM sys_dept_relation WHERE ancestor = #{descendant}) temp)
+        AND ancestor IN (SELECT temp.ancestor
+        FROM (SELECT ancestor
+        FROM sys_dept_relation
+        WHERE descendant = #{descendant}
+        AND ancestor != descendant) temp)
+    </delete>
+
+    <!--删除部门节点关系,同时删除所有关联此部门子节点的部门关系-->
+    <delete id="deleteDeptRelationsById">
+        DELETE
+        FROM sys_dept_relation
+        WHERE descendant IN (
+        SELECT temp.descendant
+        FROM (
+        SELECT descendant
+        FROM sys_dept_relation
+        WHERE ancestor = #{id}
+        ) temp
+        )
+    </delete>
+
+    <!-- 新增部门节点关系	-->
+    <insert id="insertDeptRelations">
+        INSERT INTO sys_dept_relation (ancestor, descendant)
+        SELECT a.ancestor, b.descendant
+        FROM sys_dept_relation a
+        CROSS JOIN sys_dept_relation b
+        WHERE a.descendant = #{ancestor}
+        AND b.ancestor = #{descendant}
+    </insert>
+</mapper>