Skip to content

Commit

Permalink
feat: 作业集查询功能支持根据作业id列表查询
Browse files Browse the repository at this point in the history
  • Loading branch information
dragove committed Mar 18, 2024
1 parent 092890e commit c6ed391
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long>? = null
)
33 changes: 27 additions & 6 deletions src/main/kotlin/plus/maa/backend/service/CopilotSetService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 { }

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c6ed391

Please sign in to comment.