|
@@ -11,9 +11,11 @@ import org.jeecg.modules.kms.bas.entity.Category;
|
|
|
import org.jeecg.modules.kms.bas.mapper.KmsCategoryMapper;
|
|
|
import org.jeecg.modules.kms.bas.service.ICategoryService;
|
|
|
import org.jeecg.modules.kms.knowledge.entity.Article;
|
|
|
+import org.jeecg.modules.kms.knowledge.mapper.ArticleMapper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -26,6 +28,9 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
|
public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category> implements ICategoryService {
|
|
|
|
|
|
+ @Resource
|
|
|
+ private ArticleMapper articleMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public void addCategory(Category category) {
|
|
|
//新增时设置hasChild为0
|
|
@@ -60,9 +65,58 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
|
|
|
baseMapper.updateTreeNodeStatus(category.getPid(), ICategoryService.HASCHILD);
|
|
|
}
|
|
|
}
|
|
|
- baseMapper.updateById(category);
|
|
|
+ // 文章分类状态改变
|
|
|
+ String oldIsPublic = entity.getIsPublic();
|
|
|
+ String newIsPublic = category.getIsPublic();
|
|
|
+ int i = baseMapper.updateById(category);
|
|
|
+ // 更新成功后改变本身及其子结点的文章状态
|
|
|
+ if (!oldIsPublic.equals(newIsPublic) && i > 0) {
|
|
|
+ updateArticleIsPublicByCategoryId(category);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新父节点及其文章状态 、 更新子节点及其文章状态
|
|
|
+ *
|
|
|
+ * @param category
|
|
|
+ */
|
|
|
+ private void updateArticleIsPublicByCategoryId(Category category) {
|
|
|
+ List<Category> categories = new ArrayList<>();
|
|
|
+ findAllChildrenIds(category, categories);
|
|
|
+ // 更新子节点的is_public状态
|
|
|
+ if (categories.isEmpty() || categories.size() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ categories.forEach(c -> c.setIsPublic(category.getIsPublic()));
|
|
|
+ boolean updateBatchByIdStatus = this.updateBatchById(categories);
|
|
|
+ //更新失败,抛出异常
|
|
|
+ if (!updateBatchByIdStatus) {
|
|
|
+ throw new JeecgBootException("更新失败");
|
|
|
+ }
|
|
|
+ // 更新article的is_public状态
|
|
|
+ List<String> categoryIds = categories.stream()
|
|
|
+ .map(Category::getId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ articleMapper.updateIsPublicByCategoryIds(categoryIds, category.getIsPublic());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 递归获取所有子节点
|
|
|
+ *
|
|
|
+ * @param category
|
|
|
+ * @param categories
|
|
|
+ */
|
|
|
+ private void findAllChildrenIds(Category category, List<Category> categories) {
|
|
|
+ categories.add(category);
|
|
|
+ QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
|
|
|
+ List<Category> children = this.list(queryWrapper.eq("pid", category.getId()));
|
|
|
+ for (Category child : children) {
|
|
|
+ findAllChildrenIds(child, categories);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void deleteCategory(String id) throws JeecgBootException {
|