Compare commits

..

No commits in common. "b14438cc15788c1bc5fa25a7fb164c60ae4f7dc4" and "b27d3bd5c66bfb1b910d263a198c8fc1addfa3e1" have entirely different histories.

1 changed files with 61 additions and 42 deletions

View File

@ -6,7 +6,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kr.co.vividnext.sodalive.content.order.Order
import kr.co.vividnext.sodalive.content.order.OrderRepository
import kr.co.vividnext.sodalive.point.MemberPoint
import kr.co.vividnext.sodalive.point.MemberPointRepository
@ -18,6 +17,7 @@ import org.springframework.stereotype.Service
import org.springframework.transaction.support.TransactionTemplate
import java.time.LocalDateTime
import javax.annotation.PreDestroy
import javax.persistence.EntityManager
@Service
class UserActionService(
@ -26,7 +26,9 @@ class UserActionService(
private val policyRepository: PointRewardPolicyRepository,
private val grantLogRepository: PointGrantLogRepository,
private val memberPointRepository: MemberPointRepository,
private val transactionTemplate: TransactionTemplate
private val transactionTemplate: TransactionTemplate,
private val entityManager: EntityManager
) {
private val coroutineScope = CoroutineScope(
@ -43,13 +45,6 @@ class UserActionService(
contentCommentId: Long? = null
) {
coroutineScope.launch {
var actionCount = 0
var grantedCount = 0
val order: Order?
var orderId: Long? = null
var point = 0
val now = LocalDateTime.now()
val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now)
if (policy != null) {
@ -63,7 +58,7 @@ class UserActionService(
val isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate =
policyType == PolicyType.DAILY && policyTypeDailyStartDate >= policy.startDate
order = if (contentId != null) {
val order = if (contentId != null) {
orderRepository.findByMemberIdAndContentId(
memberId = memberId,
contentId = contentId,
@ -76,37 +71,7 @@ class UserActionService(
} else {
null
}
orderId = order?.id
if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@launch
point = if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) {
order.can
} else {
policy.pointAmount
}
actionCount = repository.countByMemberIdAndActionTypeAndCreatedAtBetween(
memberId = memberId,
actionType = actionType,
startDate = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) {
policyTypeDailyStartDate
} else {
policy.startDate
},
endDate = policy.endDate ?: LocalDateTime.now()
)
grantedCount = grantLogRepository.countByMemberIdAndPolicyIdAndStartDate(
memberId,
policy.id!!,
startDate = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) {
policyTypeDailyStartDate
} else {
policy.startDate
},
orderId = order?.id
)
}
withContext(Dispatchers.IO) {
@ -126,10 +91,64 @@ class UserActionService(
if (isAuth) {
try {
transactionTemplate.execute {
entityManager.clear()
if (policy != null) {
if (actionCount + 1 < policy.threshold) return@execute
val policyType = policy.policyType
val todayAt15 = now.toLocalDate().atTime(15, 0)
val policyTypeDailyStartDate =
if (now.toLocalTime().isBefore(todayAt15.toLocalTime())) {
now.toLocalDate().minusDays(1).atTime(15, 0)
} else {
todayAt15
}
val isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate =
policyType == PolicyType.DAILY && policyTypeDailyStartDate >= policy.startDate
val order = if (contentId != null) {
orderRepository.findByMemberIdAndContentId(
memberId = memberId,
contentId = contentId,
createdAt = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) {
policyTypeDailyStartDate
} else {
policy.startDate
}
)
} else {
null
}
if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@execute
val actionCount = repository.countByMemberIdAndActionTypeAndCreatedAtBetween(
memberId = memberId,
actionType = actionType,
startDate = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) {
policyTypeDailyStartDate
} else {
policy.startDate
},
endDate = policy.endDate ?: LocalDateTime.now()
)
if (actionCount < policy.threshold) return@execute
val grantedCount = grantLogRepository.countByMemberIdAndPolicyIdAndStartDate(
memberId,
policy.id!!,
startDate = if (isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate) {
policyTypeDailyStartDate
} else {
policy.startDate
},
orderId = order?.id
)
if (grantedCount >= policy.availableCount) return@execute
val point = if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) {
order.can
} else {
policy.pointAmount
}
if (point > 0) {
grantLogRepository.save(
PointGrantLog(
@ -137,7 +156,7 @@ class UserActionService(
point = point,
actionType = actionType,
policyId = policy.id!!,
orderId = orderId
orderId = order?.id
)
)