package com.docus.server.archive.job;

import com.docus.core.util.Func;
import com.docus.infrastructure.core.utils.TableJsonRead;
import com.docus.server.archive.entity.TBasic;
import com.docus.server.archive.mapper.TBasicMapper;
import com.docus.server.trusthis.mapper.JsOrdersMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 茂名人医医嘱序号同步
 *
 * @author YongBin Wen
 * @date 2024/3/11 8:59
 */
@Slf4j
@Component
public class MmRyOrderIndexSyncJob {

    private TBasicMapper tBasicMapper;

    private JsOrdersMapper jsOrdersMapper;

    @Autowired
    public MmRyOrderIndexSyncJob(TBasicMapper tBasicMapper, JsOrdersMapper jsOrdersMapper) {
        this.tBasicMapper = tBasicMapper;
        this.jsOrdersMapper = jsOrdersMapper;
    }

    public TBasicMapper gettBasicMapper() {
        return tBasicMapper;
    }

    public JsOrdersMapper getJsOrdersMapper() {
        return jsOrdersMapper;
    }

    @XxlJob("MmRyOrderIndexSyncJob")
    public void mmRyOrderIndexSyncJob() {
        LocalDateTime now = LocalDateTime.now();
        String configPath = "data-config";
        String configFileName = "order-index-config";
        TableJsonRead jsonRead = new TableJsonRead();
        MmRyOrderIndexSyncJobConfig config = jsonRead.Read(configPath, configFileName, MmRyOrderIndexSyncJobConfig.class);
        if (Objects.isNull(config)) {
            config = new MmRyOrderIndexSyncJobConfig();
            LocalDateTime dateTime = now.plusDays(-config.getDay());
            config.setDateTime(Func.formatDateTime(dateTime));
        }

        int currentSyncCount = 0;
        try {
            while (true) {
                List<TBasic> tBasicList = tBasicMapper.getBasicByDate(config.getDateTime(), config.getOffset(), config.getSize());
                int basicSize = tBasicList.size();
                log.info("========== 茂名人医，根据日期同步医嘱序号JOB，日期：{}  limit {},{} 查询基础数据{}条 ==========", config.getDateTime(), config.getOffset(), config.getSize(), basicSize);
                currentSyncCount += basicSize;
                if (basicSize == 0) {
                    log.info("========== 茂名人医，根据日期同步医嘱序号JOB，日期：{} 同步基础数据 {} 条 ,JOB 结束==========", config.getDateTime(), currentSyncCount);
                    config.setOffset(0);
                    config.setDateTime(Func.formatDateTime(now));
                    jsonRead.Save(configPath, configFileName, Func.toJson(config));
                    return;
                }
                for (TBasic basic : tBasicList) {
                    mmRyOrderIndexSync(basic);
                }
                config.setOffset(config.getOffset() + config.getSize());
            }
        } catch (Exception ex) {
            log.error("茂名人医，根据日期同步医嘱序号JOB，日期：" + config.getDateTime() + " 发生错误," + ex.getMessage(), ex);
        }


    }

    public void mmRyOrderIndexSync(TBasic basic) {
        Date disDate = basic.getDisDate();
        String inpatientNo = basic.getInpatientNo();
        String date = Func.formatDate(disDate);
        List<String> orderIndex = jsOrdersMapper.getOrderIndex(inpatientNo, date, date);
        log.info("茂名人医，同步医嘱序号，病案号：{}，出院日期：{},同步医嘱序号为：{} ,病案主键：{}", inpatientNo, date, orderIndex,basic.getPatientId());
        if (Func.isNotEmpty(orderIndex)) {
            tBasicMapper.updateOrderIndex(basic.getPatientId(),String.join(",",orderIndex));
        }
    }

    private static class MmRyOrderIndexSyncJobConfig {
        private String dateTime;
        private int day = 7;
        private int offset = 0;
        private int size = 100;

        public String getDateTime() {
            return dateTime;
        }

        public void setDateTime(String dateTime) {
            this.dateTime = dateTime;
        }

        public int getOffset() {
            return offset;
        }

        public void setOffset(int offset) {
            this.offset = offset;
        }

        public int getSize() {
            return size;
        }

        public void setSize(int size) {
            this.size = size;
        }

        public int getDay() {
            return day;
        }

        public void setDay(int day) {
            this.day = day;
        }
    }


}
