package com.docus.server.archive.job;

import com.docus.core.util.Func;
import com.docus.infrastructure.core.utils.TableJsonRead;
import com.docus.server.archive.service.ZkInfoSyncService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Objects;

/**
 * @author YongBin Wen
 * @date 2024/4/9 14:07
 */
@Component
@Slf4j
public class SyncZkInfoJob {

    @Resource
    private ZkInfoSyncService zkInfoSyncService;

    /**
     * 农垦转科信息同步，默认同步当天和前面一天的和所有出院的
     *
     * @date 2024/3/27 16:27
     * @author YongBin Wen
     */
    @XxlJob("NkZkInfoSyncJob")
    public void nkZkInfoSyncJob() {
        log.info("====================> 农垦转科信息同步任务开始 ===================");
        try {
            nkZkInfoSync();
        } catch (Exception e) {
            log.error("====================> 农垦转科信息同步任务出错啦！===================" + e.getMessage(), e);
            return;
        }
        log.info("====================> 农垦转科信息同步任务结束 ===================");
    }

    private void nkZkInfoSync() {
        String jobConfigPath = "data-config\\job-config";
        String jobConfigName = "NkZkInfoSyncJob.json";
        TableJsonRead jsonReader = new TableJsonRead();
        NkZkInfoSyncJobConfig syncJobConfig = jsonReader.Read(jobConfigPath, jobConfigName, NkZkInfoSyncJobConfig.class);
        syncJobConfig = NkZkInfoSyncJobConfig.checkAndInit(syncJobConfig);
        Integer pageSize = syncJobConfig.getPageSize();
        Integer pageNumber = syncJobConfig.getPageNumber();
        String startDate = syncJobConfig.getStartDate();
        LocalDate runLocalDate = LocalDate.now();
        String runJobDate = runLocalDate.toString();

        while (true) {
            int syncCount = zkInfoSyncService.syncZkInfo(startDate + " 00:00:00", runJobDate + " 23:59:59", pageNumber, pageSize);
            if (syncCount < pageSize) {
                break;
            }
            pageNumber += 1;
        }
        // 下次同步则开始多同步一天，防止时间差引起同步缺漏
        String nexRunStartDate = runLocalDate.plusDays(-1).toString();
        syncJobConfig.setPageNumber(1);
        syncJobConfig.setStartDate(nexRunStartDate);
        jsonReader.Save(jobConfigPath, jobConfigName, Func.toJson(syncJobConfig));
    }

    private static class NkZkInfoSyncJobConfig {
        private String startDate;
        private Integer pageNumber;
        private Integer pageSize;

        public static NkZkInfoSyncJobConfig checkAndInit(NkZkInfoSyncJobConfig jobConfig) {
            if (Objects.isNull(jobConfig)) {
                jobConfig = new NkZkInfoSyncJobConfig();
            }
            if (Objects.isNull(jobConfig.getPageNumber()) || jobConfig.getPageNumber() <= 0) {
                jobConfig.setPageNumber(1);
            }
            if (Objects.isNull(jobConfig.getPageSize()) || jobConfig.getPageSize() <= 0) {
                jobConfig.setPageSize(100);
            }
            if (Objects.isNull(jobConfig.getStartDate())) {
                jobConfig.setStartDate(LocalDate.now().toString());
            }
            return jobConfig;
        }

        public String getStartDate() {
            return startDate;
        }

        public void setStartDate(String startDate) {
            this.startDate = startDate;
        }

        public Integer getPageNumber() {
            return pageNumber;
        }

        public void setPageNumber(Integer pageNumber) {
            this.pageNumber = pageNumber;
        }

        public Integer getPageSize() {
            return pageSize;
        }

        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
    }
}
