feat(agent-ratio): 에이전트 정산 비율 관리 기능을 추가한다
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package kr.co.vividnext.sodalive.admin.partner.agent.ratio
|
||||
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.partner.agent.ratio.CreateAgentSettlementRatioRequest
|
||||
import org.springframework.data.domain.Pageable
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@RequestMapping("/admin/partner/agent/ratio")
|
||||
class AdminAgentSettlementRatioController(private val service: AdminAgentSettlementRatioService) {
|
||||
@PostMapping
|
||||
fun createAgentSettlementRatio(@RequestBody request: CreateAgentSettlementRatioRequest) =
|
||||
ApiResponse.ok(service.createAgentSettlementRatio(request))
|
||||
|
||||
@PostMapping("/update")
|
||||
fun updateAgentSettlementRatio(@RequestBody request: CreateAgentSettlementRatioRequest) =
|
||||
ApiResponse.ok(service.updateAgentSettlementRatio(request))
|
||||
|
||||
@GetMapping
|
||||
fun getAgentSettlementRatio(pageable: Pageable) = ApiResponse.ok(
|
||||
service.getAgentSettlementRatio(
|
||||
offset = pageable.offset,
|
||||
limit = pageable.pageSize.toLong()
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package kr.co.vividnext.sodalive.admin.partner.agent.ratio
|
||||
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.member.MemberRepository
|
||||
import kr.co.vividnext.sodalive.member.MemberRole
|
||||
import kr.co.vividnext.sodalive.partner.agent.ratio.AgentSettlementRatio
|
||||
import kr.co.vividnext.sodalive.partner.agent.ratio.AgentSettlementRatioRepository
|
||||
import kr.co.vividnext.sodalive.partner.agent.ratio.CreateAgentSettlementRatioRequest
|
||||
import kr.co.vividnext.sodalive.partner.agent.ratio.GetAgentSettlementRatioResponse
|
||||
import org.springframework.dao.DataIntegrityViolationException
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@Service
|
||||
class AdminAgentSettlementRatioService(
|
||||
private val repository: AgentSettlementRatioRepository,
|
||||
private val memberRepository: MemberRepository
|
||||
) {
|
||||
@Transactional
|
||||
fun createAgentSettlementRatio(request: CreateAgentSettlementRatioRequest) {
|
||||
validateSettlementRatio(request.settlementRatio)
|
||||
val agent = getAgent(request.memberId)
|
||||
closeCurrentRatioIfExists(request)
|
||||
validateClosedHistory(request)
|
||||
|
||||
val ratio = request.toEntity()
|
||||
ratio.member = agent
|
||||
saveRatioOrThrow(request, ratio)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun updateAgentSettlementRatio(request: CreateAgentSettlementRatioRequest) {
|
||||
validateSettlementRatio(request.settlementRatio)
|
||||
val agent = getAgent(request.memberId)
|
||||
|
||||
val existing = repository.findFirstByMemberIdAndEffectiveToIsNull(request.memberId)
|
||||
?: throw SodaException(messageKey = "partner.agent.ratio.not_found")
|
||||
validateNewEffectiveFrom(existing.effectiveFrom, request.effectiveFrom)
|
||||
existing.close(request.effectiveFrom)
|
||||
repository.save(existing)
|
||||
|
||||
val ratio = request.toEntity()
|
||||
ratio.member = agent
|
||||
saveRatioOrThrow(request, ratio)
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
fun getAgentSettlementRatio(offset: Long, limit: Long): GetAgentSettlementRatioResponse {
|
||||
val totalCount = repository.getAgentSettlementRatioTotalCount()
|
||||
val items = repository.getAgentSettlementRatio(offset = offset, limit = limit)
|
||||
return GetAgentSettlementRatioResponse(totalCount = totalCount, items = items)
|
||||
}
|
||||
|
||||
private fun getAgent(memberId: Long) = memberRepository.findByIdForUpdate(memberId)
|
||||
?.also {
|
||||
if (it.role != MemberRole.AGENT) {
|
||||
throw SodaException(messageKey = "partner.agent.ratio.invalid_agent")
|
||||
}
|
||||
}
|
||||
?: throw SodaException(messageKey = "partner.agent.ratio.agent_not_found")
|
||||
|
||||
private fun closeCurrentRatioIfExists(request: CreateAgentSettlementRatioRequest) {
|
||||
val existing = repository.findFirstByMemberIdAndEffectiveToIsNull(request.memberId) ?: return
|
||||
validateNewEffectiveFrom(existing.effectiveFrom, request.effectiveFrom)
|
||||
existing.close(request.effectiveFrom)
|
||||
repository.save(existing)
|
||||
}
|
||||
|
||||
private fun validateNewEffectiveFrom(
|
||||
currentEffectiveFrom: LocalDateTime,
|
||||
newEffectiveFrom: LocalDateTime
|
||||
) {
|
||||
if (!newEffectiveFrom.isAfter(currentEffectiveFrom)) {
|
||||
throw SodaException(messageKey = "partner.agent.ratio.invalid_effective_from")
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateClosedHistory(request: CreateAgentSettlementRatioRequest) {
|
||||
val hasOverlap = repository.findAllByMemberIdOrderByEffectiveFromAsc(request.memberId)
|
||||
.any { history ->
|
||||
val effectiveTo = history.effectiveTo ?: return@any false
|
||||
!request.effectiveFrom.isBefore(history.effectiveFrom) && request.effectiveFrom.isBefore(effectiveTo)
|
||||
}
|
||||
if (hasOverlap) {
|
||||
throw SodaException(messageKey = "partner.agent.ratio.invalid_effective_from")
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateSettlementRatio(settlementRatio: Int) {
|
||||
if (settlementRatio !in 0..100) {
|
||||
throw SodaException(messageKey = "common.error.invalid_request")
|
||||
}
|
||||
}
|
||||
|
||||
private fun saveRatioOrThrow(
|
||||
request: CreateAgentSettlementRatioRequest,
|
||||
ratio: AgentSettlementRatio
|
||||
) {
|
||||
try {
|
||||
repository.saveAndFlush(ratio)
|
||||
} catch (e: DataIntegrityViolationException) {
|
||||
repository.findFirstByMemberIdAndEffectiveToIsNull(request.memberId)
|
||||
?: throw e
|
||||
throw SodaException(messageKey = "partner.agent.ratio.invalid_effective_from")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user