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

import java.util.*;

import org.egovframe.rte.fdl.cmmn.*;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;

import kr.xit.core.biz.batch.mapper.*;
import kr.xit.core.biz.batch.model.BatchCmmDTO.*;
import kr.xit.core.exception.*;

@Service
public class BatchCmmService extends EgovAbstractServiceImpl implements IBatchCmmService {
    private final IBatchCmmMapper batchCmmMapper;

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

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

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

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

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

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

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

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

        String uri = rtnDTO.getInstanceUrl();

        return null;
    }

}
