feat(gift): 카테고리별 신청번호를 발급한다
This commit is contained in:
+14
-5
@@ -3,6 +3,7 @@ package kr.co.vividnext.sodalive.v2.gift.application
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.v2.gift.adapter.out.persistence.GiftApplicationNoSequence
|
||||
import kr.co.vividnext.sodalive.v2.gift.adapter.out.persistence.GiftApplicationNoSequenceRepository
|
||||
import kr.co.vividnext.sodalive.v2.gift.adapter.out.persistence.GiftCategory
|
||||
import org.springframework.dao.DataIntegrityViolationException
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.PlatformTransactionManager
|
||||
@@ -18,15 +19,23 @@ class GiftApplicationNoGenerator(
|
||||
) {
|
||||
private val transactionTemplate = TransactionTemplate(transactionManager)
|
||||
|
||||
fun generate(now: LocalDateTime = LocalDateTime.now(SEOUL_ZONE)): String {
|
||||
fun generate(category: GiftCategory, now: LocalDateTime = LocalDateTime.now(SEOUL_ZONE)): String {
|
||||
val categoryId = category.id ?: throw SodaException(messageKey = "common.error.invalid_request")
|
||||
val sequenceDate = now.format(DATE_FORMATTER)
|
||||
for (attempt in 1..MAX_RETRY_COUNT) {
|
||||
try {
|
||||
return transactionTemplate.execute {
|
||||
val sequence = repository.findBySequenceDateForUpdate(sequenceDate)
|
||||
?: repository.saveAndFlush(GiftApplicationNoSequence(sequenceDate = sequenceDate, lastSequence = 0))
|
||||
val sequence = repository.findByCategoryIdAndSequenceDateForUpdate(categoryId, sequenceDate)
|
||||
?: repository.saveAndFlush(
|
||||
GiftApplicationNoSequence(
|
||||
categoryId = categoryId,
|
||||
sequenceDate = sequenceDate,
|
||||
lastSequence = 0
|
||||
)
|
||||
)
|
||||
sequence.lastSequence += 1
|
||||
"G$sequenceDate${sequence.lastSequence.toString().padStart(6, '0')}"
|
||||
"${category.receiptCode}-${category.classificationNumber}$sequenceDate" +
|
||||
sequence.lastSequence.toString().padStart(4, '0')
|
||||
} ?: throw SodaException(messageKey = "common.error.invalid_request")
|
||||
} catch (_: DataIntegrityViolationException) {
|
||||
if (attempt == MAX_RETRY_COUNT) throw SodaException(messageKey = "gift.application_no.generate_failed")
|
||||
@@ -37,7 +46,7 @@ class GiftApplicationNoGenerator(
|
||||
|
||||
companion object {
|
||||
private val SEOUL_ZONE = ZoneId.of("Asia/Seoul")
|
||||
private val DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd")
|
||||
private val DATE_FORMATTER = DateTimeFormatter.ofPattern("yyMMdd")
|
||||
private const val MAX_RETRY_COUNT = 3
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ class GiftCommandService(
|
||||
val sizePrice = sizePriceRepository.findBySizeCode(command.sizeCode)?.takeIf { it.isActive } ?: throw invalidRequest()
|
||||
val appliedAt = now()
|
||||
val trackingDeadlineAt = appliedAt.plusDays(3)
|
||||
val applicationNo = applicationNoGenerator.generate(appliedAt)
|
||||
val applicationNo = applicationNoGenerator.generate(category, appliedAt)
|
||||
val canUsageId = canPaymentService.spendGiftCan(
|
||||
senderMemberId,
|
||||
sizePrice.salePriceCan,
|
||||
|
||||
Reference in New Issue
Block a user