Kaynağa Gözat

模型目录管理

ZhuDeKang 3 ay önce
ebeveyn
işleme
37897be0ac

+ 82 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/controller/MdCatalogController.java

@@ -0,0 +1,82 @@
+package com.ruoyi.interfaces.controller;
+
+import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.core.domain.entity.SysDept;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.interfaces.domain.MdCatalog;
+import com.ruoyi.interfaces.service.IMdCatalogService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+
+@RestController
+@RequestMapping("/md/catalog")
+public class MdCatalogController extends BaseController {
+    @Autowired
+    private IMdCatalogService  mdCatalogService;
+
+
+
+    /**
+     * 获取目录列表
+     */
+    @GetMapping("/list")
+    public AjaxResult list(MdCatalog catalog)
+    {
+        List<MdCatalog> catalogs = mdCatalogService.selectCatalogList(catalog);
+        return success(catalogs);
+    }
+
+    /**
+     * 新增目录
+     */
+    @PostMapping
+    public AjaxResult add(@RequestBody MdCatalog catalog)
+    {
+        catalog.setCreateBy(getUsername());
+        return toAjax(mdCatalogService.insertCatalog(catalog));
+    }
+
+
+    /**
+     * 修改目录
+     */
+    @PutMapping
+    public AjaxResult edit(@RequestBody MdCatalog catalog)
+    {
+        Integer catalogId = catalog.getCatalogId();
+        if (catalog.getParentId().equals(catalogId))
+        {
+            return error("修改目录'" + catalog.getCatalogName() + "'失败,上级目录不能是自己");
+        }
+        else if (StringUtils.equals("0", catalog.getStatus())
+                && mdCatalogService.selectNormalChildrenCatalogById(catalogId) > 0)
+        {
+            return error("该目录包含未停用的子目录!");
+        }
+        catalog.setUpdateBy(getUsername());
+        return toAjax(mdCatalogService.updateCatalog(catalog));
+    }
+
+
+
+    /**
+     * 删除目录
+     */
+    @DeleteMapping("/{catalogId}")
+    public AjaxResult remove(@PathVariable Integer catalogId)
+    {
+        if (mdCatalogService.hasChildByCatalogId(catalogId))
+        {
+            return warn("存在下级目录,不允许删除");
+        }
+
+        return toAjax(mdCatalogService.deleteCatalogById(catalogId));
+    }
+
+
+}

+ 58 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/domain/MdCatalog.java

@@ -0,0 +1,58 @@
+package com.ruoyi.interfaces.domain;
+
+import com.ruoyi.common.core.domain.BaseEntity;
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * 模型目录
+ */
+@Data
+public class MdCatalog extends BaseEntity {
+    /**
+     * 目录id
+     */
+    private Integer catalogId;
+    /**
+     * 目录名称
+     */
+    private String catalogName;
+    /**
+     * 父目录id
+     */
+    private Integer parentId;
+    /**
+     * 祖级列表
+     */
+    private String ancestors;
+    /**
+     * 目录说明
+     */
+    private String remark;
+    /**
+     * 显示顺序
+     */
+    private Integer sort;
+    /**
+     * 目录状态(0 = 停用 1 = 正常)
+     */
+    private String status;
+    /**
+     * 创建者
+     */
+    private String createBy;
+    /**
+     * 创建时间
+     */
+    private Date createTime;
+    /**
+     * 更新者
+     */
+    private String updateBy;
+    /**
+     * 更新时间
+     */
+    private Date updateTime;
+}
+

+ 34 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/mapper/MdCatalogMapper.java

@@ -0,0 +1,34 @@
+package com.ruoyi.interfaces.mapper;
+
+import com.ruoyi.common.annotation.DataSource;
+import com.ruoyi.common.enums.DataSourceType;
+import com.ruoyi.interfaces.domain.MdCatalog;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+@Mapper
+@DataSource(DataSourceType.SLAVE)
+public interface MdCatalogMapper {
+
+    int insertCatalog(MdCatalog catalog);
+
+    MdCatalog selectCatalogById(Integer catalogId);
+
+    int selectNormalChildrenDeptById(Integer catalogId);
+
+    List<MdCatalog> selectChildrenCatalogById(Integer catalogId);
+
+    int updateDeptChildren(@Param("catalogs") List<MdCatalog> catalogs);
+
+    int updateCatalog(MdCatalog dept);
+
+    int updateCatalogStatusNormal(Integer[] catalogIds);
+
+    int hasChildByCatalogId(Integer catalogId);
+
+    int deleteCatalogById(Integer catalogId);
+
+    List<MdCatalog> selectCatalogList(MdCatalog catalog);
+}

+ 19 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/service/IMdCatalogService.java

