package com.caliverse.admin.global.component.transaction;

import com.caliverse.admin.global.common.utils.CommonUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import static com.caliverse.admin.global.common.constants.CommonConstants.TRANSACTION_ID_KEY;

@Component
public class TransactionIdManager {

    public String getCurrentTransactionId() {
        String currentId = (String) TransactionSynchronizationManager.getResource(TRANSACTION_ID_KEY);
        if (currentId == null) {
            currentId = CommonUtils.getCreateGuId();
            TransactionSynchronizationManager.bindResource(TRANSACTION_ID_KEY, currentId);
        }
        return currentId;
    }

    public String getNewTransactionId() {
        String newId = CommonUtils.getCreateGuId();
        TransactionSynchronizationManager.bindResource(TRANSACTION_ID_KEY, newId);
        return newId;
    }

}
