From c6ed3913ab2d8dfb8fa82aa9cc78ff940f29dbb0 Mon Sep 17 00:00:00 2001 From: dragove Date: Mon, 18 Mar 2024 23:50:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=9C=E4=B8=9A=E9=9B=86=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8A=9F=E8=83=BD=E6=94=AF=E6=8C=81=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E4=BD=9C=E4=B8=9Aid=E5=88=97=E8=A1=A8=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/converter/CopilotSetConverter.kt | 3 +- .../request/copilotset/CopilotSetQuery.kt | 5 ++- .../maa/backend/service/CopilotSetService.kt | 33 +++++++++++++++---- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/plus/maa/backend/common/utils/converter/CopilotSetConverter.kt b/src/main/kotlin/plus/maa/backend/common/utils/converter/CopilotSetConverter.kt index 6261ae97..f07c1c5b 100644 --- a/src/main/kotlin/plus/maa/backend/common/utils/converter/CopilotSetConverter.kt +++ b/src/main/kotlin/plus/maa/backend/common/utils/converter/CopilotSetConverter.kt @@ -3,8 +3,8 @@ package plus.maa.backend.common.utils.converter import org.mapstruct.Mapper import org.mapstruct.Mapping import plus.maa.backend.controller.request.copilotset.CopilotSetCreateReq -import plus.maa.backend.controller.response.copilotset.CopilotSetRes import plus.maa.backend.controller.response.copilotset.CopilotSetListRes +import plus.maa.backend.controller.response.copilotset.CopilotSetRes import plus.maa.backend.repository.entity.CopilotSet import java.time.LocalDateTime @@ -23,7 +23,6 @@ interface CopilotSetConverter { @Mapping(target = "updateTime", expression = "java(LocalDateTime.now())") fun convert(createReq: CopilotSetCreateReq, id: Long, creatorId: String): CopilotSet - @Mapping(target = "creator", ignore = true) fun convert(copilotSet: CopilotSet, creator: String): CopilotSetListRes fun convertDetail(copilotSet: CopilotSet, creator: String): CopilotSetRes diff --git a/src/main/kotlin/plus/maa/backend/controller/request/copilotset/CopilotSetQuery.kt b/src/main/kotlin/plus/maa/backend/controller/request/copilotset/CopilotSetQuery.kt index bdd3642d..2c423273 100644 --- a/src/main/kotlin/plus/maa/backend/controller/request/copilotset/CopilotSetQuery.kt +++ b/src/main/kotlin/plus/maa/backend/controller/request/copilotset/CopilotSetQuery.kt @@ -21,5 +21,8 @@ data class CopilotSetQuery ( ) Int = 10, @Schema(title = "查询关键词") - val keyword: String? = null + val keyword: String? = null, + + @Schema(title = "需要包含的作业id列表") + val copilotIds: List? = null ) diff --git a/src/main/kotlin/plus/maa/backend/service/CopilotSetService.kt b/src/main/kotlin/plus/maa/backend/service/CopilotSetService.kt index 3f502a69..d2680b29 100644 --- a/src/main/kotlin/plus/maa/backend/service/CopilotSetService.kt +++ b/src/main/kotlin/plus/maa/backend/service/CopilotSetService.kt @@ -4,6 +4,10 @@ import cn.hutool.core.lang.Assert import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.data.domain.PageRequest import org.springframework.data.domain.Sort +import org.springframework.data.mongodb.core.MongoTemplate +import org.springframework.data.mongodb.core.query.Criteria +import org.springframework.data.mongodb.core.query.Query +import org.springframework.data.support.PageableExecutionUtils import org.springframework.stereotype.Service import plus.maa.backend.common.utils.IdComponent import plus.maa.backend.common.utils.converter.CopilotSetConverter @@ -18,7 +22,9 @@ import plus.maa.backend.repository.UserRepository import plus.maa.backend.repository.entity.CopilotSet import plus.maa.backend.repository.entity.MaaUser import plus.maa.backend.repository.findByUsersId +import plus.maa.backend.service.model.CopilotSetStatus import java.time.LocalDateTime +import java.util.regex.Pattern private val log = KotlinLogging.logger { } @@ -32,6 +38,7 @@ class CopilotSetService( private val converter: CopilotSetConverter, private val repository: CopilotSetRepository, private val userRepository: UserRepository, + private val mongoTemplate: MongoTemplate ) { private val defaultSort: Sort = Sort.by("id").descending() @@ -106,13 +113,27 @@ class CopilotSetService( fun query(req: CopilotSetQuery): CopilotSetPageRes { val pageRequest = PageRequest.of(req.page - 1, req.limit, defaultSort) - val keyword = req.keyword - val copilotSets = if (keyword.isNullOrBlank()) { - repository.findAll(pageRequest) - } else { - repository.findByKeyword(keyword, pageRequest) + val query = Query.query( + Criteria.where("status").`is`(CopilotSetStatus.PUBLIC).and("delete") + .`is`(false) + ).with(pageRequest) + if (!req.copilotIds.isNullOrEmpty()) { + query.addCriteria(Criteria.where("copilotIds").all(req.copilotIds)).with(pageRequest) } - + if (!req.keyword.isNullOrBlank()) { + val pattern = Pattern.compile(req.keyword, Pattern.CASE_INSENSITIVE) + query.addCriteria( + Criteria().orOperator(Criteria.where("name").regex(pattern), + Criteria.where("description").regex(pattern)) + ) + } + val copilotSets = + PageableExecutionUtils.getPage(mongoTemplate.find(query, CopilotSet::class.java), pageRequest) { + mongoTemplate.count( + query.limit(-1).skip(-1), + CopilotSet::class.java + ) + } val userIds = copilotSets .map { obj: CopilotSet -> obj.creatorId } .distinct()