@@ -0,0 +1,19 @@
+package com.ruoyi.interfaces.service;
+
+import com.ruoyi.interfaces.domain.MdCatalog;
+
+import java.util.List;
+
+public interface IMdCatalogService {
+    int insertCatalog(MdCatalog catalog);
+
+    int selectNormalChildrenCatalogById(Integer catalogId);
+
+    int updateCatalog(MdCatalog dept);
+
+    boolean hasChildByCatalogId(Integer catalogId);
+
+    int deleteCatalogById(Integer catalogId);
+
+    List<MdCatalog> selectCatalogList(MdCatalog catalog);
+}

+ 109 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/service/impl/MdCatalogServiceImpl.java

@@ -0,0 +1,109 @@
+package com.ruoyi.interfaces.service.impl;
+
+import com.ruoyi.common.constant.UserConstants;
+import com.ruoyi.common.core.text.Convert;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.interfaces.domain.MdCatalog;
+import com.ruoyi.interfaces.mapper.MdCatalogMapper;
+import com.ruoyi.interfaces.service.IMdCatalogService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Collections;
+import java.util.List;
+
+@Service
+public class MdCatalogServiceImpl implements IMdCatalogService {
+    @Autowired
+    private MdCatalogMapper  mdCatalogMapper;
+
+    @Override
+    public int insertCatalog(MdCatalog catalog) {
+        if (StringUtils.isNull(catalog.getParentId())) {
+            //新增顶级节点
+            return mdCatalogMapper.insertCatalog(catalog);
+        }
+        MdCatalog info = mdCatalogMapper.selectCatalogById(catalog.getParentId());
+        if (StringUtils.isNull(info)) {
+            throw new ServiceException("父目录不存在,不允许新增");
+        }
+        // 如果父节点不为正常状态,则不允许新增子节点
+        if (!"1".equals(info.getStatus()) ) {
+            throw new ServiceException("目录停用,不允许新增");
+        }
+        catalog.setAncestors(info.getAncestors() + "," + catalog.getParentId());
+        return mdCatalogMapper.insertCatalog(catalog);
+    }
+
+    @Override
+    public int selectNormalChildrenCatalogById(Integer catalogId) {
+
+        return mdCatalogMapper.selectNormalChildrenDeptById(catalogId);
+    }
+
+    @Override
+    public int updateCatalog(MdCatalog catalog) {
+        MdCatalog newParentMdCatalog = mdCatalogMapper.selectCatalogById(catalog.getParentId());
+        MdCatalog oldCatalog = mdCatalogMapper.selectCatalogById(catalog.getCatalogId());
+        if (StringUtils.isNotNull(newParentMdCatalog) && StringUtils.isNotNull(oldCatalog)) {
+            String newAncestors = newParentMdCatalog.getAncestors() + "," + newParentMdCatalog.getCatalogId();
+            String oldAncestors = oldCatalog.getAncestors();
+            catalog.setAncestors(newAncestors);
+            updateCatalogChildren(catalog.getCatalogId(), newAncestors, oldAncestors);
+        }
+        int result = mdCatalogMapper.updateCatalog(catalog);
+        if ("1".equals(catalog.getStatus()) && StringUtils.isNotEmpty(catalog.getAncestors())
+               ) {
+            // 如果该部门是启用状态,则启用该部门的所有上级部门
+            updateParentCatalogStatusNormal(catalog);
+        }
+        return result;
+    }
+
+    @Override
+    public boolean hasChildByCatalogId(Integer catalogId) {
+        int result = mdCatalogMapper.hasChildByCatalogId(catalogId);
+        return result > 0;
+    }
+
+    @Override
+    public int deleteCatalogById(Integer catalogId) {
+
+        return mdCatalogMapper.deleteCatalogById(catalogId);
+    }
+
+    @Override
+    public List<MdCatalog> selectCatalogList(MdCatalog catalog) {
+        return mdCatalogMapper.selectCatalogList(catalog);
+    }
+
+    /**
+     * 修改该目录的父级目录状态
+     *
+     * @param catalog 当前目录
+     */
+    private void updateParentCatalogStatusNormal(MdCatalog catalog) {
+        String ancestors = catalog.getAncestors();
+        Integer[] catalogIds = Convert.toIntArray(ancestors);
+        mdCatalogMapper.updateCatalogStatusNormal(catalogIds);
+    }
+
+    /**
+     * 修改子元素关系
+     *
+     * @param catalogId       被修改的部门ID
+     * @param newAncestors 新的父ID集合
+     * @param oldAncestors 旧的父ID集合
+     */
+    public void updateCatalogChildren(Integer catalogId, String newAncestors, String oldAncestors) {
+        List<MdCatalog> children = mdCatalogMapper.selectChildrenCatalogById(catalogId);
+        for (MdCatalog child : children) {
+            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
+        }
+        if (children.size() > 0) {
+            mdCatalogMapper.updateDeptChildren(children);
+        }
+    }
+
+}

+ 155 - 0
ruoyi-api-patform/src/main/resources/mapper/interfaces/MdCatalogMapper.xml

