From 7756f860321c677413e356b09cead5774d8388cd Mon Sep 17 00:00:00 2001 From: "s:103" <2040484356@qq.com> Date: Wed, 16 Oct 2024 21:17:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PaperLibraryController.java | 11 ++++++++ .../model/dao/mapper/QuestionPaperMapper.java | 2 ++ .../model/dto/LibraryReferencePaperDTO.java | 25 +++++++++++++++++++ .../paper/model/dto/PaperLibraryDTO.java | 2 +- .../paper/model/dto/PaperQuestionLinkDTO.java | 3 ++- .../paper/model/dto/QuestionPaperDTO.java | 5 ++-- .../paper/service/QuestionPaperService.java | 2 ++ .../impl/QuestionPaperServiceImpl.java | 16 ++++++++++++ .../controller/QuestionLibraryController.java | 11 ++++++++ .../model/dao/mapper/QuestionMapper.java | 2 ++ .../dto/LibraryReferenceQuestionDTO.java | 25 +++++++++++++++++++ .../model/dto/QuestionLibraryDTO.java | 2 +- .../question/service/QuestionService.java | 2 ++ .../service/impl/QuestionServiceImpl.java | 16 ++++++++++++ .../paper/ext/QuestionPaperExtMapper.xml | 19 ++++++++++++++ .../mapper/question/ext/QuestionExtMapper.xml | 19 ++++++++++++++ 16 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/achobeta/domain/paper/model/dto/LibraryReferencePaperDTO.java create mode 100644 src/main/java/com/achobeta/domain/question/model/dto/LibraryReferenceQuestionDTO.java diff --git a/src/main/java/com/achobeta/domain/paper/controller/PaperLibraryController.java b/src/main/java/com/achobeta/domain/paper/controller/PaperLibraryController.java index 6442b9e8..20ec0aad 100644 --- a/src/main/java/com/achobeta/domain/paper/controller/PaperLibraryController.java +++ b/src/main/java/com/achobeta/domain/paper/controller/PaperLibraryController.java @@ -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) { // 检查 diff --git a/src/main/java/com/achobeta/domain/paper/model/dao/mapper/QuestionPaperMapper.java b/src/main/java/com/achobeta/domain/paper/model/dao/mapper/QuestionPaperMapper.java index d8270db0..2b0993f1 100644 --- a/src/main/java/com/achobeta/domain/paper/model/dao/mapper/QuestionPaperMapper.java +++ b/src/main/java/com/achobeta/domain/paper/model/dao/mapper/QuestionPaperMapper.java @@ -17,6 +17,8 @@ public interface QuestionPaperMapper extends BaseMapper { // 并不会将结果集加入 page,而是返回值 IPage 里 IPage queryPapers(IPage page, @Param("libIds") List libIds); + + List getPapers(@Param("libIds") List libIds); } diff --git a/src/main/java/com/achobeta/domain/paper/model/dto/LibraryReferencePaperDTO.java b/src/main/java/com/achobeta/domain/paper/model/dto/LibraryReferencePaperDTO.java new file mode 100644 index 00000000..fc3fc0f9 --- /dev/null +++ b/src/main/java/com/achobeta/domain/paper/model/dto/LibraryReferencePaperDTO.java @@ -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 paperIds; + +} diff --git a/src/main/java/com/achobeta/domain/paper/model/dto/PaperLibraryDTO.java b/src/main/java/com/achobeta/domain/paper/model/dto/PaperLibraryDTO.java index e6de6bc9..7d9dfbde 100644 --- a/src/main/java/com/achobeta/domain/paper/model/dto/PaperLibraryDTO.java +++ b/src/main/java/com/achobeta/domain/paper/model/dto/PaperLibraryDTO.java @@ -14,7 +14,7 @@ @Data public class PaperLibraryDTO { - @NotNull(message = "库的 id 不能为空") + @NotNull(message = "试卷库 id 不能为空") private Long libId; @NotBlank(message = "库的类型不能为空") diff --git a/src/main/java/com/achobeta/domain/paper/model/dto/PaperQuestionLinkDTO.java b/src/main/java/com/achobeta/domain/paper/model/dto/PaperQuestionLinkDTO.java index 34f39141..8a149324 100644 --- a/src/main/java/com/achobeta/domain/paper/model/dto/PaperQuestionLinkDTO.java +++ b/src/main/java/com/achobeta/domain/paper/model/dto/PaperQuestionLinkDTO.java @@ -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 questionIds; } diff --git a/src/main/java/com/achobeta/domain/paper/model/dto/QuestionPaperDTO.java b/src/main/java/com/achobeta/domain/paper/model/dto/QuestionPaperDTO.java index e15294e2..24361403 100644 --- a/src/main/java/com/achobeta/domain/paper/model/dto/QuestionPaperDTO.java +++ b/src/main/java/com/achobeta/domain/paper/model/dto/QuestionPaperDTO.java @@ -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 libIds; @NotBlank(message = "题目不能为空") @@ -25,5 +25,4 @@ public class QuestionPaperDTO { @NotBlank(message = "试卷说明不能为空") private String description; - } diff --git a/src/main/java/com/achobeta/domain/paper/service/QuestionPaperService.java b/src/main/java/com/achobeta/domain/paper/service/QuestionPaperService.java index 5565f0c9..2363f269 100644 --- a/src/main/java/com/achobeta/domain/paper/service/QuestionPaperService.java +++ b/src/main/java/com/achobeta/domain/paper/service/QuestionPaperService.java @@ -21,6 +21,8 @@ public interface QuestionPaperService extends IService { Long addQuestionPaper(List libIds, String title, String description); + void referencePapers(Long libId, List paperIds); + void updateQuestionPaper(Long paperId, List libIds, String title, String description); /** diff --git a/src/main/java/com/achobeta/domain/paper/service/impl/QuestionPaperServiceImpl.java b/src/main/java/com/achobeta/domain/paper/service/impl/QuestionPaperServiceImpl.java index eff1936b..51876914 100644 --- a/src/main/java/com/achobeta/domain/paper/service/impl/QuestionPaperServiceImpl.java +++ b/src/main/java/com/achobeta/domain/paper/service/impl/QuestionPaperServiceImpl.java @@ -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 libIds, String title, String description return paperId; } + @Override + public void referencePapers(Long libId, List paperIds) { + Set hash = questionPaperMapper.getPapers(List.of(libId)).stream().map(QuestionPaper::getId).collect(Collectors.toSet()); + List 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 libIds, String title, String description) { diff --git a/src/main/java/com/achobeta/domain/question/controller/QuestionLibraryController.java b/src/main/java/com/achobeta/domain/question/controller/QuestionLibraryController.java index 75929d49..03bf1c28 100644 --- a/src/main/java/com/achobeta/domain/question/controller/QuestionLibraryController.java +++ b/src/main/java/com/achobeta/domain/question/controller/QuestionLibraryController.java @@ -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) { // 检查 diff --git a/src/main/java/com/achobeta/domain/question/model/dao/mapper/QuestionMapper.java b/src/main/java/com/achobeta/domain/question/model/dao/mapper/QuestionMapper.java index abcb0c6a..eb41d0d2 100644 --- a/src/main/java/com/achobeta/domain/question/model/dao/mapper/QuestionMapper.java +++ b/src/main/java/com/achobeta/domain/question/model/dao/mapper/QuestionMapper.java @@ -18,6 +18,8 @@ public interface QuestionMapper extends BaseMapper { // 并不会将结果集加入 page,而是返回值 IPage 里 IPage queryQuestions(IPage page, @Param("libIds") List libIds); + List getQuestions(@Param("libIds") List libIds); + } diff --git a/src/main/java/com/achobeta/domain/question/model/dto/LibraryReferenceQuestionDTO.java b/src/main/java/com/achobeta/domain/question/model/dto/LibraryReferenceQuestionDTO.java new file mode 100644 index 00000000..9de7a50b --- /dev/null +++ b/src/main/java/com/achobeta/domain/question/model/dto/LibraryReferenceQuestionDTO.java @@ -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 questionIds; + +} diff --git a/src/main/java/com/achobeta/domain/question/model/dto/QuestionLibraryDTO.java b/src/main/java/com/achobeta/domain/question/model/dto/QuestionLibraryDTO.java index 3064be85..41cea0b5 100644 --- a/src/main/java/com/achobeta/domain/question/model/dto/QuestionLibraryDTO.java +++ b/src/main/java/com/achobeta/domain/question/model/dto/QuestionLibraryDTO.java @@ -14,7 +14,7 @@ @Data public class QuestionLibraryDTO { - @NotNull(message = "库的 id 不能为空") + @NotNull(message = "题库 id 不能为空") private Long libId; @NotBlank(message = "库的类型不能为空") diff --git a/src/main/java/com/achobeta/domain/question/service/QuestionService.java b/src/main/java/com/achobeta/domain/question/service/QuestionService.java index 2bf8bce9..da9c9079 100644 --- a/src/main/java/com/achobeta/domain/question/service/QuestionService.java +++ b/src/main/java/com/achobeta/domain/question/service/QuestionService.java @@ -23,6 +23,8 @@ public interface QuestionService extends IService { Long addQuestion(List libIds, String title, String standard); + void referenceQuestions(Long libId, List questionIds); + void saveBatchQuestion(QuestionSaveBatchDTO questionSaveBatchDTO); void updateQuestion(Long questionId, List libIds, String title, String standard); diff --git a/src/main/java/com/achobeta/domain/question/service/impl/QuestionServiceImpl.java b/src/main/java/com/achobeta/domain/question/service/impl/QuestionServiceImpl.java index 6c4c475e..52c14c82 100644 --- a/src/main/java/com/achobeta/domain/question/service/impl/QuestionServiceImpl.java +++ b/src/main/java/com/achobeta/domain/question/service/impl/QuestionServiceImpl.java @@ -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 libIds, String title, String standard) { return questionId; } + @Override + public void referenceQuestions(Long libId, List questionIds) { + Set hash = questionMapper.getQuestions(List.of(libId)).stream().map(Question::getId).collect(Collectors.toSet()); + List 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) { diff --git a/src/main/resources/mapper/paper/ext/QuestionPaperExtMapper.xml b/src/main/resources/mapper/paper/ext/QuestionPaperExtMapper.xml index c6fd0986..6c027d12 100644 --- a/src/main/resources/mapper/paper/ext/QuestionPaperExtMapper.xml +++ b/src/main/resources/mapper/paper/ext/QuestionPaperExtMapper.xml @@ -31,4 +31,23 @@ order by lp_create_time asc + + diff --git a/src/main/resources/mapper/question/ext/QuestionExtMapper.xml b/src/main/resources/mapper/question/ext/QuestionExtMapper.xml index 5ddf8826..927e36ec 100644 --- a/src/main/resources/mapper/question/ext/QuestionExtMapper.xml +++ b/src/main/resources/mapper/question/ext/QuestionExtMapper.xml @@ -30,4 +30,23 @@ order by lq_create_time asc + +