Skip to content

Commit

Permalink
fix: 修复专题的查询逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozzzi committed Apr 10, 2024
1 parent 7d14414 commit a718002
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.blossom.backend.server.doc.DocService;
import com.blossom.backend.server.folder.pojo.*;
import com.blossom.backend.server.utils.DocUtil;
import com.blossom.common.base.enums.YesNo;
import com.blossom.common.base.exception.XzException404;
import com.blossom.common.base.pojo.DelReq;
import com.blossom.common.base.pojo.R;
Expand Down Expand Up @@ -46,7 +45,7 @@ public R<List<FolderSubjectRes>> listSubjectOpen(@RequestHeader(BlConstants.REQ_
if (userId == null) {
return R.ok(new ArrayList<>());
}
return R.ok(baseService.subjects(userId, YesNo.YES));
return R.ok(baseService.subjects(userId, true, false));
}

/**
Expand All @@ -55,8 +54,8 @@ public R<List<FolderSubjectRes>> listSubjectOpen(@RequestHeader(BlConstants.REQ_
* @param starStatus 公开状态
*/
@GetMapping("/subjects")
public R<List<FolderSubjectRes>> listSubject(@RequestParam("starStatus") Integer starStatus) {
return R.ok(baseService.subjects(AuthContext.getUserId(), YesNo.getValue(starStatus)));
public R<List<FolderSubjectRes>> listSubject() {
return R.ok(baseService.subjects(AuthContext.getUserId(), false, true));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -56,41 +55,46 @@ public class FolderService extends ServiceImpl<FolderMapper, FolderEntity> {
* @param userId 用户ID
* @param starStatus 公开状态
*/
public List<FolderSubjectRes> subjects(Long userId, @NotNull YesNo starStatus) {
public List<FolderSubjectRes> subjects(Long userId, boolean openStatus, boolean starStatus) {
// 1. 查询所有专题
FolderEntity where = new FolderEntity();
where.setTags(TagEnum.subject.name());
where.setUserId(userId);
where.setStarStatus(starStatus.getValue());
List<FolderEntity> allOpenSubject = baseMapper.listAll(where);
if (CollUtil.isEmpty(allOpenSubject)) {
if (openStatus) {
where.setOpenStatus(YesNo.YES.getValue());
}
if (starStatus) {
where.setStarStatus(YesNo.YES.getValue());
}
List<FolderEntity> allSubjects = baseMapper.listAll(where);
if (CollUtil.isEmpty(allSubjects)) {
return new ArrayList<>();
}

// 专题的ID
List<Long> allOpenSubjectIds = allOpenSubject.stream().map(FolderEntity::getId).collect(Collectors.toList());
List<Long> allSubjectIds = allSubjects.stream().map(FolderEntity::getId).collect(Collectors.toList());

// 2. 查询全部专题的子文件夹
List<FolderEntity> allOpenSubjectChildFolders = baseMapper.recursiveToChildren(CollUtil.newArrayList(allOpenSubjectIds));
allOpenSubjectIds.addAll(allOpenSubjectChildFolders.stream().map(FolderEntity::getId).collect(Collectors.toList()));
List<FolderEntity> allSubjectChildFolders = baseMapper.recursiveToChildren(CollUtil.newArrayList(allSubjectIds));
allSubjectIds.addAll(allSubjectChildFolders.stream().map(FolderEntity::getId).collect(Collectors.toList()));

// 3. 查询这些文件夹下的所有文章
ArticleEntity articleWhere = new ArticleEntity();
articleWhere.setPids(allOpenSubjectIds);
articleWhere.setPids(allSubjectIds);
articleWhere.setUserId(userId);
List<ArticleEntity> articles = articleMapper.listAll(articleWhere);

List<FolderSubjectRes> results = new ArrayList<>();

for (FolderEntity subject : allOpenSubject) {
for (FolderEntity subject : allSubjects) {
// 专题对象, 包含字数, 更新日期等信息
FolderSubjectRes result = subject.to(FolderSubjectRes.class);
// 默认专题字数
result.setSubjectWords(0);
// 默认专题修改时间
result.setSubjectUpdTime(subject.getCreTime());
// 4. 这个专题下的所有文件夹ID
List<Long> subjectAllId = DocUtil.getChildrenIds(subject.getId(), allOpenSubjectChildFolders);
List<Long> subjectAllId = DocUtil.getChildrenIds(subject.getId(), allSubjectChildFolders);
// 5. 遍历文章, 将文章归属到某个专题下, 并统计相关字数, 日期等信息
for (ArticleEntity article : articles) {
if (subjectAllId.contains(article.getPid())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let maxWords = 0
const subjects = ref<any>([])
const getSubjects = () => {
subjectsApi({ starStatus: 1 }).then((resp) => {
subjectsApi().then((resp) => {
subjects.value = resp.data
if (!isEmpty(resp.data)) {
maxWords = resp.data
Expand Down

0 comments on commit a718002

Please sign in to comment.