Files
sodalive-backend-spring-boot/src/main/kotlin/kr/co/vividnext/sodalive/can/CanService.kt

196 lines
6.9 KiB
Kotlin

package kr.co.vividnext.sodalive.can
import kr.co.vividnext.sodalive.can.charge.ChargeStatus
import kr.co.vividnext.sodalive.can.use.CanUsage
import kr.co.vividnext.sodalive.common.CountryContext
import kr.co.vividnext.sodalive.member.Member
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import java.time.ZoneId
import java.time.format.DateTimeFormatter
@Service
class CanService(
private val repository: CanRepository,
private val countryContext: CountryContext
) {
fun getCans(isNotSelectedCurrency: Boolean): List<CanResponse> {
val currency = if (isNotSelectedCurrency) {
null
} else {
when (countryContext.countryCode) {
"KR" -> "KRW"
else -> "USD"
}
}
return repository.findAllByStatusAndCurrency(status = CanStatus.SALE, currency = currency)
}
fun getCanStatus(member: Member, container: String): GetCanStatusResponse {
return GetCanStatusResponse(
chargeCan = member.getChargeCan(container),
rewardCan = member.getRewardCan(container)
)
}
fun getCanUseStatus(
member: Member,
pageable: Pageable,
timezone: String,
container: String
): List<GetCanUseStatusResponseItem> {
val zoneId = try {
ZoneId.of(timezone)
} catch (_: Exception) {
ZoneId.of("UTC")
}
return repository.getCanUseStatus(member, pageable, container)
.map {
val title: String = when (it.canUsage) {
CanUsage.HEART, CanUsage.DONATION, CanUsage.SPIN_ROULETTE -> {
if (it.roomMemberNickname != null) {
"[라이브 후원] ${it.roomMemberNickname}"
} else if (it.audioContentMemberNickname != null) {
"[콘텐츠 후원] ${it.audioContentMemberNickname}"
} else {
"[후원]"
}
}
CanUsage.CHANNEL_DONATION -> {
if (it.recipientCreatorNickname.isNullOrBlank()) {
"[채널 후원]"
} else {
"[채널 후원] ${it.recipientCreatorNickname}"
}
}
CanUsage.LIVE -> {
if (it.roomTitle != null) {
"[라이브] ${it.roomTitle}"
} else if (it.roomMemberNickname != null) {
"[라이브] ${it.roomMemberNickname}"
} else {
"[라이브]"
}
}
CanUsage.CHANGE_NICKNAME -> "닉네임 변경"
CanUsage.ALARM_SLOT -> "알람 슬롯 구매"
CanUsage.ORDER_CONTENT -> {
if (it.audioContentTitle != null) {
"[콘텐츠 구매] ${it.audioContentTitle}"
} else if (it.audioContentMemberNickname != null) {
"[콘텐츠 구매] ${it.audioContentMemberNickname}"
} else {
"[콘텐츠 구매]"
}
}
CanUsage.PAID_COMMUNITY_POST -> {
if (it.communityPostMemberNickname != null) {
"[게시글 보기] ${it.communityPostMemberNickname}"
} else {
"[게시글 보기]"
}
}
CanUsage.AUDITION_VOTE -> {
if (it.auditionTitle != null) {
"[오디션 투표] ${it.auditionTitle}"
} else {
"[오디션 투표]"
}
}
CanUsage.CHAT_MESSAGE_PURCHASE -> {
if (it.characterName != null) {
"[메시지 구매] ${it.characterName}"
} else {
"[메시지 구매]"
}
}
CanUsage.CHARACTER_IMAGE_PURCHASE -> {
if (it.characterName != null) {
"[캐릭터 이미지 구매] ${it.characterName}"
} else {
"[캐릭터 이미지 구매]"
}
}
CanUsage.CHAT_QUOTA_PURCHASE -> "캐릭터 톡 이용권 구매"
CanUsage.CHAT_ROOM_RESET -> "캐릭터 톡 초기화"
}
val createdAt = it.createdAt
.atZone(ZoneId.of("UTC"))
.withZoneSameInstant(zoneId)
GetCanUseStatusResponseItem(
title = title,
date = createdAt.format(
DateTimeFormatter.ofPattern("yyyy-MM-dd | HH:mm:ss")
),
can = it.can + it.rewardCan
)
}
}
fun getCanChargeStatus(
member: Member,
pageable: Pageable,
timezone: String,
container: String
): List<GetCanChargeStatusResponseItem> {
val zoneId = try {
ZoneId.of(timezone)
} catch (e: Exception) {
ZoneId.of("UTC")
}
return repository.getCanChargeStatus(member, pageable, container)
.map {
val canTitle = it.title ?: ""
val chargeMethod = when (it.status) {
ChargeStatus.CHARGE, ChargeStatus.EVENT -> {
it.payment!!.method ?: ""
}
ChargeStatus.REFUND_CHARGE -> {
"환불"
}
ChargeStatus.ADMIN -> {
"관리자 지급"
}
ChargeStatus.ADS -> {
"제휴보상"
}
ChargeStatus.COUPON -> {
it.payment!!.method ?: "쿠폰충전"
}
else -> {
"환불"
}
}
val createdAt = (it.createdAt ?: it.updatedAt!!)
.atZone(ZoneId.of("UTC"))
.withZoneSameInstant(zoneId)
GetCanChargeStatusResponseItem(
canTitle = canTitle,
date = createdAt.format(
DateTimeFormatter.ofPattern("yyyy-MM-dd | HH:mm:ss")
),
chargeMethod = chargeMethod
)
}
}
}