package kr.xit.core.biz.batch.service;

import java.util.List;
import java.util.Optional;

import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import kr.xit.core.biz.batch.mapper.IBatchCmmMapper;
import kr.xit.core.biz.batch.model.BatchCmmDTO.BatchCmmRequest;
import kr.xit.core.biz.batch.model.BatchCmmDTO.BatchLock;
import kr.xit.core.exception.BizRuntimeException;

/**
 * <pre>
 * description :
 *
 * packageName : kr.xit.core.biz.service
 * fileName    : BatchLockService
 * author      : limju
 * date        : 2023-05-18
 * ======================================================================
 * 변경일         변경자        변경 내용
 * ----------------------------------------------------------------------
 * 2023-05-18    limju       최초 생성
 *
 * </pre>
 */
@Service
public class BatchCmmService extends EgovAbstractServiceImpl implements IBatchCmmService {
    private final IBatchCmmMapper mapper;

    public BatchCmmService(IBatchCmmMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<BatchLock> findById(final String instanceId) {
        return mapper.selectBatchLockByInstanceId(instanceId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addBatchLock(final BatchLock dto) {
        mapper.insertBatchLock(dto);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void modifyBatchLock(final BatchLock dto) {
        mapper.updateBatchLock(dto);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addBatchLog(final BatchLock dto) {
        mapper.insertBatchLog(dto);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void modifyBatchLog(final BatchLock dto) {
        mapper.updateBatchLog(dto);
    }

    //--------------------------------------------------------------
    // 배치 관리
    //--------------------------------------------------------------
    @Override
    @Transactional(readOnly = true)
    public List<BatchLock> findBatchLocks(){
        return mapper.selectBatchLocks();
    }

    @Override
    @Transactional(readOnly = true)
    public String execBatch(final BatchCmmRequest reqDTO){
        BatchLock rtnDTO = mapper.selectBatchLock(reqDTO.getInstanceId()).orElseThrow(() -> BizRuntimeException.create("배치 인스턴스를 찾을 수 없습니다."));
        reqDTO.setProfile(reqDTO.getProfile());

        String uri = rtnDTO.getInstanceUrl();

        return null;
    }

}
