package com.ffii.fpsms.modules.pickOrder.service

import com.ffii.fpsms.modules.pickOrder.entity.TruckLaneScheduleLineStatus
import com.ffii.fpsms.modules.pickOrder.entity.TruckLaneScheduleStatus

object TruckLaneScheduleLockSupport {
    val openScheduleStatuses: List<TruckLaneScheduleStatus> =
        listOf(TruckLaneScheduleStatus.PENDING, TruckLaneScheduleStatus.APPLYING)

    val pendingLineStatus: TruckLaneScheduleLineStatus = TruckLaneScheduleLineStatus.PENDING

    private val applyingScheduleIdHolder = ThreadLocal<Long?>()

    fun applyingScheduleId(): Long? = applyingScheduleIdHolder.get()

    fun <T> withApplyingSchedule(scheduleId: Long, block: () -> T): T {
        applyingScheduleIdHolder.set(scheduleId)
        try {
            return block()
        } finally {
            applyingScheduleIdHolder.remove()
        }
    }
}
