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.SndngCloseJobConfig;
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 :상태 close - send-ok
 *              대상 : 연계 발송 마스터(tb_cntc_sndng_mastr)
 *                ->  연계 발송 마스터(tb_cntc_sndng_mastr)
 *                    통합 발송 마스터(tb_ens_unity_sndng_mastr)
 *                    발송 마스터(tb_ens_sndng_mastr)
 * packageName : kr.xit.batch.ens.scheduler
 * fileName    : SndngCloseJobScheduler
 * author      : seojh
 * date        : 2023-06-14
 * ======================================================================
 * 변경일         변경자        변경 내용
 * ----------------------------------------------------------------------
 * 2023-06-14    seojh       최초 생성
 *
 * </pre>
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class SndngCloseJobScheduler {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    private final JobLauncher jobLauncher;
    private final SndngCloseJobConfig jobConfiguration;
    @Value("${app.slack-webhook.enabled:false}")
    private String isSlackEnabled;

    @Scheduled(cron = "${app.batch.cron.ens.close}")
    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.SEND_OK.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.sndngCloseJob(), jobParameters);
            }

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

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