fix(agent-calculate): 에이전트 정산 total projection 조회를 DB 합계 쿼리로 분리한다
This commit is contained in:
@@ -17,9 +17,13 @@ import kr.co.vividnext.sodalive.partner.agent.assignment.QAgentCreatorRelation.a
|
||||
import kr.co.vividnext.sodalive.partner.agent.ratio.QAgentSettlementRatio.agentSettlementRatio
|
||||
import org.springframework.stereotype.Repository
|
||||
import java.time.LocalDateTime
|
||||
import javax.persistence.EntityManager
|
||||
|
||||
@Repository
|
||||
class AgentCalculateQueryRepository(private val queryFactory: JPAQueryFactory) {
|
||||
class AgentCalculateQueryRepository(
|
||||
private val queryFactory: JPAQueryFactory,
|
||||
private val entityManager: EntityManager
|
||||
) {
|
||||
fun getAssignedCreatorTotalCount(agentId: Long, currentTime: LocalDateTime): Int {
|
||||
return queryFactory
|
||||
.select(agentCreatorRelation.id.count())
|
||||
@@ -84,6 +88,19 @@ class AgentCalculateQueryRepository(private val queryFactory: JPAQueryFactory) {
|
||||
return getCalculateLiveByCreatorRows(startDate, endDate, agentId, creatorIds = null)
|
||||
}
|
||||
|
||||
fun getCalculateLiveByCreatorTotal(
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
agentId: Long
|
||||
): GetAgentSettlementByCreatorTotal {
|
||||
return getGenericSettlementTotal(
|
||||
subQuery = buildLiveSettlementTotalSubQuery(),
|
||||
startDate = startDate,
|
||||
endDate = endDate,
|
||||
agentId = agentId
|
||||
)
|
||||
}
|
||||
|
||||
fun getCalculateLiveByCreator(
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
@@ -148,6 +165,19 @@ class AgentCalculateQueryRepository(private val queryFactory: JPAQueryFactory) {
|
||||
return getCalculateContentByCreatorRows(startDate, endDate, agentId, creatorIds = null)
|
||||
}
|
||||
|
||||
fun getCalculateContentByCreatorTotal(
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
agentId: Long
|
||||
): GetAgentSettlementByCreatorTotal {
|
||||
return getGenericSettlementTotal(
|
||||
subQuery = buildContentSettlementTotalSubQuery(),
|
||||
startDate = startDate,
|
||||
endDate = endDate,
|
||||
agentId = agentId
|
||||
)
|
||||
}
|
||||
|
||||
fun getCalculateContentByCreator(
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
@@ -213,6 +243,19 @@ class AgentCalculateQueryRepository(private val queryFactory: JPAQueryFactory) {
|
||||
return getCalculateCommunityByCreatorRows(startDate, endDate, agentId, creatorIds = null)
|
||||
}
|
||||
|
||||
fun getCalculateCommunityByCreatorTotal(
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
agentId: Long
|
||||
): GetAgentSettlementByCreatorTotal {
|
||||
return getGenericSettlementTotal(
|
||||
subQuery = buildCommunitySettlementTotalSubQuery(),
|
||||
startDate = startDate,
|
||||
endDate = endDate,
|
||||
agentId = agentId
|
||||
)
|
||||
}
|
||||
|
||||
fun getCalculateCommunityByCreator(
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
@@ -283,6 +326,19 @@ class AgentCalculateQueryRepository(private val queryFactory: JPAQueryFactory) {
|
||||
return getCalculateContentDonationByCreatorRows(startDate, endDate, agentId, creatorIds = null)
|
||||
}
|
||||
|
||||
fun getCalculateContentDonationByCreatorTotal(
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
agentId: Long
|
||||
): GetAgentSettlementByCreatorTotal {
|
||||
return getGenericSettlementTotal(
|
||||
subQuery = buildContentDonationSettlementTotalSubQuery(),
|
||||
startDate = startDate,
|
||||
endDate = endDate,
|
||||
agentId = agentId
|
||||
)
|
||||
}
|
||||
|
||||
fun getCalculateContentDonationByCreator(
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
@@ -351,6 +407,65 @@ class AgentCalculateQueryRepository(private val queryFactory: JPAQueryFactory) {
|
||||
return getChannelDonationByCreatorRows(startDate, endDate, agentId, creatorIds = null)
|
||||
}
|
||||
|
||||
fun getChannelDonationByCreatorTotal(
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
agentId: Long
|
||||
): GetAgentChannelDonationSettlementTotal {
|
||||
val query = entityManager.createNativeQuery(
|
||||
"""
|
||||
SELECT
|
||||
COALESCE(SUM(grouped.count_value), 0) AS count,
|
||||
COALESCE(SUM(grouped.total_can), 0) AS totalCan,
|
||||
COALESCE(SUM(grouped.total_can * 100), 0) AS krw,
|
||||
COALESCE(SUM(ROUND(grouped.total_can * 100 * 0.066, 0)), 0) AS fee,
|
||||
COALESCE(SUM(ROUND(((grouped.total_can * 100) - ROUND(grouped.total_can * 100 * 0.066, 0)) * 0.85, 0)), 0) AS settlementAmount,
|
||||
COALESCE(SUM(ROUND(ROUND(((grouped.total_can * 100) - ROUND(grouped.total_can * 100 * 0.066, 0)) * 0.85, 0) * 0.033, 0)), 0) AS withholdingTax,
|
||||
COALESCE(SUM(ROUND(((grouped.total_can * 100) - ROUND(grouped.total_can * 100 * 0.066, 0)) * 0.85, 0) - ROUND(ROUND(((grouped.total_can * 100) - ROUND(grouped.total_can * 100 * 0.066, 0)) * 0.85, 0) * 0.033, 0)), 0) AS depositAmount,
|
||||
COALESCE(SUM(ROUND(ROUND(((grouped.total_can * 100) - ROUND(grouped.total_can * 100 * 0.066, 0)) * 0.85, 0) * (grouped.agent_settlement_ratio / 100.0), 0)), 0) AS agentSettlementAmount
|
||||
FROM (
|
||||
SELECT
|
||||
COUNT(DISTINCT uc.id) AS count_value,
|
||||
SUM(ucc.can) AS total_can,
|
||||
COALESCE(asr.settlement_ratio, 10) AS agent_settlement_ratio
|
||||
FROM use_can_calculate ucc
|
||||
INNER JOIN use_can uc ON ucc.use_can_id = uc.id
|
||||
INNER JOIN member m ON m.id = ucc.recipient_creator_id
|
||||
INNER JOIN agent_creator_relation acr ON acr.creator_id = m.id
|
||||
AND acr.agent_id = :agentId
|
||||
AND acr.assigned_at <= uc.created_at
|
||||
AND (acr.unassigned_at IS NULL OR acr.unassigned_at > uc.created_at)
|
||||
LEFT JOIN agent_settlement_ratio asr ON asr.member_id = :agentId
|
||||
AND asr.effective_from <= uc.created_at
|
||||
AND (asr.effective_to IS NULL OR asr.effective_to > uc.created_at)
|
||||
WHERE uc.can_usage = 'CHANNEL_DONATION'
|
||||
AND uc.is_refund = FALSE
|
||||
AND ucc.status = 'RECEIVED'
|
||||
AND uc.created_at >= :startDate
|
||||
AND uc.created_at <= :endDate
|
||||
GROUP BY acr.id, asr.id, COALESCE(asr.settlement_ratio, 10)
|
||||
) grouped
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
query.setParameter("startDate", startDate)
|
||||
query.setParameter("endDate", endDate)
|
||||
query.setParameter("agentId", agentId)
|
||||
|
||||
val result = query.singleResult as Array<*>
|
||||
|
||||
return GetAgentChannelDonationSettlementTotal(
|
||||
count = result[0].toIntValue(),
|
||||
totalCan = result[1].toIntValue(),
|
||||
krw = result[2].toIntValue(),
|
||||
fee = result[3].toIntValue(),
|
||||
settlementAmount = result[4].toIntValue(),
|
||||
withholdingTax = result[5].toIntValue(),
|
||||
depositAmount = result[6].toIntValue(),
|
||||
agentSettlementAmount = result[7].toIntValue()
|
||||
)
|
||||
}
|
||||
|
||||
fun getChannelDonationByCreator(
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
@@ -643,6 +758,156 @@ class AgentCalculateQueryRepository(private val queryFactory: JPAQueryFactory) {
|
||||
.fetch()
|
||||
}
|
||||
|
||||
private fun getGenericSettlementTotal(
|
||||
subQuery: String,
|
||||
startDate: LocalDateTime,
|
||||
endDate: LocalDateTime,
|
||||
agentId: Long
|
||||
): GetAgentSettlementByCreatorTotal {
|
||||
val query = entityManager.createNativeQuery(
|
||||
"""
|
||||
SELECT
|
||||
COALESCE(SUM(grouped.count_value), 0) AS count,
|
||||
COALESCE(SUM(grouped.total_can), 0) AS totalCan,
|
||||
COALESCE(SUM(grouped.total_can * 100), 0) AS krw,
|
||||
COALESCE(SUM(ROUND(grouped.total_can * 100 * 0.066, 0)), 0) AS fee,
|
||||
COALESCE(SUM(ROUND(((grouped.total_can * 100) - ((grouped.total_can * 100) * 0.066)) * (grouped.settlement_ratio / 100.0), 0)), 0) AS settlementAmount,
|
||||
COALESCE(SUM(ROUND((((grouped.total_can * 100) - ((grouped.total_can * 100) * 0.066)) * (grouped.settlement_ratio / 100.0)) * 0.033, 0)), 0) AS tax,
|
||||
COALESCE(SUM(ROUND((((grouped.total_can * 100) - ((grouped.total_can * 100) * 0.066)) * (grouped.settlement_ratio / 100.0)) - ((((grouped.total_can * 100) - ((grouped.total_can * 100) * 0.066)) * (grouped.settlement_ratio / 100.0)) * 0.033), 0)), 0) AS depositAmount,
|
||||
COALESCE(SUM(ROUND(ROUND(((grouped.total_can * 100) - ((grouped.total_can * 100) * 0.066)) * (grouped.settlement_ratio / 100.0), 0) * (grouped.agent_settlement_ratio / 100.0), 0)), 0) AS agentSettlementAmount
|
||||
FROM (
|
||||
$subQuery
|
||||
) grouped
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
query.setParameter("startDate", startDate)
|
||||
query.setParameter("endDate", endDate)
|
||||
query.setParameter("agentId", agentId)
|
||||
|
||||
val result = query.singleResult as Array<*>
|
||||
|
||||
return GetAgentSettlementByCreatorTotal(
|
||||
count = result[0].toIntValue(),
|
||||
totalCan = result[1].toIntValue(),
|
||||
krw = result[2].toIntValue(),
|
||||
fee = result[3].toIntValue(),
|
||||
settlementAmount = result[4].toIntValue(),
|
||||
tax = result[5].toIntValue(),
|
||||
depositAmount = result[6].toIntValue(),
|
||||
agentSettlementAmount = result[7].toIntValue()
|
||||
)
|
||||
}
|
||||
|
||||
private fun buildLiveSettlementTotalSubQuery() = """
|
||||
SELECT
|
||||
COUNT(uc.id) AS count_value,
|
||||
SUM(uc.can + uc.reward_can) AS total_can,
|
||||
COALESCE(csr.live_settlement_ratio, 70) AS settlement_ratio,
|
||||
COALESCE(asr.settlement_ratio, 10) AS agent_settlement_ratio
|
||||
FROM use_can uc
|
||||
INNER JOIN live_room lr ON uc.room_id = lr.id
|
||||
INNER JOIN member m ON lr.member_id = m.id
|
||||
INNER JOIN agent_creator_relation acr ON acr.creator_id = m.id
|
||||
AND acr.agent_id = :agentId
|
||||
AND acr.assigned_at <= uc.created_at
|
||||
AND (acr.unassigned_at IS NULL OR acr.unassigned_at > uc.created_at)
|
||||
LEFT JOIN creator_settlement_ratio csr ON csr.member_id = m.id
|
||||
AND csr.deleted_at IS NULL
|
||||
LEFT JOIN agent_settlement_ratio asr ON asr.member_id = :agentId
|
||||
AND asr.effective_from <= uc.created_at
|
||||
AND (asr.effective_to IS NULL OR asr.effective_to > uc.created_at)
|
||||
WHERE uc.is_refund = FALSE
|
||||
AND uc.created_at >= :startDate
|
||||
AND uc.created_at <= :endDate
|
||||
GROUP BY acr.id, asr.id, COALESCE(csr.live_settlement_ratio, 70), COALESCE(asr.settlement_ratio, 10)
|
||||
""".trimIndent()
|
||||
|
||||
private fun buildContentSettlementTotalSubQuery() = """
|
||||
SELECT
|
||||
COUNT(o.id) AS count_value,
|
||||
SUM(o.can) AS total_can,
|
||||
COALESCE(COALESCE(c.settlement_ratio, csr.content_settlement_ratio), 70) AS settlement_ratio,
|
||||
COALESCE(asr.settlement_ratio, 10) AS agent_settlement_ratio
|
||||
FROM orders o
|
||||
INNER JOIN content c ON o.content_id = c.id
|
||||
INNER JOIN member m ON c.member_id = m.id
|
||||
INNER JOIN agent_creator_relation acr ON acr.creator_id = m.id
|
||||
AND acr.agent_id = :agentId
|
||||
AND acr.assigned_at <= o.created_at
|
||||
AND (acr.unassigned_at IS NULL OR acr.unassigned_at > o.created_at)
|
||||
LEFT JOIN creator_settlement_ratio csr ON csr.member_id = m.id
|
||||
AND csr.deleted_at IS NULL
|
||||
LEFT JOIN agent_settlement_ratio asr ON asr.member_id = :agentId
|
||||
AND asr.effective_from <= o.created_at
|
||||
AND (asr.effective_to IS NULL OR asr.effective_to > o.created_at)
|
||||
WHERE o.created_at >= :startDate
|
||||
AND o.created_at <= :endDate
|
||||
AND o.is_active = TRUE
|
||||
GROUP BY
|
||||
acr.id,
|
||||
asr.id,
|
||||
COALESCE(c.settlement_ratio, csr.content_settlement_ratio),
|
||||
COALESCE(COALESCE(c.settlement_ratio, csr.content_settlement_ratio), 70),
|
||||
COALESCE(asr.settlement_ratio, 10)
|
||||
""".trimIndent()
|
||||
|
||||
private fun buildCommunitySettlementTotalSubQuery() = """
|
||||
SELECT
|
||||
COUNT(uc.id) AS count_value,
|
||||
SUM(uc.can + uc.reward_can) AS total_can,
|
||||
COALESCE(csr.community_settlement_ratio, 70) AS settlement_ratio,
|
||||
COALESCE(asr.settlement_ratio, 10) AS agent_settlement_ratio
|
||||
FROM use_can uc
|
||||
INNER JOIN creator_community cc ON uc.creator_community_id = cc.id
|
||||
INNER JOIN member m ON cc.member_id = m.id
|
||||
INNER JOIN agent_creator_relation acr ON acr.creator_id = m.id
|
||||
AND acr.agent_id = :agentId
|
||||
AND acr.assigned_at <= uc.created_at
|
||||
AND (acr.unassigned_at IS NULL OR acr.unassigned_at > uc.created_at)
|
||||
LEFT JOIN creator_settlement_ratio csr ON csr.member_id = m.id
|
||||
AND csr.deleted_at IS NULL
|
||||
LEFT JOIN agent_settlement_ratio asr ON asr.member_id = :agentId
|
||||
AND asr.effective_from <= uc.created_at
|
||||
AND (asr.effective_to IS NULL OR asr.effective_to > uc.created_at)
|
||||
WHERE uc.is_refund = FALSE
|
||||
AND uc.can_usage = 'PAID_COMMUNITY_POST'
|
||||
AND uc.created_at >= :startDate
|
||||
AND uc.created_at <= :endDate
|
||||
GROUP BY acr.id, asr.id, COALESCE(csr.community_settlement_ratio, 70), COALESCE(asr.settlement_ratio, 10)
|
||||
""".trimIndent()
|
||||
|
||||
private fun buildContentDonationSettlementTotalSubQuery() = """
|
||||
SELECT
|
||||
COUNT(uc.id) AS count_value,
|
||||
SUM(uc.can + uc.reward_can) AS total_can,
|
||||
70 AS settlement_ratio,
|
||||
COALESCE(asr.settlement_ratio, 10) AS agent_settlement_ratio
|
||||
FROM use_can uc
|
||||
INNER JOIN content c ON uc.content_id = c.id
|
||||
INNER JOIN member m ON c.member_id = m.id
|
||||
INNER JOIN agent_creator_relation acr ON acr.creator_id = m.id
|
||||
AND acr.agent_id = :agentId
|
||||
AND acr.assigned_at <= uc.created_at
|
||||
AND (acr.unassigned_at IS NULL OR acr.unassigned_at > uc.created_at)
|
||||
LEFT JOIN agent_settlement_ratio asr ON asr.member_id = :agentId
|
||||
AND asr.effective_from <= uc.created_at
|
||||
AND (asr.effective_to IS NULL OR asr.effective_to > uc.created_at)
|
||||
WHERE uc.is_refund = FALSE
|
||||
AND uc.can_usage = 'DONATION'
|
||||
AND uc.created_at >= :startDate
|
||||
AND uc.created_at <= :endDate
|
||||
GROUP BY acr.id, asr.id, COALESCE(asr.settlement_ratio, 10)
|
||||
""".trimIndent()
|
||||
|
||||
private fun Any?.toIntValue(): Int {
|
||||
return when (this) {
|
||||
null -> 0
|
||||
is Number -> toInt()
|
||||
else -> this.toString().toInt()
|
||||
}
|
||||
}
|
||||
|
||||
private fun assignedToAgentAtEventTime(
|
||||
agentId: Long,
|
||||
eventTime: DateTimeExpression<LocalDateTime>
|
||||
|
||||
@@ -40,8 +40,8 @@ class AgentCalculateService(
|
||||
totalCountLoader = { startDate, endDate ->
|
||||
repository.getCalculateLiveByCreatorTotalCount(startDate, endDate, agentId)
|
||||
},
|
||||
totalRowsLoader = { startDate, endDate ->
|
||||
repository.getCalculateLiveByCreator(startDate, endDate, agentId)
|
||||
totalLoader = { startDate, endDate ->
|
||||
repository.getCalculateLiveByCreatorTotal(startDate, endDate, agentId)
|
||||
},
|
||||
pagedRowsLoader = { startDate, endDate ->
|
||||
repository.getCalculateLiveByCreator(startDate, endDate, agentId, offset, limit)
|
||||
@@ -67,8 +67,8 @@ class AgentCalculateService(
|
||||
totalCountLoader = { startDate, endDate ->
|
||||
repository.getCalculateContentByCreatorTotalCount(startDate, endDate, agentId)
|
||||
},
|
||||
totalRowsLoader = { startDate, endDate ->
|
||||
repository.getCalculateContentByCreator(startDate, endDate, agentId)
|
||||
totalLoader = { startDate, endDate ->
|
||||
repository.getCalculateContentByCreatorTotal(startDate, endDate, agentId)
|
||||
},
|
||||
pagedRowsLoader = { startDate, endDate ->
|
||||
repository.getCalculateContentByCreator(startDate, endDate, agentId, offset, limit)
|
||||
@@ -94,8 +94,8 @@ class AgentCalculateService(
|
||||
totalCountLoader = { startDate, endDate ->
|
||||
repository.getCalculateCommunityByCreatorTotalCount(startDate, endDate, agentId)
|
||||
},
|
||||
totalRowsLoader = { startDate, endDate ->
|
||||
repository.getCalculateCommunityByCreator(startDate, endDate, agentId)
|
||||
totalLoader = { startDate, endDate ->
|
||||
repository.getCalculateCommunityByCreatorTotal(startDate, endDate, agentId)
|
||||
},
|
||||
pagedRowsLoader = { startDate, endDate ->
|
||||
repository.getCalculateCommunityByCreator(startDate, endDate, agentId, offset, limit)
|
||||
@@ -121,8 +121,8 @@ class AgentCalculateService(
|
||||
totalCountLoader = { startDate, endDate ->
|
||||
repository.getCalculateContentDonationByCreatorTotalCount(startDate, endDate, agentId)
|
||||
},
|
||||
totalRowsLoader = { startDate, endDate ->
|
||||
repository.getCalculateContentDonationByCreator(startDate, endDate, agentId)
|
||||
totalLoader = { startDate, endDate ->
|
||||
repository.getCalculateContentDonationByCreatorTotal(startDate, endDate, agentId)
|
||||
},
|
||||
pagedRowsLoader = { startDate, endDate ->
|
||||
repository.getCalculateContentDonationByCreator(startDate, endDate, agentId, offset, limit)
|
||||
@@ -156,9 +156,7 @@ class AgentCalculateService(
|
||||
}
|
||||
|
||||
val totalCount = repository.getChannelDonationByCreatorTotalCount(startDate, endDate, agentId)
|
||||
val total = repository.getChannelDonationByCreator(startDate, endDate, agentId)
|
||||
.toMergedResponseItems()
|
||||
.toResponseTotal()
|
||||
val total = repository.getChannelDonationByCreatorTotal(startDate, endDate, agentId)
|
||||
val items = repository.getChannelDonationByCreator(startDate, endDate, agentId, offset, limit)
|
||||
.toMergedResponseItems()
|
||||
|
||||
@@ -177,7 +175,7 @@ class AgentCalculateService(
|
||||
offset: Long,
|
||||
limit: Long,
|
||||
totalCountLoader: (LocalDateTime, LocalDateTime) -> Int,
|
||||
totalRowsLoader: (LocalDateTime, LocalDateTime) -> List<GetAgentCreatorSettlementSummaryQueryData>,
|
||||
totalLoader: (LocalDateTime, LocalDateTime) -> GetAgentSettlementByCreatorTotal,
|
||||
pagedRowsLoader: (LocalDateTime, LocalDateTime) -> List<GetAgentCreatorSettlementSummaryQueryData>
|
||||
): GetAgentSettlementByCreatorResponse {
|
||||
val (startDate, endDate) = toDateRange(startDateStr, endDateStr)
|
||||
@@ -198,8 +196,7 @@ class AgentCalculateService(
|
||||
}
|
||||
|
||||
val totalCount = totalCountLoader(startDate, endDate)
|
||||
val total = totalRowsLoader(startDate, endDate)
|
||||
.toResponseTotal()
|
||||
val total = totalLoader(startDate, endDate)
|
||||
val items = pagedRowsLoader(startDate, endDate)
|
||||
.toMergedResponseItems()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user