package cokr.xit.ens.modules.common.ctgy.sys.mng.service;

import java.util.HashMap;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import cokr.xit.ens.modules.common.ctgy.sys.mng.mapper.IKeySequenceMapper;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class KeySequenceService {

    private final IKeySequenceMapper mapper;

    /**
     * 키 시퀀스 조회
     * @param seqName 키 시퀀스 이름
     * @return Long
     */
    @Transactional
    public Long getKeySequence(String seqName) {
        Long nextSeq = mapper.selectKeySequence(seqName);
        if(nextSeq == null) {
            nextSeq = 1L;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("seqName", seqName);
        map.put("nextVal", nextSeq);
        mapper.saveKeySequence(map);
        return nextSeq;
    }
}
