You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

73 regels
2.9 KiB

  1. package com.ffii.fpsms.py
  2. import com.ffii.fpsms.modules.jobOrder.entity.JobOrderRepository
  3. import com.ffii.fpsms.py.entity.PyJobOrderPrintSubmit
  4. import org.springframework.http.HttpStatus
  5. import org.springframework.stereotype.Service
  6. import org.springframework.transaction.annotation.Transactional
  7. import org.springframework.web.server.ResponseStatusException
  8. @Service
  9. open class PyJobOrderPrintSubmitService(
  10. private val repository: PyJobOrderPrintSubmitRepository,
  11. private val jobOrderRepository: JobOrderRepository,
  12. ) {
  13. /**
  14. * Cumulative printed qty per job order, split by channel (打袋 / 標籤 / 激光).
  15. */
  16. open fun sumPrintedQtyByJobOrderIds(ids: List<Long>): Map<Long, PrintedQtyByChannel> {
  17. if (ids.isEmpty()) return emptyMap()
  18. val rows = repository.sumQtyGroupedByJobOrderIdAndChannel(ids)
  19. val out = mutableMapOf<Long, PrintedQtyByChannel>()
  20. for (row in rows) {
  21. val jobOrderId = (row[0] as Number).toLong()
  22. val channel = (row[1] as String).trim()
  23. val qty = (row[2] as Number).toLong()
  24. val cur = out.getOrDefault(jobOrderId, PrintedQtyByChannel())
  25. out[jobOrderId] =
  26. when (channel) {
  27. PyPrintChannel.DATAFLEX -> cur.copy(bagPrintedQty = qty)
  28. PyPrintChannel.LABEL -> cur.copy(labelPrintedQty = qty)
  29. PyPrintChannel.LASER -> cur.copy(laserPrintedQty = qty)
  30. else -> cur
  31. }
  32. }
  33. return out
  34. }
  35. private fun sumPrintedByJobOrderIdsAndChannel(ids: List<Long>, channel: String): Map<Long, Long> {
  36. if (ids.isEmpty()) return emptyMap()
  37. val rows = repository.sumQtyGroupedByJobOrderId(ids, channel)
  38. return rows.associate { row ->
  39. (row[0] as Number).toLong() to (row[1] as Number).toLong()
  40. }
  41. }
  42. /**
  43. * Persist one submit row and return cumulative print total for that job order and channel.
  44. */
  45. @Transactional
  46. open fun recordPrint(jobOrderId: Long, qty: Int, printChannel: String): PyJobOrderPrintSubmitResponse {
  47. if (qty < 1) {
  48. throw ResponseStatusException(HttpStatus.BAD_REQUEST, "qty must be at least 1")
  49. }
  50. val jo = jobOrderRepository.findById(jobOrderId).orElseThrow {
  51. ResponseStatusException(HttpStatus.NOT_FOUND, "Job order not found: $jobOrderId")
  52. }
  53. val row = PyJobOrderPrintSubmit().apply {
  54. jobOrder = jo
  55. this.qty = qty
  56. this.printChannel = printChannel
  57. }
  58. repository.save(row)
  59. val total = sumPrintedByJobOrderIdsAndChannel(listOf(jobOrderId), printChannel)[jobOrderId] ?: qty.toLong()
  60. return PyJobOrderPrintSubmitResponse(
  61. jobOrderId = jobOrderId,
  62. submittedQty = qty,
  63. printChannel = printChannel,
  64. cumulativeQtyForChannel = total,
  65. )
  66. }
  67. }