feat(creator-calculate): 본인 월별 정산 요약 API를 추가한다
This commit is contained in:
+35
@@ -0,0 +1,35 @@
|
||||
package kr.co.vividnext.sodalive.v2.api.creator.settlement.adapter.`in`.web
|
||||
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.v2.api.creator.settlement.application.CreatorMonthlySettlementService
|
||||
import kr.co.vividnext.sodalive.v2.api.creator.settlement.dto.GetCreatorMonthlySettlementResponse
|
||||
import org.springframework.http.CacheControl
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import java.time.YearMonth
|
||||
|
||||
@RestController
|
||||
@PreAuthorize("hasRole('CREATOR')")
|
||||
@RequestMapping("/creator-admin/calculate")
|
||||
class CreatorMonthlySettlementController(private val service: CreatorMonthlySettlementService) {
|
||||
@GetMapping("/monthly-summary")
|
||||
fun getMonthlySummary(
|
||||
@RequestParam month: String,
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
|
||||
): ResponseEntity<ApiResponse<GetCreatorMonthlySettlementResponse>> {
|
||||
val memberId = member?.id ?: throw SodaException(messageKey = "common.error.bad_credentials")
|
||||
if (!month.matches(Regex("[0-9]{4}-(0[1-9]|1[0-2])"))) {
|
||||
throw SodaException(messageKey = "common.error.invalid_request", errorProperty = "month")
|
||||
}
|
||||
val requestedMonth = YearMonth.parse(month)
|
||||
val summary = service.getMonthlySummary(memberId, requestedMonth)
|
||||
return ResponseEntity.ok().cacheControl(CacheControl.noStore()).body(ApiResponse.ok(summary))
|
||||
}
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
package kr.co.vividnext.sodalive.v2.api.creator.settlement.application
|
||||
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.creator.admin.calculate.CreatorAdminCalculateQueryRepository
|
||||
import kr.co.vividnext.sodalive.creator.admin.calculate.channelDonation.CreatorAdminChannelDonationCalculateQueryRepository
|
||||
import kr.co.vividnext.sodalive.member.MemberRepository
|
||||
import kr.co.vividnext.sodalive.member.MemberRole
|
||||
import kr.co.vividnext.sodalive.v2.api.creator.settlement.dto.CreatorMonthlySettlementAmount
|
||||
import kr.co.vividnext.sodalive.v2.api.creator.settlement.dto.CreatorMonthlySettlementSource
|
||||
import kr.co.vividnext.sodalive.v2.api.creator.settlement.dto.CreatorMonthlySettlementSourceAmount
|
||||
import kr.co.vividnext.sodalive.v2.api.creator.settlement.dto.GetCreatorMonthlySettlementResponse
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Isolation
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import java.time.Instant
|
||||
import java.time.LocalDateTime
|
||||
import java.time.YearMonth
|
||||
import java.time.ZoneId
|
||||
import java.time.ZoneOffset
|
||||
|
||||
@Service
|
||||
class CreatorMonthlySettlementService(
|
||||
private val repository: CreatorAdminCalculateQueryRepository,
|
||||
private val channelRepository: CreatorAdminChannelDonationCalculateQueryRepository,
|
||||
private val memberRepository: MemberRepository
|
||||
) {
|
||||
@Transactional(readOnly = true, isolation = Isolation.REPEATABLE_READ)
|
||||
fun getMonthlySummary(memberId: Long, month: YearMonth): GetCreatorMonthlySettlementResponse {
|
||||
val creator = memberRepository.findByIdOrNull(memberId)
|
||||
?: throw SodaException(messageKey = "common.error.bad_credentials")
|
||||
if (creator.role != MemberRole.CREATOR) throw SodaException(messageKey = "common.error.access_denied")
|
||||
|
||||
val zone = ZoneId.of("Asia/Seoul")
|
||||
val start = month.atDay(1).atStartOfDay(zone).toInstant()
|
||||
val end = month.plusMonths(1).atDay(1).atStartOfDay(zone).toInstant()
|
||||
val startUtc = LocalDateTime.ofInstant(start, ZoneOffset.UTC)
|
||||
val endUtc = LocalDateTime.ofInstant(end, ZoneOffset.UTC)
|
||||
val live = repository.getMonthlyCalculateLive(startUtc, endUtc, memberId)
|
||||
.map { it.toGetCalculateLiveResponse() }
|
||||
val content = repository.getMonthlyCalculateContentList(startUtc, endUtc, memberId)
|
||||
.map { it.toGetCalculateContentResponse() }
|
||||
val contentDonation = repository.getMonthlyCalculateContentDonationList(startUtc, endUtc, memberId)
|
||||
.map { it.toGetCalculateContentDonationResponse() }
|
||||
val community = repository.getMonthlyCalculateCommunityPostList(startUtc, endUtc, memberId)
|
||||
.map { it.toGetCalculateCommunityPostResponse() }
|
||||
val channelQuery = channelRepository.getMonthlyChannelDonationSettlementTotal(startUtc, endUtc, memberId)
|
||||
val channel = channelQuery.toResponseTotal()
|
||||
|
||||
val breakdown = listOf(
|
||||
CreatorMonthlySettlementSourceAmount(
|
||||
source = CreatorMonthlySettlementSource.LIVE,
|
||||
hasRevenueData = live.isNotEmpty(),
|
||||
amount = CreatorMonthlySettlementAmount(
|
||||
totalCan = live.sumOf { it.totalAmount.toLong() },
|
||||
grossAmount = live.sumOf { it.totalKrw.toLong() },
|
||||
paymentFee = live.sumOf { it.paymentFee.toLong() },
|
||||
settlementAmount = live.sumOf { it.settlementAmount.toLong() },
|
||||
withholdingTax = live.sumOf { it.tax.toLong() },
|
||||
depositAmount = live.sumOf { it.depositAmount.toLong() }
|
||||
)
|
||||
),
|
||||
CreatorMonthlySettlementSourceAmount(
|
||||
source = CreatorMonthlySettlementSource.CONTENT_SALES,
|
||||
hasRevenueData = content.isNotEmpty(),
|
||||
amount = CreatorMonthlySettlementAmount(
|
||||
totalCan = content.sumOf { it.totalCan.toLong() },
|
||||
grossAmount = content.sumOf { it.totalKrw.toLong() },
|
||||
paymentFee = content.sumOf { it.paymentFee.toLong() },
|
||||
settlementAmount = content.sumOf { it.settlementAmount.toLong() },
|
||||
withholdingTax = content.sumOf { it.tax.toLong() },
|
||||
depositAmount = content.sumOf { it.depositAmount.toLong() }
|
||||
)
|
||||
),
|
||||
CreatorMonthlySettlementSourceAmount(
|
||||
source = CreatorMonthlySettlementSource.CONTENT_DONATION,
|
||||
hasRevenueData = contentDonation.isNotEmpty(),
|
||||
amount = CreatorMonthlySettlementAmount(
|
||||
totalCan = contentDonation.sumOf { it.totalCan.toLong() },
|
||||
grossAmount = contentDonation.sumOf { it.totalKrw.toLong() },
|
||||
paymentFee = contentDonation.sumOf { it.paymentFee.toLong() },
|
||||
settlementAmount = contentDonation.sumOf { it.settlementAmount.toLong() },
|
||||
withholdingTax = contentDonation.sumOf { it.tax.toLong() },
|
||||
depositAmount = contentDonation.sumOf { it.depositAmount.toLong() }
|
||||
)
|
||||
),
|
||||
CreatorMonthlySettlementSourceAmount(
|
||||
source = CreatorMonthlySettlementSource.COMMUNITY_POST,
|
||||
hasRevenueData = community.isNotEmpty(),
|
||||
amount = CreatorMonthlySettlementAmount(
|
||||
totalCan = community.sumOf { it.totalCan.toLong() },
|
||||
grossAmount = community.sumOf { it.totalKrw.toLong() },
|
||||
paymentFee = community.sumOf { it.paymentFee.toLong() },
|
||||
settlementAmount = community.sumOf { it.settlementAmount.toLong() },
|
||||
withholdingTax = community.sumOf { it.tax.toLong() },
|
||||
depositAmount = community.sumOf { it.depositAmount.toLong() }
|
||||
)
|
||||
),
|
||||
CreatorMonthlySettlementSourceAmount(
|
||||
source = CreatorMonthlySettlementSource.CHANNEL_DONATION,
|
||||
hasRevenueData = (channelQuery.count ?: 0L) > 0L,
|
||||
amount = CreatorMonthlySettlementAmount(
|
||||
totalCan = channel.totalCan.toLong(),
|
||||
grossAmount = channel.krw.toLong(),
|
||||
paymentFee = channel.fee.toLong(),
|
||||
settlementAmount = channel.settlementAmount.toLong(),
|
||||
withholdingTax = channel.withholdingTax.toLong(),
|
||||
depositAmount = channel.depositAmount.toLong()
|
||||
)
|
||||
)
|
||||
)
|
||||
val total = CreatorMonthlySettlementAmount(
|
||||
totalCan = breakdown.sumOf { it.amount.totalCan },
|
||||
grossAmount = breakdown.sumOf { it.amount.grossAmount },
|
||||
paymentFee = breakdown.sumOf { it.amount.paymentFee },
|
||||
settlementAmount = breakdown.sumOf { it.amount.settlementAmount },
|
||||
withholdingTax = breakdown.sumOf { it.amount.withholdingTax },
|
||||
depositAmount = breakdown.sumOf { it.amount.depositAmount }
|
||||
)
|
||||
return GetCreatorMonthlySettlementResponse(
|
||||
month = month.toString(),
|
||||
periodStartInclusive = start.toString(),
|
||||
periodEndExclusive = end.toString(),
|
||||
calculatedAt = Instant.now().toString(),
|
||||
total = total,
|
||||
breakdown = breakdown
|
||||
)
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package kr.co.vividnext.sodalive.v2.api.creator.settlement.dto
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonUnwrapped
|
||||
|
||||
data class GetCreatorMonthlySettlementResponse(
|
||||
val month: String,
|
||||
val periodStartInclusive: String,
|
||||
val periodEndExclusive: String,
|
||||
val calculatedAt: String,
|
||||
val total: CreatorMonthlySettlementAmount,
|
||||
val breakdown: List<CreatorMonthlySettlementSourceAmount>
|
||||
) {
|
||||
val timeZone: String = "Asia/Seoul"
|
||||
val currency: String = "KRW"
|
||||
val calculationStatus: String = "PROVISIONAL"
|
||||
val hasRevenueData: Boolean = breakdown.any { it.hasRevenueData }
|
||||
val payoutStatus: String = "UNKNOWN"
|
||||
}
|
||||
|
||||
data class CreatorMonthlySettlementAmount(
|
||||
val totalCan: Long,
|
||||
val grossAmount: Long,
|
||||
val paymentFee: Long,
|
||||
val settlementAmount: Long,
|
||||
val withholdingTax: Long,
|
||||
val depositAmount: Long
|
||||
) {
|
||||
val platformFee: Long
|
||||
get() = grossAmount - paymentFee - settlementAmount
|
||||
}
|
||||
|
||||
data class CreatorMonthlySettlementSourceAmount(
|
||||
val source: CreatorMonthlySettlementSource,
|
||||
val hasRevenueData: Boolean,
|
||||
@get:JsonUnwrapped val amount: CreatorMonthlySettlementAmount
|
||||
)
|
||||
|
||||
enum class CreatorMonthlySettlementSource {
|
||||
LIVE, CONTENT_SALES, CONTENT_DONATION, COMMUNITY_POST, CHANNEL_DONATION
|
||||
}
|
||||
Reference in New Issue
Block a user