package kr.xit.batch.ens.scheduler;

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

import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import kr.xit.batch.ens.job.SndngAcceptJobConfig;
import kr.xit.batch.ens.task.cmm.TaskCmmUtils;
import kr.xit.biz.common.ApiConstants;
import kr.xit.biz.common.ApiConstants.SignguCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <pre>
 * description : 연계 발송 - 접수 데이타 생성
 *               대상 : 연계발송마스터(tb_cntc_sndng_mastr) : sndng_process_sttus = 'accept'
 *                     연계발송상세(tb_cntc_sndng_detail)
 *               ->  통합발송 마스터(tb_ens_unity_sndng_mastr)
 *                   통합발송상세(tb_ens_unity_sndng_detail)
 *                : 연계발송마스터 and tb_ens_unity_sndng_mastr - sndng_process_sttus = 'accept-ok'
 * packageName : kr.xit.batch.ens.scheduler
 * fileName    : SndngAcceptJobScheduler
 * author      : limju
 * date        : 2023-06-12
 * ======================================================================
 * 변경일         변경자        변경 내용
 * ----------------------------------------------------------------------
 * 2023-06-12    limju       최초 생성
 *
 * </pre>
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class SndngAcceptJobScheduler {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    private final JobLauncher jobLauncher;
    private final SndngAcceptJobConfig jobConfiguration;
    @Value("${app.slack-webhook.enabled:false}")
    private String isSlackEnabled;

    @Scheduled(cron = "${app.batch.cron.ens.accept}")
    public void runJob() {

        Map<String, JobParameter> confMap = new HashMap<>();
        confMap.put("startDate", new JobParameter(new Date()));
        confMap.put("isSlackEnabled", new JobParameter(isSlackEnabled));
        confMap.put("sndngProcessSttus", new JobParameter(ApiConstants.SndngProcessStatus.ACCEPT.getCode()));

        try {
            for(SignguCode signguCode : TaskCmmUtils.getSignguCodeList()) {
                confMap.put("signguCode", new JobParameter(signguCode.getCode()));
                confMap.put("ffnlgCode", new JobParameter(ApiConstants.FFNLN_CODE));
                JobParameters jobParameters = new JobParameters(confMap);
                jobLauncher.run(jobConfiguration.sndngAcceptJob(), jobParameters);
            }

        } catch (JobExecutionAlreadyRunningException | JobInstanceAlreadyCompleteException
                 | JobParametersInvalidException | org.springframework.batch.core.repository.JobRestartException e) {

            log.error(e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
