From 70b042541f32b79696e16fb90d2631b309247759 Mon Sep 17 00:00:00 2001 From: AkagiYui Date: Sun, 25 Aug 2024 12:13:47 +0800 Subject: [PATCH] fix: use metamodel for specification --- .../service/impl/AnnouncementServiceImpl.kt | 7 ++++--- .../drive/service/impl/FileInfoServiceImpl.kt | 12 +++++++----- .../drive/service/impl/RoleServiceImpl.kt | 12 +++++++----- .../drive/service/impl/UserServiceImpl.kt | 16 ++++++++-------- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/com/akagiyui/drive/service/impl/AnnouncementServiceImpl.kt b/app/src/main/kotlin/com/akagiyui/drive/service/impl/AnnouncementServiceImpl.kt index 1b791ea..be7a5a9 100644 --- a/app/src/main/kotlin/com/akagiyui/drive/service/impl/AnnouncementServiceImpl.kt +++ b/app/src/main/kotlin/com/akagiyui/drive/service/impl/AnnouncementServiceImpl.kt @@ -4,6 +4,7 @@ import com.akagiyui.common.ResponseEnum import com.akagiyui.common.exception.CustomException import com.akagiyui.common.utils.hasText import com.akagiyui.drive.entity.Announcement +import com.akagiyui.drive.entity.Announcement_ import com.akagiyui.drive.model.AnnouncementFilter import com.akagiyui.drive.model.request.announcement.UpdateAnnouncementRequest import com.akagiyui.drive.repository.AnnouncementRepository @@ -51,11 +52,11 @@ class AnnouncementServiceImpl(private val announcementRepository: AnnouncementRe val pageable = PageRequest.of(index, size) // 条件查询 - val specification = Specification { root, _, cb -> + val specification = Specification { root, _, cb -> filter?.expression.hasText { val likePattern = "%$it%" - val titlePredicate = cb.like(root.get("title"), likePattern) - val contentPredicate = cb.like(root.get("content"), likePattern) + val titlePredicate = cb.like(root[Announcement_.title], likePattern) + val contentPredicate = cb.like(root[Announcement_.content], likePattern) cb.or(titlePredicate, contentPredicate) } } diff --git a/app/src/main/kotlin/com/akagiyui/drive/service/impl/FileInfoServiceImpl.kt b/app/src/main/kotlin/com/akagiyui/drive/service/impl/FileInfoServiceImpl.kt index f66bcb7..9a28f40 100644 --- a/app/src/main/kotlin/com/akagiyui/drive/service/impl/FileInfoServiceImpl.kt +++ b/app/src/main/kotlin/com/akagiyui/drive/service/impl/FileInfoServiceImpl.kt @@ -7,6 +7,7 @@ import com.akagiyui.common.delegate.LoggerDelegate import com.akagiyui.common.exception.CustomException import com.akagiyui.common.utils.hasText import com.akagiyui.drive.entity.FileInfo +import com.akagiyui.drive.entity.FileInfo_ import com.akagiyui.drive.entity.User import com.akagiyui.drive.model.CacheConstants import com.akagiyui.drive.model.FileInfoFilter @@ -123,12 +124,13 @@ class FileInfoServiceImpl( val pageable = PageRequest.of(pageIndex, pageSize) // 条件查询 - val specification = Specification { root, _, cb -> - if (filter != null && filter.expression.hasText()) { - val namePredicate = cb.like(root.get("name"), "%${filter.expression}%") - val typePredicate = cb.like(root.get("type"), "%${filter.expression}%") + val specification = Specification { root, _, cb -> + filter?.expression.hasText { + val likePattern = "%$it%" + val namePredicate = cb.like(root[FileInfo_.name], likePattern) + val typePredicate = cb.like(root[FileInfo_.type], likePattern) cb.or(namePredicate, typePredicate) - } else null + } } return fileInfoRepository.findAll(specification, pageable) diff --git a/app/src/main/kotlin/com/akagiyui/drive/service/impl/RoleServiceImpl.kt b/app/src/main/kotlin/com/akagiyui/drive/service/impl/RoleServiceImpl.kt index 243bb15..f519f12 100644 --- a/app/src/main/kotlin/com/akagiyui/drive/service/impl/RoleServiceImpl.kt +++ b/app/src/main/kotlin/com/akagiyui/drive/service/impl/RoleServiceImpl.kt @@ -5,6 +5,7 @@ import com.akagiyui.common.delegate.LoggerDelegate import com.akagiyui.common.exception.CustomException import com.akagiyui.common.utils.hasText import com.akagiyui.drive.entity.Role +import com.akagiyui.drive.entity.Role_ import com.akagiyui.drive.entity.User import com.akagiyui.drive.model.CacheConstants import com.akagiyui.drive.model.Permission @@ -55,12 +56,13 @@ class RoleServiceImpl(private val roleRepository: RoleRepository) : RoleService val pageable = PageRequest.of(index, size) // 条件查询 - val specification = Specification { root, _, cb -> - if (filter != null && filter.expression.hasText()) { - val namePredicate = cb.like(root.get("name"), "%${filter.expression}%") - val descriptionPredicate = cb.like(root.get("description"), "%${filter.expression}%") + val specification = Specification { root, _, cb -> + filter?.expression.hasText { + val likePattern = "%$it%" + val namePredicate = cb.like(root[Role_.name], likePattern) + val descriptionPredicate = cb.like(root[Role_.description], likePattern) cb.or(namePredicate, descriptionPredicate) - } else null + } } return roleRepository.findAll(specification, pageable) diff --git a/app/src/main/kotlin/com/akagiyui/drive/service/impl/UserServiceImpl.kt b/app/src/main/kotlin/com/akagiyui/drive/service/impl/UserServiceImpl.kt index bd1133f..479c310 100644 --- a/app/src/main/kotlin/com/akagiyui/drive/service/impl/UserServiceImpl.kt +++ b/app/src/main/kotlin/com/akagiyui/drive/service/impl/UserServiceImpl.kt @@ -9,6 +9,7 @@ import com.akagiyui.common.utils.hasText import com.akagiyui.drive.component.RedisCache import com.akagiyui.drive.entity.Role import com.akagiyui.drive.entity.User +import com.akagiyui.drive.entity.User_ import com.akagiyui.drive.model.AddUserModel import com.akagiyui.drive.model.CacheConstants import com.akagiyui.drive.model.UserFilter @@ -17,7 +18,6 @@ import com.akagiyui.drive.model.request.user.UpdateUserInfoRequest import com.akagiyui.drive.repository.UserRepository import com.akagiyui.drive.service.* import jakarta.annotation.Resource -import jakarta.persistence.criteria.Predicate import org.jetbrains.annotations.NotNull import org.springframework.cache.annotation.CacheEvict import org.springframework.cache.annotation.Cacheable @@ -77,14 +77,14 @@ class UserServiceImpl( override fun getUsers(index: Int, size: Int, userFilter: UserFilter?): Page { val pageable: Pageable = PageRequest.of(index, size) - val specification: Specification = Specification { root, _, cb -> - if (userFilter != null && userFilter.expression.hasText()) { - val queryString = "%${userFilter.expression}%" - val usernamePredicate: Predicate = cb.like(root["username"], queryString) - val nicknamePredicate: Predicate = cb.like(root["nickname"], queryString) - val emailPredicate: Predicate = cb.like(root["email"], queryString) + val specification = Specification { root, _, cb -> + userFilter?.expression.hasText { + val queryString = "%$it%" + val usernamePredicate = cb.like(root[User_.username], queryString) + val nicknamePredicate = cb.like(root[User_.nickname], queryString) + val emailPredicate = cb.like(root[User_.email], queryString) cb.or(usernamePredicate, nicknamePredicate, emailPredicate) - } else null + } } return repository.findAll(specification, pageable)