package com.style24.core.biz.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.style24.core.biz.dao.TscCommonDao;
import com.style24.persistence.domain.Sequence;

import lombok.extern.slf4j.Slf4j;

/**
 * 공용 Service
 *
 * @author eskim
 * @since 2020. 10. 20
 */
@Service
@Slf4j
public class TscCommonService {

	@Autowired
	private TscCommonDao commonDao;

	/**
	 * 시퀀스 조회(중첩트랜잭션으로 처리. 부모 트랜잭션의 커밋과 롤백에는 영향을 받지만 자신의 커밋과 롤백은 부모 트랜잭션에게 영향을 주지 않는다.)
	 * @param sequenceNm - 시퀀스명
	 * @return
	 * @author gagamel
	 * @since 2021. 1. 4
	 */
	@Transactional(value = "shopTxnManager", isolation = Isolation.SERIALIZABLE, propagation = Propagation.NESTED)
	public Integer getNextSequence(String sequenceNm) {
		Integer nextVal = commonDao.getNextSequence(sequenceNm);

		Sequence sequence = new Sequence();
		sequence.setSequenceNm(sequenceNm);
		sequence.setNextVal(nextVal);
		commonDao.createNextSequence(sequence);

		return sequence.getNextVal();
	}
}
