|
|
|
@@ -224,6 +224,49 @@ open class TruckLaneScheduleService( |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
open fun reactivateCancelled( |
|
|
|
id: Long, |
|
|
|
executeAt: LocalDateTime?, |
|
|
|
): TruckLaneScheduleResponse { |
|
|
|
val s = scheduleRepository.findByIdAndDeletedFalse(id) |
|
|
|
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "排程不存在") |
|
|
|
if (s.status != TruckLaneScheduleStatus.CANCELLED) { |
|
|
|
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "僅 CANCELLED 可重新排程") |
|
|
|
} |
|
|
|
val lines = |
|
|
|
scheduleLineRepository.findAllBySchedule_IdAndDeletedFalseOrderByIdAsc(s.id ?: 0L) |
|
|
|
val reactivatable = |
|
|
|
lines.filter { it.lineStatus == TruckLaneScheduleLineStatus.SKIPPED } |
|
|
|
if (reactivatable.isEmpty()) { |
|
|
|
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "無可重新排程的明細") |
|
|
|
} |
|
|
|
val whenAt = executeAt ?: LocalDateTime.now().plusMinutes(1) |
|
|
|
validateExecuteAt(whenAt) |
|
|
|
val requestLines = reactivatable.mapNotNull { line -> lineToRequest(line) } |
|
|
|
if (requestLines.isEmpty()) { |
|
|
|
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "無可重新排程的明細") |
|
|
|
} |
|
|
|
val blocking = schedulePlanValidator.validatePlanLines(requestLines) |
|
|
|
if (blocking.isNotEmpty()) { |
|
|
|
throw ResponseStatusException( |
|
|
|
HttpStatus.BAD_REQUEST, |
|
|
|
blocking.joinToString("; ") { "${it.code}:${it.messageKey}" }, |
|
|
|
) |
|
|
|
} |
|
|
|
s.executeAt = whenAt |
|
|
|
s.status = TruckLaneScheduleStatus.PENDING |
|
|
|
s.appliedAt = null |
|
|
|
s.errorMessage = null |
|
|
|
for (line in reactivatable) { |
|
|
|
line.lineStatus = TruckLaneScheduleLineStatus.PENDING |
|
|
|
line.errorMessage = null |
|
|
|
line.appliedAt = null |
|
|
|
} |
|
|
|
scheduleLineRepository.saveAll(reactivatable) |
|
|
|
return toResponse(scheduleRepository.save(s), includeLines = true) |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
open fun ignore(id: Long): TruckLaneScheduleResponse { |
|
|
|
val s = scheduleRepository.findByIdAndDeletedFalse(id) |
|
|
|
|