package cokr.xit.ens.modules.common.biztmplt;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.EnsException;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.core.utils.CmmnUtil;
import cokr.xit.ens.modules.common.code.StatCd;
import cokr.xit.ens.modules.common.domain.SendMast;
import cokr.xit.ens.modules.common.domain.repository.SendMastRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public abstract class SendProcTemplate implements EnsPhaseProcSupport<EnsResponseVO, Long> {

    @Autowired
    private SendMastRepository sendMastRepository;


    /**
     * 전송준비
     * @param sendMastIds
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public EnsResponseVO statReady(List<Long> sendMastIds){
        EnsResponseVO respVO = null;
        try {
            /**
             * 필수값 확인
             */
            if (CmmnUtil.isEmpty(sendMastIds))
                throw new EnsException(EnsErrCd.SEND410, "발송마스터ID(은)는 필수조건 입니다.");

            /**
             * 저장
             *  -. 발송마스터의 상태를 "전송준비(sendrdy)" 으로 변경 한다.
             */
            List<Long> resultInfo = sendMastRepository.findAllBySendMastIdIn(sendMastIds).stream()
                    .map(row -> {
                        row.setStatCd(StatCd.sendrdy);
                        return row.getSendMastId();
                    })
                    .collect(Collectors.toList());
            ;
           
            respVO = EnsResponseVO.okBuilder().resultInfo(resultInfo).build();
        } catch (EnsException e) {
           
            respVO = EnsResponseVO.errBuilder().errCode(e.getErrCd()).errMsg(e.getMessage()).build();
        } catch (Exception e) {
           
            respVO = EnsResponseVO.errBuilder().errCode(EnsErrCd.SEND500).errMsg(e.getMessage()).build();
        }

        return respVO;
    }


    /**
     * 전송시작
     * @param sendMastId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public EnsResponseVO statBegin(Long sendMastId) {

        EnsResponseVO respVO = null;
        try {
            /**
             * 필수값 확인
             */
            if (CmmnUtil.isEmpty(sendMastId))
                throw new EnsException(EnsErrCd.SEND410, "발송마스터ID(은)는 필수조건 입니다.");

            /**
             * 저장
             *  -. 발송마스터의 상태를 "전송중(sending)" 으로 변경 한다.
             */
            SendMast sendMast = sendMastRepository.findById(sendMastId).orElseThrow(()->new EnsException(EnsErrCd.SEND404, String.format("일치하는 발송마스터 자료가 없습니다. [ sendMastId %s ]", sendMastId)));
            sendMast.setStatCd(StatCd.sending);

           
            respVO = EnsResponseVO.okBuilder().resultInfo(sendMastId).build();
        } catch (EnsException e) {
           
            respVO = EnsResponseVO.errBuilder().errCode(e.getErrCd()).errMsg(e.getMessage()).build();
        } catch (Exception e) {
           
            respVO = EnsResponseVO.errBuilder().errCode(EnsErrCd.SEND500).errMsg(e.getMessage()).build();
        }

        return respVO;
    }
}
