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.PatientInfoSyncService;
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/3/27 16:23
 */
@Component
@Slf4j
public class PatientInfoSyncJob {
    @Resource
    private PatientInfoSyncService patientInfoSyncService;

    /**
     * 佛山三院基础数据同步,默认同步当天和前面一天的和所有在院的
     *
     * @date 2024/3/27 16:27
     * @author YongBin Wen
     */
    @XxlJob("FoShanSyPatientInfoSyncJob")
    public void foShanSyPatientInfoSyncJob() {
        log.info("====================> 佛山三院基础数据同步任务开始 ===================");
        String jobConfigPath = "data-config\\job-config";
        String jobConfigName = "FoShanSyPatientInfoSyncJob.json";
        TableJsonRead jsonReader = new TableJsonRead();
        FoShanSyPatientInfoSyncJobConfig syncJobConfig = jsonReader.Read(jobConfigPath, jobConfigName, FoShanSyPatientInfoSyncJobConfig.class);
        syncJobConfig = FoShanSyPatientInfoSyncJobConfig.checkAndInit(syncJobConfig);
        Integer pageSize = syncJobConfig.getPageSize();
        Integer pageNumber = syncJobConfig.getPageNumber();
        String startDate = syncJobConfig.getStartDate();
        LocalDate runLocalDate = LocalDate.now();
        String runJobDate = runLocalDate.toString();

        patientInfoSyncService.syncInHospitalData();
        while (true) {
            int syscCount = patientInfoSyncService.syncDischargeData(startDate + " 00:00:00", runJobDate + " 23:59:59", pageNumber, pageSize);
            if (syscCount < pageSize) {
                break;
            }
            pageNumber += 1;
        }
        // 下次同步则开始多同步一天，防止时间差引起同步缺漏 例如  10分钟同步一次 导致 23:50:01 出院未同步到
        String nexRunStartDate = runLocalDate.plusDays(-1).toString();
        syncJobConfig.setPageNumber(1);
        syncJobConfig.setStartDate(nexRunStartDate);
        jsonReader.Save(jobConfigPath, jobConfigName, Func.toJson(syncJobConfig));
        log.info("====================> 佛山三院基础数据同步任务结束 ===================");
    }

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

        public static FoShanSyPatientInfoSyncJobConfig checkAndInit(FoShanSyPatientInfoSyncJobConfig jobConfig) {
            if (Objects.isNull(jobConfig)) {
                jobConfig = new FoShanSyPatientInfoSyncJobConfig();
            }
            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;
        }
    }
}
