瀏覽代碼

分类使用逻辑删除,文章查询分类使用sql绕过逻辑删除

danch 3 月之前
父節點
當前提交
fe644c5c5f

+ 6 - 1
jeecg-module-kms/src/main/java/org/jeecg/modules/kms/bas/entity/Category.java

@@ -2,6 +2,7 @@ package org.jeecg.modules.kms.bas.entity;
 
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
 import com.baomidou.mybatisplus.annotation.TableName;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import io.swagger.annotations.ApiModel;
@@ -74,5 +75,9 @@ public class Category implements Serializable {
     /**是否公开*/
     @ApiModelProperty(value = "是否公开")
     private String isPublic;
-
+    /**
+     * 删除状态(0,正常,1已删除)
+     */
+    @TableLogic
+    private Integer  delFlag;
 }

+ 1 - 2
jeecg-module-kms/src/main/java/org/jeecg/modules/kms/bas/mapper/KmsCategoryMapper.java

@@ -42,6 +42,5 @@ public interface KmsCategoryMapper extends BaseMapper<Category> {
 	 * 获取公共数据
 	 */
 	List<SelectTreeModel> queryListByPidNoPrivilege(@Param("pid") String pid, @Param("query") Map<String, String> query);
-
-
+	Category  selectByIdWithoutDelete(@Param("id") String id);
 }

+ 5 - 2
jeecg-module-kms/src/main/java/org/jeecg/modules/kms/bas/mapper/xml/KmsCategoryMapper.xml

@@ -36,7 +36,7 @@
         (case when has_child = '1' then 0 else 1 end) as isLeaf,
         pid as parentId
         from kms_bas_article_category
-        where pid = #{pid} AND (is_public IS NULL OR is_public = '0')
+        where pid = #{pid} and del_flag = 0 AND (is_public IS NULL OR is_public = '0')
         <if test="query != null">
             <foreach collection="query.entrySet()" item="value" index="key">
                 and ${key} = #{value}
@@ -55,7 +55,7 @@
         (case when has_child = '1' then 0 else 1 end) as isLeaf,
         pid as parentId
         from kms_bas_article_category
-        where pid = #{pid} and is_public = '1'
+        where pid = #{pid} and is_public = '1' and del_flag = 0
         <if test="query != null">
             <foreach collection="query.entrySet()" item="value" index="key">
                 and ${key} = #{value}
@@ -63,5 +63,8 @@
         </if>
         order by sort_no asc
     </select>
+    <select id="selectByIdWithoutDelete" resultType="org.jeecg.modules.kms.bas.entity.Category">
+        select * from kms_bas_article_category where id = #{id}
+    </select>
 
 </mapper>

+ 95 - 88
jeecg-module-kms/src/main/java/org/jeecg/modules/kms/bas/service/impl/CategoryServiceImpl.java

