Browse Source

行业类别新增

LIVE_YE 1 year ago
parent
commit
69d794ecec

+ 7 - 7
ruoyi-admin/src/main/java/com/ruoyi/web/controller/category/IndustryCategoryController.java

@@ -35,11 +35,11 @@ public class IndustryCategoryController extends BaseController
     @Autowired
     private IIndustryCategoryService industryCategoryService;
 
-/**
- * 查询行业类别列表
- */
-@PreAuthorize("@ss.hasPermi('system:category:list')")
-@GetMapping("/list")
+    /**
+     * 查询行业类别列表
+     */
+    @PreAuthorize("@ss.hasPermi('system:category:list')")
+    @GetMapping("/list")
     public TableDataInfo list(IndustryCategory industryCategory)
     {
         //startPage();
@@ -104,7 +104,7 @@ public class IndustryCategoryController extends BaseController
     }
 
     /**
-     * 获取提案类别树列表
+     * 获取行业类别树列表
      */
     @GetMapping("/categoryTree")
     public AjaxResult categoryTree(IndustryCategory industryCategory)
@@ -113,7 +113,7 @@ public class IndustryCategoryController extends BaseController
     }
 
     /**
-     * 查询提案类别列表(排除节点)
+     * 查询行业类别列表(排除节点)
      */
     @GetMapping("/list/exclude/{categoryId}")
     public AjaxResult excludeChild(@PathVariable(value = "categoryId", required = false) Long categoryId)

+ 7 - 0
ruoyi-system/src/main/java/com/ruoyi/system/mapper/IndustryCategoryMapper.java

@@ -2,6 +2,7 @@ package com.ruoyi.system.mapper;
 
 import java.util.List;
 import com.ruoyi.common.core.domain.entity.IndustryCategory;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * 行业类别Mapper接口
@@ -58,4 +59,10 @@ public interface IndustryCategoryMapper
      * @return 结果
      */
     public int deleteIndustryCategoryByCategoryIds(Long[] categoryIds);
+
+    List<IndustryCategory> selectChildrenIndustryById(Long id);
+
+    public int updateIndustryChildren(@Param("industrys")List<IndustryCategory> children);
+
+    public int updateDeptStatusNormal(Long[] ids);
 }

+ 61 - 1
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/IndustryCategoryServiceImpl.java

@@ -5,7 +5,11 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.stream.Collectors;
 
+import com.ruoyi.common.constant.UserConstants;
 import com.ruoyi.common.core.domain.TreeSelect;
+import com.ruoyi.common.core.domain.entity.SysDept;
+import com.ruoyi.common.core.text.Convert;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.spring.SpringUtils;
@@ -60,6 +64,14 @@ public class IndustryCategoryServiceImpl implements IIndustryCategoryService
     @Override
     public int insertIndustryCategory(IndustryCategory industryCategory)
     {
+
+        IndustryCategory industry = industryCategoryMapper.selectIndustryCategoryByCategoryId(industryCategory.getParentId());
+        // 如果父节点不为正常状态,则不允许新增子节点
+        if (!UserConstants.DEPT_NORMAL.equals(industry.getStatus()))
+        {
+            throw new ServiceException("类别停用,不允许新增");
+        }
+        industryCategory.setAncestors(industry.getAncestors() + "," + industryCategory.getParentId());
         industryCategory.setCreateTime(DateUtils.getNowDate());
         return industryCategoryMapper.insertIndustryCategory(industryCategory);
     }
@@ -73,8 +85,56 @@ public class IndustryCategoryServiceImpl implements IIndustryCategoryService
     @Override
     public int updateIndustryCategory(IndustryCategory industryCategory)
     {
+        IndustryCategory newParentIndustry = industryCategoryMapper.selectIndustryCategoryByCategoryId(industryCategory.getParentId());
+        IndustryCategory oldIndustry = industryCategoryMapper.selectIndustryCategoryByCategoryId(industryCategory.getCategoryId());
+        if (StringUtils.isNotNull(newParentIndustry) && StringUtils.isNotNull(oldIndustry))
+        {
+            String newAncestors = newParentIndustry.getAncestors() + "," + newParentIndustry.getCategoryId();
+            String oldAncestors = oldIndustry.getAncestors();
+            industryCategory.setAncestors(newAncestors);
+            updateDeptChildren(industryCategory.getCategoryId(), newAncestors, oldAncestors);
+        }
         industryCategory.setUpdateTime(DateUtils.getNowDate());
-        return industryCategoryMapper.updateIndustryCategory(industryCategory);
+        int result = industryCategoryMapper.updateIndustryCategory(industryCategory);
+        if (UserConstants.DEPT_NORMAL.equals(industryCategory.getStatus()) && StringUtils.isNotEmpty(industryCategory.getAncestors())
+                && !StringUtils.equals("0", industryCategory.getAncestors()))
+        {
+            // 如果该行业是启用状态,则启用该行业的所有上级部门
+            updateParentDeptStatusNormal(industryCategory);
+        }
+        return result;
+    }
+
+    /**
+     * 修改该行业的父级部门状态
+     *
+     * @param industryCategory 当前行业
+     */
+    private void updateParentDeptStatusNormal(IndustryCategory industryCategory)
+    {
+        String ancestors = industryCategory.getAncestors();
+        Long[] ids = Convert.toLongArray(ancestors);
+        industryCategoryMapper.updateDeptStatusNormal(ids);
+    }
+
+    /**
+     * 修改子元素关系
+     *
+     * @param id 被修改的ID
+     * @param newAncestors 新的父ID集合
+     * @param oldAncestors 旧的父ID集合
+     */
+    public void updateDeptChildren(Long id, String newAncestors, String oldAncestors)
+    {
+        List<IndustryCategory> children = industryCategoryMapper.selectChildrenIndustryById(id);
+        for (IndustryCategory child : children)
+        {
+            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
+        }
+        if (children.size() > 0)
+        {
+            industryCategoryMapper.updateIndustryChildren(children);
+        }
     }
 
     /**

+ 22 - 1
ruoyi-system/src/main/resources/mapper/system/IndustryCategoryMapper.xml

@@ -37,7 +37,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <include refid="selectIndustryCategoryVo"/>
         where category_id = #{categoryId}
     </select>
-        
+    <select id="selectChildrenIndustryById" resultMap="IndustryCategoryResult" parameterType="java.lang.Long">
+        select * from industry_category where find_in_set(#{id}, ancestors)
+    </select>
+
     <insert id="insertIndustryCategory" parameterType="IndustryCategory" useGeneratedKeys="true" keyProperty="categoryId">
         insert into industry_category
         <trim prefix="(" suffix=")" suffixOverrides=",">
@@ -82,6 +85,24 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         </trim>
         where category_id = #{categoryId}
     </update>
+    <update id="updateIndustryChildren" parameterType="java.util.List">
+        update industry_category set ancestors =
+        <foreach collection="industrys" item="item" index="index"
+                 separator=" " open="case category_id" close="end">
+            when #{item.categoryId} then #{item.ancestors}
+        </foreach>
+        where category_id in
+        <foreach collection="industrys" item="item" index="index"
+                 separator="," open="(" close=")">
+            #{item.categoryId}
+        </foreach>
+    </update>
+    <update id="updateDeptStatusNormal" parameterType="Long">
+        update industry_category set status = '0' where category_id in
+        <foreach collection="array" item="id" open="(" separator="," close=")">
+            #{id}
+        </foreach>
+    </update>
 
     <delete id="deleteIndustryCategoryByCategoryId" parameterType="Long">
         delete from industry_category where category_id = #{categoryId}