Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 调整代码
Browse files Browse the repository at this point in the history
CarefreeState committed Oct 16, 2024
1 parent 82b9cc4 commit 7756f86
Showing 16 changed files with 156 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -4,9 +4,11 @@
import com.achobeta.common.annotation.Intercept;
import com.achobeta.common.enums.UserTypeEnum;
import com.achobeta.domain.paper.model.converter.LibraryConverter;
import com.achobeta.domain.paper.model.dto.LibraryReferencePaperDTO;
import com.achobeta.domain.paper.model.dto.PaperLibraryDTO;
import com.achobeta.domain.paper.model.entity.QuestionPaperLibrary;
import com.achobeta.domain.paper.service.QuestionPaperLibraryService;
import com.achobeta.domain.paper.service.QuestionPaperService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
@@ -33,12 +35,21 @@ public class PaperLibraryController {

private final QuestionPaperLibraryService questionPaperLibraryService;

private final QuestionPaperService questionPaperService;

@PostMapping("/create")
public SystemJsonResponse createPaperLibrary(@RequestParam("libType") @NotBlank String libType) {
Long paperLibraryId = questionPaperLibraryService.createPaperLibrary(libType);
return SystemJsonResponse.SYSTEM_SUCCESS(paperLibraryId);
}

@PostMapping("/reference")
public SystemJsonResponse referencePapers(@Valid @RequestBody LibraryReferencePaperDTO libraryReferencePaperDTO) {
// 引用
questionPaperService.referencePapers(libraryReferencePaperDTO.getLibId(), libraryReferencePaperDTO.getPaperIds());
return SystemJsonResponse.SYSTEM_SUCCESS();
}

@PostMapping("/rename")
public SystemJsonResponse renamePaperLibrary(@Valid @RequestBody PaperLibraryDTO paperLibraryDTO) {
// 检查
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@ public interface QuestionPaperMapper extends BaseMapper<QuestionPaper> {

// 并不会将结果集加入 page,而是返回值 IPage 里
IPage<QuestionPaper> queryPapers(IPage<QuestionPaper> page, @Param("libIds") List<Long> libIds);

List<QuestionPaper> getPapers(@Param("libIds") List<Long> libIds);
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.achobeta.domain.paper.model.dto;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.util.List;

/**
* Created With Intellij IDEA
* Description:
* User: 马拉圈
* Date: 2024-10-16
* Time: 20:43
*/
@Data
public class LibraryReferencePaperDTO {

@NotNull(message = "试卷库 id 不能为空")
private Long libId;

@NotEmpty(message = "试卷 id 列表不能为空")
private List<Long> paperIds;

}
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
@Data
public class PaperLibraryDTO {

@NotNull(message = "库的 id 不能为空")
@NotNull(message = "试卷库 id 不能为空")
private Long libId;

@NotBlank(message = "库的类型不能为空")
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.achobeta.domain.paper.model.dto;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

@@ -18,7 +19,7 @@ public class PaperQuestionLinkDTO {
@NotNull(message = "试卷 id 不能为空")
private Long paperId;

@NotNull(message = "问题 ids 不能为空")
@NotEmpty(message = "问题 ids 不能为空")
private List<Long> questionIds;

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.achobeta.domain.paper.model.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;

import java.util.List;
@@ -16,7 +16,7 @@
@Data
public class QuestionPaperDTO {

@NotNull(message = "试卷库 ids 不能为空")
@NotEmpty(message = "试卷库 ids 不能为空")
private List<Long> libIds;

@NotBlank(message = "题目不能为空")
@@ -25,5 +25,4 @@ public class QuestionPaperDTO {
@NotBlank(message = "试卷说明不能为空")
private String description;


}
Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@ public interface QuestionPaperService extends IService<QuestionPaper> {

Long addQuestionPaper(List<Long> libIds, String title, String description);

void referencePapers(Long libId, List<Long> paperIds);

void updateQuestionPaper(Long paperId, List<Long> libIds, String title, String description);

/**
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author 马拉圈
@@ -77,6 +78,21 @@ public Long addQuestionPaper(List<Long> libIds, String title, String description
return paperId;
}

@Override
public void referencePapers(Long libId, List<Long> paperIds) {
Set<Long> hash = questionPaperMapper.getPapers(List.of(libId)).stream().map(QuestionPaper::getId).collect(Collectors.toSet());
List<LibraryPaperLink> libraryPaperLinkList = paperIds.stream()
.distinct()
.filter(paperId -> Objects.nonNull(paperId) && !hash.contains(paperId))
.map(paperId -> {
LibraryPaperLink libraryPaperLink = new LibraryPaperLink();
libraryPaperLink.setPaperId(paperId);
libraryPaperLink.setLibId(libId);
return libraryPaperLink;
}).toList();
libraryPaperLinkService.saveBatch(libraryPaperLinkList);
}

@Override
@Transactional
public void updateQuestionPaper(Long paperId, List<Long> libIds, String title, String description) {
Original file line number Diff line number Diff line change
@@ -4,9 +4,11 @@
import com.achobeta.common.annotation.Intercept;
import com.achobeta.common.enums.UserTypeEnum;
import com.achobeta.domain.paper.model.converter.LibraryConverter;
import com.achobeta.domain.question.model.dto.LibraryReferenceQuestionDTO;
import com.achobeta.domain.question.model.dto.QuestionLibraryDTO;
import com.achobeta.domain.question.model.entity.QuestionLibrary;
import com.achobeta.domain.question.service.QuestionLibraryService;
import com.achobeta.domain.question.service.QuestionService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
@@ -33,12 +35,21 @@ public class QuestionLibraryController {

private final QuestionLibraryService questionLibraryService;

private final QuestionService questionService;

@PostMapping("/create")
public SystemJsonResponse createQuestionLibrary(@RequestParam("libType") @NotBlank String libType) {
Long questionLibraryId = questionLibraryService.createQuestionLibrary(libType);
return SystemJsonResponse.SYSTEM_SUCCESS(questionLibraryId);
}

@PostMapping("/reference")
public SystemJsonResponse referenceQuestions(@Valid @RequestBody LibraryReferenceQuestionDTO libraryReferenceQuestionDTO) {
// 引用
questionService.referenceQuestions(libraryReferenceQuestionDTO.getLibId(), libraryReferenceQuestionDTO.getQuestionIds());
return SystemJsonResponse.SYSTEM_SUCCESS();
}

@PostMapping("/rename")
public SystemJsonResponse renameQuestionLibrary(@Valid @RequestBody QuestionLibraryDTO questionLibraryDTO) {
// 检查
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@ public interface QuestionMapper extends BaseMapper<Question> {
// 并不会将结果集加入 page,而是返回值 IPage 里
IPage<Question> queryQuestions(IPage<Question> page, @Param("libIds") List<Long> libIds);

List<Question> getQuestions(@Param("libIds") List<Long> libIds);

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.achobeta.domain.question.model.dto;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.util.List;

/**
* Created With Intellij IDEA
* Description:
* User: 马拉圈
* Date: 2024-10-16
* Time: 20:22
*/
@Data
public class LibraryReferenceQuestionDTO {

@NotNull(message = "题库 id 不能为空")
private Long libId;

@NotEmpty(message = "问题 id 列表不能为空")
private List<Long> questionIds;

}
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
@Data
public class QuestionLibraryDTO {

@NotNull(message = "库的 id 不能为空")
@NotNull(message = "题库 id 不能为空")
private Long libId;

@NotBlank(message = "库的类型不能为空")
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@ public interface QuestionService extends IService<Question> {

Long addQuestion(List<Long> libIds, String title, String standard);

void referenceQuestions(Long libId, List<Long> questionIds);

void saveBatchQuestion(QuestionSaveBatchDTO questionSaveBatchDTO);

void updateQuestion(Long questionId, List<Long> libIds, String title, String standard);
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author 马拉圈
@@ -90,6 +91,21 @@ public Long addQuestion(List<Long> libIds, String title, String standard) {
return questionId;
}

@Override
public void referenceQuestions(Long libId, List<Long> questionIds) {
Set<Long> hash = questionMapper.getQuestions(List.of(libId)).stream().map(Question::getId).collect(Collectors.toSet());
List<LibraryQuestionLink> libraryQuestionLinkList = questionIds.stream()
.distinct()
.filter(questionId -> Objects.nonNull(questionId) && !hash.contains(questionId))
.map(questionId -> {
LibraryQuestionLink libraryQuestionLink = new LibraryQuestionLink();
libraryQuestionLink.setQuestionId(questionId);
libraryQuestionLink.setLibId(libId);
return libraryQuestionLink;
}).toList();
libraryQuestionLinkService.saveBatch(libraryQuestionLinkList);
}

@Override
@Transactional
public void saveBatchQuestion(QuestionSaveBatchDTO questionSaveBatchDTO) {
19 changes: 19 additions & 0 deletions src/main/resources/mapper/paper/ext/QuestionPaperExtMapper.xml
Original file line number Diff line number Diff line change
@@ -31,4 +31,23 @@
order by lp_create_time asc
</select>

<select id="getPapers" parameterType="java.lang.Long"
resultMap="com.achobeta.domain.paper.model.dao.mapper.QuestionPaperMapper.BaseResultMap">
select
-- 试卷与试卷库多对多,在本条 sql 容易出现重复行
distinct p.id, p.title, p.description, p.create_time, p.update_time, lp.create_time lp_create_time
from question_paper_library l
left join library_paper_link lp on l.id = lp.lib_id and l.is_deleted = 0 and lp.is_deleted = 0
left join question_paper p on p.id = lp.paper_id and p.is_deleted = 0 and lp.is_deleted = 0
<where>
p.id is not null and l.is_deleted = 0
<if test="libIds != null">
<foreach collection="libIds" open="and l.id in (" close=")" item="libId" separator=",">
#{libId,jdbcType=BIGINT}
</foreach>
</if>
</where>
order by lp_create_time asc
</select>

</mapper>
19 changes: 19 additions & 0 deletions src/main/resources/mapper/question/ext/QuestionExtMapper.xml
Original file line number Diff line number Diff line change
@@ -30,4 +30,23 @@
</where>
order by lq_create_time asc
</select>

<select id="getQuestions" parameterType="java.lang.Long"
resultMap="com.achobeta.domain.question.model.dao.mapper.QuestionMapper.BaseResultMap">
select
-- 问题与题库多对多,在本条 sql 容易出现重复行
distinct q.id, q.title, q.standard, q.create_time, q.update_time, lq.create_time lq_create_time
from question_library l
left join library_question_link lq on l.id = lq.lib_id and l.is_deleted = 0 and lq.is_deleted = 0
left join question q on q.id = lq.question_id and q.is_deleted = 0 and lq.is_deleted = 0
<where>
q.id is not null and l.is_deleted = 0
<if test="libIds != null">
<foreach collection="libIds" open="and l.id in (" close=")" item="libId" separator=",">
#{libId,jdbcType=BIGINT}
</foreach>
</if>
</where>
order by lq_create_time asc
</select>
</mapper>

0 comments on commit 7756f86

Please sign in to comment.