Skip to content

Commit

Permalink
🎨 feat: 优化字典查询
Browse files Browse the repository at this point in the history
  • Loading branch information
cokie committed Jan 31, 2024
1 parent fdd5363 commit a0fb401
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ public class SystemDictController {

@Operation(summary = "查询系统数据字典")
@GET
public Uni<R<PageDTO<QuerySystemDictDTO>>> getSystemDictPage(@QueryParam("page") @DefaultValue("1") int page, @QueryParam("size") @DefaultValue("10") int size) {
return systemDictService.findDictPage(Pageable.of(page, size))
public Uni<R<PageDTO<QuerySystemDictDTO>>> getSystemDictPage(
@QueryParam("keyword") String keyword,
@QueryParam("page") @DefaultValue("1") int page,
@QueryParam("size") @DefaultValue("10") int size) {
return systemDictService.findDictPage(keyword, Pageable.of(page, size))
.map(paged -> {
List<QuerySystemDictDTO> list = paged.getFirst().stream()
.map(it -> systemDictConverter.systemDictDO2QuerySystemDictDTO(it))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package io.micro.server.dict.domain.repository

import io.micro.core.rest.Pageable
import io.micro.server.dict.domain.model.entity.SystemDictDO
import io.quarkus.panache.common.Page
import io.smallrye.mutiny.Uni

interface ISystemDictRepository {

fun findSystemDictPage(page: Page): Uni<List<SystemDictDO>>
fun findSystemDictByKeyLike(keyword: String, pageable: Pageable): Uni<List<SystemDictDO>>

fun countSystemDictPage(): Uni<Long>
fun findSystemDict(pageable: Pageable): Uni<List<SystemDictDO>>

fun countSystemDict(): Uni<Long>

fun countSystemDictByKeyLike(keyword: String): Uni<Long>

fun findById(id: Long): Uni<SystemDictDO>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.smallrye.mutiny.Uni

interface SystemDictService {

fun findDictPage(pageable: Pageable): Uni<Pair<List<SystemDictDO>, Long>>
fun findDictPage(keyword: String?, pageable: Pageable): Uni<Pair<List<SystemDictDO>, Long>>

fun saveOrUpdateDict(systemDictDO: SystemDictDO): Uni<SystemDictDO>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ import io.micro.server.dict.domain.repository.ISystemDictRepository
import io.micro.server.dict.domain.service.SystemDictService
import io.quarkus.hibernate.reactive.panache.common.WithSession
import io.quarkus.hibernate.reactive.panache.common.WithTransaction
import io.quarkus.panache.common.Page
import io.smallrye.mutiny.Uni
import jakarta.enterprise.context.ApplicationScoped

@ApplicationScoped
class SystemDictServiceImpl(private val systemDictRepository: ISystemDictRepository) : SystemDictService {

@WithSession
override fun findDictPage(pageable: Pageable): Uni<Pair<List<SystemDictDO>, Long>> {
val page = Page.of(pageable.current - 1, pageable.limit)
return systemDictRepository.findSystemDictPage(page).flatMap { list ->
systemDictRepository.countSystemDictPage().map { list to it }
override fun findDictPage(keyword: String?, pageable: Pageable): Uni<Pair<List<SystemDictDO>, Long>> {
return if (!keyword.isNullOrBlank()) {
systemDictRepository.findSystemDictByKeyLike(keyword, pageable).flatMap { list ->
systemDictRepository.countSystemDictByKeyLike(keyword).map { list to it }
}
} else {
systemDictRepository.findSystemDict(pageable).flatMap { list ->
systemDictRepository.countSystemDict().map { list to it }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package io.micro.server.dict.infra.dao

import io.micro.server.dict.infra.po.SystemDictEntity
import io.quarkus.hibernate.reactive.panache.kotlin.PanacheRepository
import io.quarkus.panache.common.Page
import io.smallrye.mutiny.Uni

interface ISystemDictDAO : PanacheRepository<SystemDictEntity> {

fun selectByKey(key: String): Uni<SystemDictEntity>

fun selectByKeyLike(key: String, page: Page): Uni<List<SystemDictEntity>>

fun countByKeyLike(key: String): Uni<Long>

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.micro.server.dict.infra.dao.impl

import io.micro.server.dict.infra.dao.ISystemDictDAO
import io.micro.server.dict.infra.po.SystemDictEntity
import io.quarkus.panache.common.Page
import io.smallrye.mutiny.Uni
import jakarta.enterprise.context.ApplicationScoped

Expand All @@ -12,4 +13,12 @@ class SystemDictDAO : ISystemDictDAO {
return find("key = ?1", key).singleResult()
}

override fun selectByKeyLike(key: String, page: Page): Uni<List<SystemDictEntity>> {
return find("key like concat('%', ?1, '%')", key).page(page).list()
}

override fun countByKeyLike(key: String): Uni<Long> {
return find("key like concat('%', ?1, '%')", key).count()
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.micro.server.dict.infra.repository

import io.micro.core.rest.Pageable
import io.micro.core.util.converter
import io.micro.server.dict.domain.model.entity.SystemDictDO
import io.micro.server.dict.domain.repository.ISystemDictRepository
Expand All @@ -15,17 +16,27 @@ class SystemDictRepository(
private val systemDictConverter: SystemDictConverter
) : ISystemDictRepository {

override fun findSystemDictPage(page: Page): Uni<List<SystemDictDO>> {
override fun findSystemDictByKeyLike(keyword: String, pageable: Pageable): Uni<List<SystemDictDO>> {
val page = Page.of(pageable.current - 1, pageable.limit)
return systemDictDAO.selectByKeyLike(keyword, page)
.map { it.converter(systemDictConverter::systemDictEntity2SystemDictDO) }
}

override fun findSystemDict(pageable: Pageable): Uni<List<SystemDictDO>> {
val page = Page.of(pageable.current - 1, pageable.limit)
return systemDictDAO.findAll()
.page(page)
.list()
.page(page).list()
.map { it.converter(systemDictConverter::systemDictEntity2SystemDictDO) }
}

override fun countSystemDictPage(): Uni<Long> {
override fun countSystemDict(): Uni<Long> {
return systemDictDAO.findAll().count()
}

override fun countSystemDictByKeyLike(keyword: String): Uni<Long> {
return systemDictDAO.countByKeyLike(keyword)
}

override fun findById(id: Long): Uni<SystemDictDO> {
return systemDictDAO.findById(id).map(systemDictConverter::systemDictEntity2SystemDictDO)
}
Expand Down

0 comments on commit a0fb401

Please sign in to comment.