@@ -21,65 +21,65 @@ import java.util.List;
 /**
  * @Description: 基础资料-知识-分类
  * @Author: jeecg-boot
- * @Date:   2023-12-08
+ * @Date: 2023-12-08
  * @Version: V1.0
  */
 @Service
 public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category> implements ICategoryService {
 
-	@Override
-	public void addCategory(Category category) {
-	   //新增时设置hasChild为0
-	    category.setHasChild(ICategoryService.NOCHILD);
-		if(oConvertUtils.isEmpty(category.getPid())){
-			category.setPid(ICategoryService.ROOT_PID_VALUE);
-		}else{
-			//如果当前节点父ID不为空 则设置父节点的hasChildren 为1
-			Category parent = baseMapper.selectById(category.getPid());
-			if(parent!=null && !"1".equals(parent.getHasChild())){
-				parent.setHasChild("1");
-				baseMapper.updateById(parent);
-			}
-		}
-		baseMapper.insert(category);
-	}
-	
-	@Override
-	public void updateCategory(Category category) {
-		Category entity = this.getById(category.getId());
-		if(entity==null) {
-			throw new JeecgBootException("未找到对应实体");
-		}
-		String old_pid = entity.getPid();
-		String new_pid = category.getPid();
-		if(!old_pid.equals(new_pid)) {
-			updateOldParentNode(old_pid);
-			if(oConvertUtils.isEmpty(new_pid)){
-				category.setPid(ICategoryService.ROOT_PID_VALUE);
-			}
-			if(!ICategoryService.ROOT_PID_VALUE.equals(category.getPid())) {
-				baseMapper.updateTreeNodeStatus(category.getPid(), ICategoryService.HASCHILD);
-			}
-		}
-		baseMapper.updateById(category);
-	}
-	
-	@Override
-	@Transactional(rollbackFor = Exception.class)
-	public void deleteCategory(String id) throws JeecgBootException {
-		//查询选中节点下所有子节点一并删除
+    @Override
+    public void addCategory(Category category) {
+        //新增时设置hasChild为0
+        category.setHasChild(ICategoryService.NOCHILD);
+        if (oConvertUtils.isEmpty(category.getPid())) {
+            category.setPid(ICategoryService.ROOT_PID_VALUE);
+        } else {
+            //如果当前节点父ID不为空 则设置父节点的hasChildren 为1
+            Category parent = baseMapper.selectById(category.getPid());
+            if (parent != null && !"1".equals(parent.getHasChild())) {
+                parent.setHasChild("1");
+                baseMapper.updateById(parent);
+            }
+        }
+        baseMapper.insert(category);
+    }
+
+    @Override
+    public void updateCategory(Category category) {
+        Category entity = this.getById(category.getId());
+        if (entity == null) {
+            throw new JeecgBootException("未找到对应实体");
+        }
+        String old_pid = entity.getPid();
+        String new_pid = category.getPid();
+        if (!old_pid.equals(new_pid)) {
+            updateOldParentNode(old_pid);
+            if (oConvertUtils.isEmpty(new_pid)) {
+                category.setPid(ICategoryService.ROOT_PID_VALUE);
+            }
+            if (!ICategoryService.ROOT_PID_VALUE.equals(category.getPid())) {
+                baseMapper.updateTreeNodeStatus(category.getPid(), ICategoryService.HASCHILD);
+            }
+        }
+        baseMapper.updateById(category);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void deleteCategory(String id) throws JeecgBootException {
+        //查询选中节点下所有子节点一并删除
         id = this.queryTreeChildIds(id);
-        if(id.indexOf(",")>0) {
+        if (id.indexOf(",") > 0) {
             StringBuffer sb = new StringBuffer();
             String[] idArr = id.split(",");
             for (String idVal : idArr) {
-                if(idVal != null){
+                if (idVal != null) {
                     Category category = this.getById(idVal);
                     String pidVal = category.getPid();
                     //查询此节点上一级是否还有其他子节点
-                    List<Category> dataList = baseMapper.selectList(new QueryWrapper<Category>().eq("pid", pidVal).notIn("id",Arrays.asList(idArr)));
+                    List<Category> dataList = baseMapper.selectList(new QueryWrapper<Category>().eq("pid", pidVal).notIn("id", Arrays.asList(idArr)));
                     boolean flag = (dataList == null || dataList.size() == 0) && !Arrays.asList(idArr).contains(pidVal) && !sb.toString().contains(pidVal);
-                    if(flag){
+                    if (flag) {
                         //如果当前节点原本有子节点 现在木有了,更新状态
                         sb.append(pidVal).append(",");
                     }
@@ -89,33 +89,33 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
             baseMapper.deleteBatchIds(Arrays.asList(idArr));
             //修改已无子节点的标识
             String[] pidArr = sb.toString().split(",");
-            for(String pid : pidArr){
+            for (String pid : pidArr) {
                 this.updateOldParentNode(pid);
             }
-        }else{
+        } else {
             Category category = this.getById(id);
-            if(category==null) {
+            if (category == null) {
                 throw new JeecgBootException("未找到对应实体");
             }
             updateOldParentNode(category.getPid());
             baseMapper.deleteById(id);
         }
-	}
-	
-	@Override
+    }
+
+    @Override
     public List<Category> queryTreeListNoPage(QueryWrapper<Category> queryWrapper) {
         List<Category> dataList = baseMapper.selectList(queryWrapper);
         List<Category> mapList = new ArrayList<>();
-        for(Category data : dataList){
+        for (Category data : dataList) {
             String pidVal = data.getPid();
             //递归查询子节点的根节点
-            if(pidVal != null && !ICategoryService.NOCHILD.equals(pidVal)){
+            if (pidVal != null && !ICategoryService.NOCHILD.equals(pidVal)) {
                 Category rootVal = this.getTreeRoot(pidVal);
-                if(rootVal != null && !mapList.contains(rootVal)){
+                if (rootVal != null && !mapList.contains(rootVal)) {
                     mapList.add(rootVal);
                 }
-            }else{
-                if(!mapList.contains(data)){
+            } else {
+                if (!mapList.contains(data)) {
                     mapList.add(data);
                 }
             }
@@ -164,35 +164,38 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
         return selectTreeModels;
     }
 
-	/**
-	 * 根据所传pid查询旧的父级节点的子节点并修改相应状态值
-	 * @param pid
-	 */
-	private void updateOldParentNode(String pid) {
-		if(!ICategoryService.ROOT_PID_VALUE.equals(pid)) {
-			Long count = baseMapper.selectCount(new QueryWrapper<Category>().eq("pid", pid));
-			if(count==null || count<=1) {
-				baseMapper.updateTreeNodeStatus(pid, ICategoryService.NOCHILD);
-			}
-		}
-	}
+    /**
+     * 根据所传pid查询旧的父级节点的子节点并修改相应状态值
+     *
+     * @param pid
+     */
+    private void updateOldParentNode(String pid) {
+        if (!ICategoryService.ROOT_PID_VALUE.equals(pid)) {
+            Long count = baseMapper.selectCount(new QueryWrapper<Category>().eq("pid", pid));
+            if (count == null || count <= 1) {
+                baseMapper.updateTreeNodeStatus(pid, ICategoryService.NOCHILD);
+            }
+        }
+    }
 
-	/**
+    /**
      * 递归查询节点的根节点
+     *
      * @param pidVal
      * @return
      */
-    private Category getTreeRoot(String pidVal){
-        Category data =  baseMapper.selectById(pidVal);
-        if(data != null && !ICategoryService.ROOT_PID_VALUE.equals(data.getPid())){
+    private Category getTreeRoot(String pidVal) {
+        Category data = baseMapper.selectById(pidVal);
+        if (data != null && !ICategoryService.ROOT_PID_VALUE.equals(data.getPid())) {
             return this.getTreeRoot(data.getPid());
-        }else{
+        } else {
             return data;
         }
     }
 
     /**
      * 根据id查询所有子节点id
+     *
      * @param ids
      * @return
      */
@@ -201,13 +204,13 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
         String[] idArr = ids.split(",");
         StringBuffer sb = new StringBuffer();
         for (String pidVal : idArr) {
-            if(pidVal != null){
-                if(!sb.toString().contains(pidVal)){
-                    if(sb.toString().length() > 0){
+            if (pidVal != null) {
+                if (!sb.toString().contains(pidVal)) {
+                    if (sb.toString().length() > 0) {
                         sb.append(",");
                     }
                     sb.append(pidVal);
-                    this.getTreeChildIds(pidVal,sb);
+                    this.getTreeChildIds(pidVal, sb);
                 }
             }
         }
@@ -216,27 +219,29 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
 
     /**
      * 递归查询所有子节点
+     *
      * @param pidVal
      * @param sb
      * @return
      */
-    private StringBuffer getTreeChildIds(String pidVal,StringBuffer sb){
+    private StringBuffer getTreeChildIds(String pidVal, StringBuffer sb) {
         List<Category> dataList = baseMapper.selectList(new QueryWrapper<Category>().eq("pid", pidVal));
-        if(dataList != null && dataList.size()>0){
-            for(Category tree : dataList) {
-                if(!sb.toString().contains(tree.getId())){
+        if (dataList != null && dataList.size() > 0) {
+            for (Category tree : dataList) {
+                if (!sb.toString().contains(tree.getId())) {
                     sb.append(",").append(tree.getId());
                 }
-                this.getTreeChildIds(tree.getId(),sb);
+                this.getTreeChildIds(tree.getId(), sb);
             }
         }
         return sb;
     }
+
     @Override
-    public List<String> getAllParentNodeById(String childId){
+    public List<String> getAllParentNodeById(String childId) {
         List<String> parentNodeList = new ArrayList<>();
         Category category = baseMapper.selectById(childId);
-        if(category != null){
+        if (category != null) {
             parentNodeList.add(category.getId());
             parentNodeList.addAll(getAllParentNodeById(category.getPid()));
         }
@@ -246,9 +251,11 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
     @Override
     public List<Category> queryParentNodeById(String id) {
         List<Category> parentNodeList = new ArrayList<>();
-        Category category = baseMapper.selectById(id);
-        if(category != null){
-            parentNodeList.add(category);
+        Category category = baseMapper.selectByIdWithoutDelete(id);
+        if (category != null) {
+            if (category.getDelFlag() == 0) {
+                parentNodeList.add(category);
+            }
             parentNodeList.addAll(queryParentNodeById(category.getPid()));
         }
         return parentNodeList;