From 3527bc056079026289d631b0edd2a839dfc2e5b4 Mon Sep 17 00:00:00 2001 From: "B.E.N.S.O.N" Date: Tue, 20 Jan 2026 17:17:01 +0800 Subject: [PATCH] Supporting function: Printer Handle --- .../fpsms/modules/master/entity/Printer.kt | 1 - .../master/entity/PrinterRepository.kt | 4 + .../modules/master/service/PrinterService.kt | 104 ++++++++++++++++++ .../modules/master/web/PrinterController.kt | 57 +++++++++- 4 files changed, 164 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ffii/fpsms/modules/master/entity/Printer.kt b/src/main/java/com/ffii/fpsms/modules/master/entity/Printer.kt index 5458240..19fc6b1 100644 --- a/src/main/java/com/ffii/fpsms/modules/master/entity/Printer.kt +++ b/src/main/java/com/ffii/fpsms/modules/master/entity/Printer.kt @@ -30,7 +30,6 @@ open class Printer : BaseEntity() { @Column(name = "ip", length = 30) open var ip: String? = null - @Size(max = 10) @Column(name = "port", length = 10) open var port: Int? = null diff --git a/src/main/java/com/ffii/fpsms/modules/master/entity/PrinterRepository.kt b/src/main/java/com/ffii/fpsms/modules/master/entity/PrinterRepository.kt index 609c73a..e436ad5 100644 --- a/src/main/java/com/ffii/fpsms/modules/master/entity/PrinterRepository.kt +++ b/src/main/java/com/ffii/fpsms/modules/master/entity/PrinterRepository.kt @@ -2,6 +2,7 @@ package com.ffii.fpsms.modules.master.entity import com.ffii.core.support.AbstractRepository import com.ffii.fpsms.modules.master.entity.projections.PrinterCombo +import org.springframework.data.jpa.repository.Query import org.springframework.stereotype.Repository import java.io.Serializable @@ -12,4 +13,7 @@ interface PrinterRepository : AbstractRepository { fun findByIdAndDeletedFalse(id: Serializable): Printer?; fun findAllByDeletedIsFalse(): List; + + @Query("SELECT DISTINCT p.description FROM Printer p WHERE p.deleted = false AND p.description IS NOT NULL AND p.description != '' ORDER BY p.description") + fun findDistinctDescriptionsByDeletedIsFalse(): List; } \ No newline at end of file diff --git a/src/main/java/com/ffii/fpsms/modules/master/service/PrinterService.kt b/src/main/java/com/ffii/fpsms/modules/master/service/PrinterService.kt index 0e45de3..20187cf 100644 --- a/src/main/java/com/ffii/fpsms/modules/master/service/PrinterService.kt +++ b/src/main/java/com/ffii/fpsms/modules/master/service/PrinterService.kt @@ -3,12 +3,20 @@ package com.ffii.fpsms.modules.master.service import com.ffii.fpsms.modules.master.entity.Printer import com.ffii.fpsms.modules.master.entity.PrinterRepository import com.ffii.fpsms.modules.master.entity.projections.PrinterCombo +import jakarta.persistence.OptimisticLockException +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.http.HttpStatus import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import org.springframework.web.server.ResponseStatusException @Service open class PrinterService( val printerRepository: PrinterRepository ) { + private val logger: Logger = LoggerFactory.getLogger(PrinterService::class.java) + open fun getPrinters(): List { return printerRepository.findAllByDeletedIsFalse(); } @@ -20,4 +28,100 @@ open class PrinterService( open fun findById(id: Long): Printer? { return printerRepository.findByIdAndDeletedFalse(id) } + + open fun getDistinctDescriptions(): List { + return printerRepository.findDistinctDescriptionsByDeletedIsFalse() + } + + @Transactional + open fun createPrinter(request: com.ffii.fpsms.modules.master.web.PrinterCreateRequest): Printer { + try { + val printer = Printer().apply { + name = request.name + code = request.code + type = request.type + description = request.description + ip = request.ip + port = request.port + dpi = request.dpi + } + + return printerRepository.save(printer) + } catch (e: OptimisticLockException) { + logger.error("Optimistic lock exception when creating printer", e) + throw ResponseStatusException( + HttpStatus.CONFLICT, + "Printer was modified by another user. Please refresh and try again." + ) + } catch (e: ResponseStatusException) { + throw e + } catch (e: Exception) { + logger.error("Error creating printer", e) + throw ResponseStatusException( + HttpStatus.INTERNAL_SERVER_ERROR, + "Failed to create printer: ${e.message ?: "Unknown error"}" + ) + } + } + + @Transactional + open fun updatePrinter(id: Long, request: com.ffii.fpsms.modules.master.web.PrinterUpdateRequest): Printer { + try { + val printer = printerRepository.findById(id).orElseThrow { + ResponseStatusException(HttpStatus.NOT_FOUND, "Printer with id $id not found") + } + + request.name?.let { printer.name = it } + request.code?.let { printer.code = it } + request.type?.let { printer.type = it } + request.description?.let { printer.description = it } + request.ip?.let { printer.ip = it } + request.port?.let { printer.port = it } + request.dpi?.let { printer.dpi = it } + + return printerRepository.save(printer) + } catch (e: OptimisticLockException) { + logger.error("Optimistic lock exception when updating printer with id $id", e) + throw ResponseStatusException( + HttpStatus.CONFLICT, + "Printer was modified by another user. Please refresh and try again." + ) + } catch (e: ResponseStatusException) { + throw e + } catch (e: Exception) { + logger.error("Error updating printer with id $id", e) + throw ResponseStatusException( + HttpStatus.INTERNAL_SERVER_ERROR, + "Failed to update printer: ${e.message ?: "Unknown error"}" + ) + } + } + + @Transactional + open fun markDeleted(id: Long): List { + try { + val printer = printerRepository.findById(id).orElseThrow { + ResponseStatusException(HttpStatus.NOT_FOUND, "Printer with id $id not found") + } + + printer.deleted = true + printerRepository.save(printer) + + return getPrinters() + } catch (e: OptimisticLockException) { + logger.error("Optimistic lock exception when deleting printer with id $id", e) + throw ResponseStatusException( + HttpStatus.CONFLICT, + "Printer was modified by another user. Please refresh and try again." + ) + } catch (e: ResponseStatusException) { + throw e + } catch (e: Exception) { + logger.error("Error deleting printer with id $id", e) + throw ResponseStatusException( + HttpStatus.INTERNAL_SERVER_ERROR, + "Failed to delete printer: ${e.message ?: "Unknown error"}" + ) + } + } } \ No newline at end of file diff --git a/src/main/java/com/ffii/fpsms/modules/master/web/PrinterController.kt b/src/main/java/com/ffii/fpsms/modules/master/web/PrinterController.kt index cc59d48..291dc0f 100644 --- a/src/main/java/com/ffii/fpsms/modules/master/web/PrinterController.kt +++ b/src/main/java/com/ffii/fpsms/modules/master/web/PrinterController.kt @@ -3,9 +3,15 @@ package com.ffii.fpsms.modules.master.web import com.ffii.fpsms.modules.master.entity.Printer import com.ffii.fpsms.modules.master.entity.projections.PrinterCombo import com.ffii.fpsms.modules.master.service.PrinterService +import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.PutMapping +import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController +import jakarta.validation.Valid @RequestMapping("printers") @RestController @@ -17,8 +23,57 @@ class PrinterController( return printerService.getPrinters(); } + @GetMapping("/{id}") + fun getPrinter(@PathVariable id: Long): Printer { + return printerService.findById(id) + ?: throw org.springframework.web.server.ResponseStatusException( + org.springframework.http.HttpStatus.NOT_FOUND, + "Printer with id $id not found" + ) + } + @GetMapping("/combo") fun findCombo(): List { return printerService.findCombo(); } -} \ No newline at end of file + + @GetMapping("/descriptions") + fun getDescriptions(): List { + return printerService.getDistinctDescriptions(); + } + + @PostMapping + fun createPrinter(@Valid @RequestBody request: PrinterCreateRequest): Printer { + return printerService.createPrinter(request) + } + + @PutMapping("/{id}") + fun updatePrinter(@PathVariable id: Long, @Valid @RequestBody request: PrinterUpdateRequest): Printer { + return printerService.updatePrinter(id, request); + } + + @DeleteMapping("/{id}") + fun deletePrinter(@PathVariable id: Long): List { + return printerService.markDeleted(id); + } +} + +data class PrinterCreateRequest( + val name: String? = null, + val code: String? = null, + val type: String? = null, + val description: String? = null, + val ip: String? = null, + val port: Int? = null, + val dpi: Int? = null +) + +data class PrinterUpdateRequest( + val name: String? = null, + val code: String? = null, + val type: String? = null, + val description: String? = null, + val ip: String? = null, + val port: Int? = null, + val dpi: Int? = null +) \ No newline at end of file