From 5d3a727b73c424593687d0d3c3564d72a8735140 Mon Sep 17 00:00:00 2001 From: SOR <3272194354@qq.com> Date: Tue, 18 Aug 2026 16:58:32 +0800 Subject: [PATCH] fix: page query wrapper --- .../service/impl/AccountServiceImpl.kt | 4 +- .../service/impl/AttachmentServiceImpl.kt | 20 +++-- .../service/impl/BorrowRecordServiceImpl.kt | 43 +++++++---- .../service/impl/CategoryServiceImpl.kt | 9 ++- .../service/impl/EmailNoticeServiceImpl.kt | 16 ++-- .../service/impl/FinanceRecordServiceImpl.kt | 73 +++++++++++-------- .../itmanager/service/impl/ItemServiceImpl.kt | 69 ++++++++++++------ .../impl/ItemTransactionServiceImpl.kt | 17 +++-- 8 files changed, 162 insertions(+), 89 deletions(-) diff --git a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/AccountServiceImpl.kt b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/AccountServiceImpl.kt index 76beadc..dbcc7b1 100644 --- a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/AccountServiceImpl.kt +++ b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/AccountServiceImpl.kt @@ -50,7 +50,9 @@ class AccountServiceImpl : ServiceImpl(), AccountService override fun findAccountById(id: Long): Mono = blockingCall { logger.info("Searching account by id") - ktQuery().eq(Account::id, id).one() + ktQuery() + .eq(Account::id, id) + .one() } override fun findAccountByEmail(email: String): Mono = diff --git a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/AttachmentServiceImpl.kt b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/AttachmentServiceImpl.kt index 718093d..f07b534 100644 --- a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/AttachmentServiceImpl.kt +++ b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/AttachmentServiceImpl.kt @@ -8,7 +8,6 @@ import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import reactor.core.publisher.Mono import reactor.core.scheduler.Schedulers -import java.util.NoSuchElementException @Service class AttachmentServiceImpl : ServiceImpl(), AttachmentService { @@ -21,19 +20,26 @@ class AttachmentServiceImpl : ServiceImpl(), Attac override fun getAttachmentById(id: Long): Mono = blockingCall { logger.info("Searching attachment by id") - ktQuery().eq(Attachment::id, id).one() + ktQuery() + .eq(Attachment::id, id) + .one() }.switchIfEmpty(Mono.error(NoSuchElementException("Attachment not found"))) override fun listByRelated(relatedTable: String, relatedId: Long): Mono> = blockingCall { logger.info("Searching attachments by related table and id") - ktQuery().eq(Attachment::relatedTable, relatedTable).eq(Attachment::relatedId, relatedId).list() + ktQuery() + .eq(Attachment::relatedTable, relatedTable) + .eq(Attachment::relatedId, relatedId) + .list() } override fun listByUploader(uploadedBy: Long): Mono> = blockingCall { logger.info("Searching attachments by uploader") - ktQuery().eq(Attachment::uploadedBy, uploadedBy).list() + ktQuery() + .eq(Attachment::uploadedBy, uploadedBy) + .list() } override fun createAttachment(attachment: Attachment): Mono = @@ -57,7 +63,11 @@ class AttachmentServiceImpl : ServiceImpl(), Attac override fun deleteByRelated(relatedTable: String, relatedId: Long): Mono = blockingCall { logger.info("Deleting attachments by related table and id") - remove(ktQuery().eq(Attachment::relatedTable, relatedTable).eq(Attachment::relatedId, relatedId)) + remove( + ktQuery() + .eq(Attachment::relatedTable, relatedTable) + .eq(Attachment::relatedId, relatedId) + ) } override fun exportAllAttachments(): Mono> = diff --git a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/BorrowRecordServiceImpl.kt b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/BorrowRecordServiceImpl.kt index b268263..d201777 100644 --- a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/BorrowRecordServiceImpl.kt +++ b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/BorrowRecordServiceImpl.kt @@ -3,6 +3,7 @@ package club.itstudio.itmanager.service.impl import club.itstudio.itmanager.entity.dto.BorrowRecord import club.itstudio.itmanager.mapper.BorrowRecordMapper import club.itstudio.itmanager.service.BorrowRecordService +import com.baomidou.mybatisplus.core.metadata.OrderItem import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper import com.baomidou.mybatisplus.extension.plugins.pagination.Page import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl @@ -10,7 +11,6 @@ import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import reactor.core.publisher.Mono import reactor.core.scheduler.Schedulers -import java.util.NoSuchElementException @Service class BorrowRecordServiceImpl : ServiceImpl(), BorrowRecordService { @@ -23,7 +23,9 @@ class BorrowRecordServiceImpl : ServiceImpl(), override fun getBorrowRecordById(id: Long): Mono = blockingCall { logger.info("Searching borrow record by id") - ktQuery().eq(BorrowRecord::id, id).one() + ktQuery() + .eq(BorrowRecord::id, id) + .one() }.switchIfEmpty(Mono.error(NoSuchElementException("BorrowRecord not found"))) override fun pageUsages( @@ -33,39 +35,52 @@ class BorrowRecordServiceImpl : ServiceImpl(), ): Mono> = blockingCall { logger.info("Paging borrow records") - val wrapper = KtQueryWrapper(BorrowRecord()) - if (!keyword.isNullOrBlank()) { - wrapper.like(BorrowRecord::borrower, keyword) + if (keyword.isNullOrBlank() && status.isNullOrBlank()) { + page.addOrder(OrderItem.desc("created_at")) + page(page) + } else { + val wrapper = KtQueryWrapper(BorrowRecord()) + if (!keyword.isNullOrBlank()) { + wrapper.like(BorrowRecord::borrower, keyword) + } + if (!status.isNullOrBlank()) { + wrapper.eq(BorrowRecord::status, status) + } + wrapper.orderByDesc(BorrowRecord::createdAt) + page(page, wrapper) } - if (!status.isNullOrBlank()) { - wrapper.eq(BorrowRecord::status, status) - } - wrapper.orderByDesc(BorrowRecord::createdAt) - page(page, wrapper) } override fun listByItem(itemId: Long): Mono> = blockingCall { logger.info("Searching borrow records by item id") - ktQuery().eq(BorrowRecord::itemId, itemId).list() + ktQuery() + .eq(BorrowRecord::itemId, itemId) + .list() } override fun listByBorrower(borrower: String): Mono> = blockingCall { logger.info("Searching borrow records by borrower") - ktQuery().eq(BorrowRecord::borrower, borrower).list() + ktQuery() + .eq(BorrowRecord::borrower, borrower) + .list() } override fun listByStatus(status: String): Mono> = blockingCall { logger.info("Searching borrow records by status") - ktQuery().eq(BorrowRecord::status, status).list() + ktQuery() + .eq(BorrowRecord::status, status) + .list() } override fun listOverdueRecords(): Mono> = blockingCall { logger.info("Searching overdue borrow records") - ktQuery().eq(BorrowRecord::status, "OVERDUE").list() + ktQuery() + .eq(BorrowRecord::status, "OVERDUE") + .list() } override fun createBorrowRecord(record: BorrowRecord): Mono = diff --git a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/CategoryServiceImpl.kt b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/CategoryServiceImpl.kt index 841bb9c..a9f39ce 100644 --- a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/CategoryServiceImpl.kt +++ b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/CategoryServiceImpl.kt @@ -8,7 +8,6 @@ import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import reactor.core.publisher.Mono import reactor.core.scheduler.Schedulers -import java.util.NoSuchElementException @Service class CategoryServiceImpl : ServiceImpl(), CategoryService { @@ -21,7 +20,9 @@ class CategoryServiceImpl : ServiceImpl(), CategorySer override fun getCategoryById(id: Long): Mono = blockingCall { logger.info("Searching category by id") - ktQuery().eq(Category::id, id).one() + ktQuery() + .eq(Category::id, id) + .one() }.switchIfEmpty(Mono.error(NoSuchElementException("Category not found"))) override fun listAllCategories(): Mono> = @@ -33,7 +34,9 @@ class CategoryServiceImpl : ServiceImpl(), CategorySer override fun listByParentId(parentId: Long): Mono> = blockingCall { logger.info("Searching categories by parent id") - ktQuery().eq(Category::parentId, parentId).list() + ktQuery() + .eq(Category::parentId, parentId) + .list() } override fun createCategory(category: Category): Mono = diff --git a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/EmailNoticeServiceImpl.kt b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/EmailNoticeServiceImpl.kt index a176e28..e5d8397 100644 --- a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/EmailNoticeServiceImpl.kt +++ b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/EmailNoticeServiceImpl.kt @@ -8,7 +8,6 @@ import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import reactor.core.publisher.Mono import reactor.core.scheduler.Schedulers -import java.util.NoSuchElementException @Service class EmailNoticeServiceImpl : ServiceImpl(), EmailNoticeService { @@ -21,7 +20,9 @@ class EmailNoticeServiceImpl : ServiceImpl(), Em override fun getEmailNoticeById(id: Long): Mono = blockingCall { logger.info("Searching email notice by id") - ktQuery().eq(EmailNotice::id, id).one() + ktQuery() + .eq(EmailNotice::id, id) + .one() }.switchIfEmpty(Mono.error(NoSuchElementException("EmailNotice not found"))) override fun listAllNotices(): Mono> = @@ -33,7 +34,9 @@ class EmailNoticeServiceImpl : ServiceImpl(), Em override fun listEnabledNotices(): Mono> = blockingCall { logger.info("Listing enabled email notices") - ktQuery().eq(EmailNotice::enabled, true).list() + ktQuery() + .eq(EmailNotice::enabled, true) + .list() } override fun createEmailNotice(notice: EmailNotice): Mono = @@ -57,11 +60,8 @@ class EmailNoticeServiceImpl : ServiceImpl(), Em override fun toggleEnabled(id: Long): Mono = blockingCall { val notice = ktQuery().eq(EmailNotice::id, id).one() - if (notice != null) { - updateById(notice.copy(enabled = !notice.enabled)) - } else { - false - } + notice != null + && updateById(notice.copy(enabled = !notice.enabled)) } override fun exportAllEmailNotices(): Mono> = diff --git a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/FinanceRecordServiceImpl.kt b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/FinanceRecordServiceImpl.kt index 22863bd..dc7a2c0 100644 --- a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/FinanceRecordServiceImpl.kt +++ b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/FinanceRecordServiceImpl.kt @@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import reactor.core.publisher.Mono import reactor.core.scheduler.Schedulers -import java.util.NoSuchElementException @Service class FinanceRecordServiceImpl : ServiceImpl(), FinanceRecordService { @@ -22,7 +21,9 @@ class FinanceRecordServiceImpl : ServiceImpl override fun getFinanceRecordById(id: Long): Mono = blockingCall { logger.info("Searching finance record by id") - ktQuery().eq(FinanceRecord::id, id).one() + ktQuery() + .eq(FinanceRecord::id, id) + .one() }.switchIfEmpty(Mono.error(NoSuchElementException("FinanceRecord not found"))) override fun pageFinanceRecords( @@ -36,55 +37,69 @@ class FinanceRecordServiceImpl : ServiceImpl ): Mono> = blockingCall { logger.info("Paging finance records") - val wrapper = ktQuery() - if (!keyword.isNullOrBlank()) { - wrapper.and { - it.like(FinanceRecord::recordNo, keyword) - .or().like(FinanceRecord::vendor, keyword) - .or().like(FinanceRecord::note, keyword) + if (keyword.isNullOrBlank() && type.isNullOrBlank() && start.isNullOrBlank() && + end.isNullOrBlank() && vendor.isNullOrBlank() && itemId == null + ) { + page(page) + } else { + val wrapper = ktQuery() + if (!keyword.isNullOrBlank()) { + wrapper.and { + it.like(FinanceRecord::recordNo, keyword) + .or().like(FinanceRecord::vendor, keyword) + .or().like(FinanceRecord::note, keyword) + } } + if (!type.isNullOrBlank()) { + wrapper.eq(FinanceRecord::type, type) + } + if (!start.isNullOrBlank() && !end.isNullOrBlank()) { + wrapper.apply("occurred_at BETWEEN {0} AND {1}", start, end) + } else if (!start.isNullOrBlank()) { + wrapper.ge(FinanceRecord::occurredAt, start) + } else if (!end.isNullOrBlank()) { + wrapper.le(FinanceRecord::occurredAt, end) + } + if (!vendor.isNullOrBlank()) { + wrapper.eq(FinanceRecord::vendor, vendor) + } + if (itemId != null) { + wrapper.eq(FinanceRecord::relatedItemId, itemId) + } + page(page, wrapper) } - if (!type.isNullOrBlank()) { - wrapper.eq(FinanceRecord::type, type) - } - if (!start.isNullOrBlank() && !end.isNullOrBlank()) { - wrapper.apply("occurred_at BETWEEN {0} AND {1}", start, end) - } else if (!start.isNullOrBlank()) { - wrapper.ge(FinanceRecord::occurredAt, start) - } else if (!end.isNullOrBlank()) { - wrapper.le(FinanceRecord::occurredAt, end) - } - if (!vendor.isNullOrBlank()) { - wrapper.eq(FinanceRecord::vendor, vendor) - } - if (itemId != null) { - wrapper.eq(FinanceRecord::relatedItemId, itemId) - } - page(page, wrapper) } override fun listByType(type: String): Mono> = blockingCall { logger.info("Searching finance records by type") - ktQuery().eq(FinanceRecord::type, type).list() + ktQuery() + .eq(FinanceRecord::type, type) + .list() } override fun listByDateRange(start: String, end: String): Mono> = blockingCall { logger.info("Searching finance records by date range") - ktQuery().apply("occurred_at BETWEEN {0} AND {1}", start, end).list() + ktQuery() + .apply("occurred_at BETWEEN {0} AND {1}", start, end) + .list() } override fun listByVendor(vendor: String): Mono> = blockingCall { logger.info("Searching finance records by vendor") - ktQuery().eq(FinanceRecord::vendor, vendor).list() + ktQuery() + .eq(FinanceRecord::vendor, vendor) + .list() } override fun listByRelatedItem(itemId: Long): Mono> = blockingCall { logger.info("Searching finance records by related item id") - ktQuery().eq(FinanceRecord::relatedItemId, itemId).list() + ktQuery() + .eq(FinanceRecord::relatedItemId, itemId) + .list() } override fun createFinanceRecord(record: FinanceRecord): Mono = diff --git a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/ItemServiceImpl.kt b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/ItemServiceImpl.kt index 7d73c58..85a7c98 100644 --- a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/ItemServiceImpl.kt +++ b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/ItemServiceImpl.kt @@ -7,9 +7,9 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl import org.slf4j.LoggerFactory import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional import reactor.core.publisher.Mono import reactor.core.scheduler.Schedulers -import java.util.NoSuchElementException @Service class ItemServiceImpl : ServiceImpl(), ItemService { @@ -22,40 +22,54 @@ class ItemServiceImpl : ServiceImpl(), ItemService { override fun pageItems(page: Page, keyword: String?, status: String?): Mono> = blockingCall { logger.info("Paging items with keyword and status") - val wrapper = ktQuery() - if (!keyword.isNullOrBlank()) { - wrapper.and { - it.like(Item::name, keyword).or().like(Item::code, keyword) + if (keyword.isNullOrBlank() && status.isNullOrBlank()) { + page(page) + } else { + val wrapper = ktQuery() + if (!keyword.isNullOrBlank()) { + wrapper.and { + it.like(Item::name, keyword) + .or().like(Item::code, keyword) + } } + if (!status.isNullOrBlank()) { + wrapper.eq(Item::status, status) + } + page(page, wrapper) } - if (!status.isNullOrBlank()) { - wrapper.eq(Item::status, status) - } - page(page, wrapper) } override fun getItemById(id: Long): Mono = blockingCall { logger.info("Searching item by id") - ktQuery().eq(Item::id, id).one() + ktQuery() + .eq(Item::id, id) + .one() }.switchIfEmpty(Mono.error(NoSuchElementException("Item not found"))) override fun listAvailableItems(): Mono> = blockingCall { logger.info("Listing available items") - ktQuery().gt(Item::availableQuantity, 0).eq(Item::status, "normal").list() + ktQuery() + .gt(Item::availableQuantity, 0) + .eq(Item::status, "normal") + .list() } override fun listInUseItems(): Mono> = blockingCall { logger.info("Listing in-use items") - ktQuery().apply("total_quantity - available_quantity > 0").list() + ktQuery() + .apply("total_quantity - available_quantity > 0") + .list() } override fun listItemsByCategory(categoryId: Long): Mono> = blockingCall { logger.info("Listing items by category") - ktQuery().eq(Item::categoryId, categoryId).list() + ktQuery() + .eq(Item::categoryId, categoryId) + .list() } override fun createItem(item: Item): Mono = @@ -79,35 +93,42 @@ class ItemServiceImpl : ServiceImpl(), ItemService { override fun setPrimaryImage(id: Long, imagePath: String): Mono = blockingCall { logger.info("Setting primary image for item") - ktUpdate().eq(Item::id, id).set(Item::imagePath, imagePath).update() + ktUpdate() + .eq(Item::id, id) + .set(Item::imagePath, imagePath) + .update() } override fun borrowItem(id: Long, quantity: Int): Mono = blockingCall { val item = ktQuery().eq(Item::id, id).one() - if (item != null && item.availableQuantity >= quantity) { - ktUpdate().eq(Item::id, id).set(Item::availableQuantity, item.availableQuantity - quantity).update() - } else { - false - } + item != null + && item.availableQuantity >= quantity + && ktUpdate() + .eq(Item::id, id) + .set(Item::availableQuantity, item.availableQuantity - quantity) + .update() } override fun returnItem(id: Long, quantity: Int): Mono = blockingCall { val item = ktQuery().eq(Item::id, id).one() - if (item != null && item.totalQuantity >= item.availableQuantity + quantity) { - ktUpdate().eq(Item::id, id).set(Item::availableQuantity, item.availableQuantity + quantity).update() - } else { - false - } + item != null + && item.totalQuantity >= item.availableQuantity + quantity + && ktUpdate() + .eq(Item::id, id) + .set(Item::availableQuantity, item.availableQuantity + quantity) + .update() } + @Transactional override fun batchInsertItems(items: List): Mono = blockingCall { logger.info("Batch inserting items") saveBatch(items) } + @Transactional override fun replaceAllItems(items: List): Mono = blockingCall { logger.info("Replacing all items") diff --git a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/ItemTransactionServiceImpl.kt b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/ItemTransactionServiceImpl.kt index b5447a5..e323f90 100644 --- a/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/ItemTransactionServiceImpl.kt +++ b/src/backend/src/main/kotlin/club/itstudio/itmanager/service/impl/ItemTransactionServiceImpl.kt @@ -8,7 +8,6 @@ import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import reactor.core.publisher.Mono import reactor.core.scheduler.Schedulers -import java.util.NoSuchElementException @Service class ItemTransactionServiceImpl : ServiceImpl(), ItemTransactionService { @@ -21,25 +20,33 @@ class ItemTransactionServiceImpl : ServiceImpl = blockingCall { logger.info("Searching transaction by id") - ktQuery().eq(ItemTransaction::id, id).one() + ktQuery() + .eq(ItemTransaction::id, id) + .one() }.switchIfEmpty(Mono.error(NoSuchElementException("ItemTransaction not found"))) override fun listByItem(itemId: Long): Mono> = blockingCall { logger.info("Searching transactions by item id") - ktQuery().eq(ItemTransaction::itemId, itemId).list() + ktQuery() + .eq(ItemTransaction::itemId, itemId) + .list() } override fun listByType(transType: String): Mono> = blockingCall { logger.info("Searching transactions by type") - ktQuery().eq(ItemTransaction::transType, transType).list() + ktQuery() + .eq(ItemTransaction::transType, transType) + .list() } override fun listByOperator(operatorId: Long): Mono> = blockingCall { logger.info("Searching transactions by operator id") - ktQuery().eq(ItemTransaction::operatorId, operatorId).list() + ktQuery() + .eq(ItemTransaction::operatorId, operatorId) + .list() } override fun createTransaction(transaction: ItemTransaction): Mono =