@@ -0,0 +1,155 @@
+<?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.ruoyi.interfaces.mapper.MdCatalogMapper">
+
+    <insert id="insertCatalog">
+        insert into md_catalog(
+                               catalog_name,
+                               parent_id,
+                               ancestors,
+                               remark,
+                               sort,
+                               status,
+                               create_by,
+                               create_time,
+                               update_by,
+                               update_time)
+        values (  #{catalogName,jdbcType=VARCHAR}
+               , #{parentId,jdbcType=INTEGER}
+               , #{ancestors,jdbcType=VARCHAR}
+               , #{remark,jdbcType=VARCHAR}
+               , #{sort,jdbcType=INTEGER}
+               , #{status,jdbcType=VARCHAR}
+               , #{createBy,jdbcType=VARCHAR}
+               , sysdate
+               , #{updateBy,jdbcType=VARCHAR}
+               , #{updateTime,jdbcType=DATE}
+               )
+    </insert>
+    <update id="updateDeptChildren">
+        update md_catalog set ancestors =
+        <foreach collection="catalogs" item="item" index="index"
+                 separator=" " open="case catalog_id" close="end">
+            when #{item.catalogId} then #{item.ancestors}
+        </foreach>
+        where catalog_id in
+        <foreach collection="catalogs" item="item" index="index"
+                 separator="," open="(" close=")">
+            #{item.catalogId}
+        </foreach>
+    </update>
+    <update id="updateCatalog">
+        update md_catalog
+        <set>
+            <if test="catalogName != null and catalogName != ''">
+                catalog_name = #{catalogName},
+            </if>
+            <if test="parentId != null ">
+                parent_id = #{parentId},
+            </if>
+            <if test="ancestors != null">
+                ancestors = #{ancestors},
+            </if>
+            <if test="remark != null">
+                remark = #{remark},
+            </if>
+            <if test="sort != null ">
+                sort = #{sort},
+            </if>
+            <if test="status != null">
+                status = #{status},
+            </if>
+            <if test="createBy != null">
+                create_by = #{createBy},
+            </if>
+            <if test="createTime != null ">
+                create_time = #{createTime},
+            </if>
+            <if test="updateBy != null">
+                update_by = #{updateBy},
+            </if>
+                update_time = sysdate
+        </set>
+        where catalog_id = #{catalogId}
+    </update>
+    <update id="updateCatalogStatusNormal">
+        update md_catalog set status = '0' where catalog_id in
+        <foreach collection="array" item="catalogId" open="(" separator="," close=")">
+            #{catalogId}
+        </foreach>
+    </update>
+    <delete id="deleteCatalogById">
+        delete from md_catalog
+        where catalog_id = #{catalogId}
+    </delete>
+    <select id="selectCatalogById" resultType="com.ruoyi.interfaces.domain.MdCatalog">
+        select catalog_id,
+               catalog_name,
+               parent_id,
+               ancestors,
+               remark,
+               sort,
+               status,
+               create_by,
+               create_time,
+               update_by,
+               update_time
+        from md_catalog
+        <where>
+            catalog_id = #{catalogId}
+        </where>
+    </select>
+    <select id="selectNormalChildrenDeptById" resultType="java.lang.Integer">
+        select count(*) from md_catalog where status = 0 and instr(','||ancestors||',' ,  ','|| #{catalogId} ||',')
+    </select>
+    <select id="selectChildrenCatalogById" resultType="com.ruoyi.interfaces.domain.MdCatalog">
+        select * from md_catalog where instr(','||ancestors||',' ,  ','|| #{catalogId} ||',')
+    </select>
+    <select id="hasChildByCatalogId" resultType="java.lang.Integer">
+        select count(1) from md_catalog
+        where parent_id = #{catalogId} limit 1
+    </select>
+    <select id="selectCatalogList" resultType="com.ruoyi.interfaces.domain.MdCatalog">
+        select
+        catalog_id,catalog_name,parent_id,ancestors,remark,sort,status,create_by,create_time,update_by,update_time
+        from md_catalog
+        <where>
+            <if test="catalogId != null">
+                and catalog_id = #{catalogId}
+            </if>
+
+            <if test="catalogName != null">
+                and catalog_name like concat('%', #{catalogName}, '%')
+            </if>
+            <if test="parentId != null">
+                and parent_id = #{parentId}
+            </if>
+            <if test="ancestors != null">
+                and ancestors = #{ancestors}
+            </if>
+            <if test="remark != null">
+                and remark = #{remark}
+            </if>
+            <if test="sort != null">
+                and sort = #{sort}
+            </if>
+            <if test="status != null">
+                and status = #{status}
+            </if>
+            <if test="createBy != null">
+                and create_by = #{createBy}
+            </if>
+            <if test="createTime != null ">
+                and create_time = #{createTime}
+            </if>
+            <if test="updateBy != null">
+                and update_by = #{updateBy}
+            </if>
+            <if test="updateTime != null">
+                and update_time = #{updateTime}
+            </if>
+        </where>
+    </select>
+</mapper>