Compare commits
No commits in common. "b14438cc15788c1bc5fa25a7fb164c60ae4f7dc4" and "b27d3bd5c66bfb1b910d263a198c8fc1addfa3e1" have entirely different histories.
b14438cc15
...
b27d3bd5c6
|
@ -6,7 +6,6 @@ import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.cancel
|
import kotlinx.coroutines.cancel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
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.content.order.OrderRepository
|
||||||
import kr.co.vividnext.sodalive.point.MemberPoint
|
import kr.co.vividnext.sodalive.point.MemberPoint
|
||||||
import kr.co.vividnext.sodalive.point.MemberPointRepository
|
import kr.co.vividnext.sodalive.point.MemberPointRepository
|
||||||
|
@ -18,6 +17,7 @@ import org.springframework.stereotype.Service
|
||||||
import org.springframework.transaction.support.TransactionTemplate
|
import org.springframework.transaction.support.TransactionTemplate
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import javax.annotation.PreDestroy
|
import javax.annotation.PreDestroy
|
||||||
|
import javax.persistence.EntityManager
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class UserActionService(
|
class UserActionService(
|
||||||
|
@ -26,7 +26,9 @@ class UserActionService(
|
||||||
private val policyRepository: PointRewardPolicyRepository,
|
private val policyRepository: PointRewardPolicyRepository,
|
||||||
private val grantLogRepository: PointGrantLogRepository,
|
private val grantLogRepository: PointGrantLogRepository,
|
||||||
private val memberPointRepository: MemberPointRepository,
|
private val memberPointRepository: MemberPointRepository,
|
||||||
private val transactionTemplate: TransactionTemplate
|
private val transactionTemplate: TransactionTemplate,
|
||||||
|
|
||||||
|
private val entityManager: EntityManager
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val coroutineScope = CoroutineScope(
|
private val coroutineScope = CoroutineScope(
|
||||||
|
@ -43,13 +45,6 @@ class UserActionService(
|
||||||
contentCommentId: Long? = null
|
contentCommentId: Long? = null
|
||||||
) {
|
) {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
var actionCount = 0
|
|
||||||
var grantedCount = 0
|
|
||||||
|
|
||||||
val order: Order?
|
|
||||||
var orderId: Long? = null
|
|
||||||
var point = 0
|
|
||||||
|
|
||||||
val now = LocalDateTime.now()
|
val now = LocalDateTime.now()
|
||||||
val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now)
|
val policy = policyRepository.findByActionTypeAndIsActiveTrue(actionType, now)
|
||||||
if (policy != null) {
|
if (policy != null) {
|
||||||
|
@ -63,7 +58,7 @@ class UserActionService(
|
||||||
|
|
||||||
val isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate =
|
val isValidPolicyTypeDailyAndDailyStartDateAfterPolicyStartDate =
|
||||||
policyType == PolicyType.DAILY && policyTypeDailyStartDate >= policy.startDate
|
policyType == PolicyType.DAILY && policyTypeDailyStartDate >= policy.startDate
|
||||||
order = if (contentId != null) {
|
val order = if (contentId != null) {
|
||||||
orderRepository.findByMemberIdAndContentId(
|
orderRepository.findByMemberIdAndContentId(
|
||||||
memberId = memberId,
|
memberId = memberId,
|
||||||
contentId = contentId,
|
contentId = contentId,
|
||||||
|
@ -76,37 +71,7 @@ class UserActionService(
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
orderId = order?.id
|
|
||||||
if (actionType == ActionType.ORDER_CONTENT_COMMENT && order == null) return@launch
|
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) {
|
withContext(Dispatchers.IO) {
|
||||||
|
@ -126,10 +91,64 @@ class UserActionService(
|
||||||
if (isAuth) {
|
if (isAuth) {
|
||||||
try {
|
try {
|
||||||
transactionTemplate.execute {
|
transactionTemplate.execute {
|
||||||
|
entityManager.clear()
|
||||||
if (policy != null) {
|
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
|
if (grantedCount >= policy.availableCount) return@execute
|
||||||
|
|
||||||
|
val point = if (actionType == ActionType.ORDER_CONTENT_COMMENT && order != null) {
|
||||||
|
order.can
|
||||||
|
} else {
|
||||||
|
policy.pointAmount
|
||||||
|
}
|
||||||
|
|
||||||
if (point > 0) {
|
if (point > 0) {
|
||||||
grantLogRepository.save(
|
grantLogRepository.save(
|
||||||
PointGrantLog(
|
PointGrantLog(
|
||||||
|
@ -137,7 +156,7 @@ class UserActionService(
|
||||||
point = point,
|
point = point,
|
||||||
actionType = actionType,
|
actionType = actionType,
|
||||||
policyId = policy.id!!,
|
policyId = policy.id!!,
|
||||||
orderId = orderId
|
orderId = order?.id
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue