package com.docus.server.sdry.job;

import com.alibaba.fastjson.JSONObject;
import com.docus.core.util.Func;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.infrastructure.web.api.ResultCode;
import com.docus.server.archive.dto.PatientInfoDto;
import com.docus.server.archive.mapper.TBasicMapper;
import com.docus.server.rpc.DownPlatformService;
import com.docus.server.rpc.TaskDistributeService;
import com.docus.server.rpc.dto.*;
import com.docus.server.sdry.collector.ShouMaCollectResult;
import com.docus.server.sdry.collector.ShouMaCollector;
import com.docus.server.sdry.mapper.ShouMaViewMapper;
import com.xxl.job.core.context.XxlJobHelper;
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.util.Collections;
import java.util.List;

/**
 * @author YongBin Wen
 * @date 2024/7/12 15:22
 */
@Component
@Slf4j
public class CollectJob {
    @Autowired
    private DownPlatformService downPlatformService;
    @Autowired
    private TaskDistributeService taskDistributeService;
    @Autowired
    private TBasicMapper tBasicMapper;
    @Autowired
    private ShouMaViewMapper shouMaViewMapper;

    @XxlJob("Sdry-ShouMa-Collect")
    public void shouMaCollect() {
        log.info(">>>>>>>>>>>>>>>>>>>>>> 顺德人医手麻视图采集任务开始！");
        // 1.获取任务和患者信息 2.查询视图信息 3.上报下载
        String jobParam = XxlJobHelper.getJobParam();
        JSONObject jobParamJsonObject = JSONObject.parseObject(jobParam);
        String assortId = jobParamJsonObject.getString("assortId");
        String collectorId = jobParamJsonObject.getString("collectorId");
        String extraCondition = jobParamJsonObject.getString("extraCondition");
        if (Func.isBlank(assortId) || Func.isBlank(collectorId)) {
            log.warn(">>>>>>>>>>>>>>>>>>>>>> 顺德人医手麻视图采集,未配置手麻采集器和采集文件分段");
            return;
        }
        CommonResult<ReportDownTwoDto> commonResult = taskDistributeService.getNoViewTaskByCollectorId(collectorId);
        if (ResultCode.SUCCESS.getCode().equals(commonResult.getCode())
                && commonResult.getData() != null) {
            ReportDownTwoDto downTwoDto = commonResult.getData();
            ReportTaskTwoDto task = downTwoDto.getTasks().get(0);
            log.info("获取了任务：{}", Func.toJson(task));

            ReportHospitalTwoDto hospitalTwoDto = downTwoDto.getHospitals().get(0);
            ReportPatientTwoDto reportPatientTwoDto = downTwoDto.getPatient();
            PatientInfoDto patientInfoDto = new PatientInfoDto();
            patientInfoDto.setInpatientNo(reportPatientTwoDto.getInpatientNo());
            patientInfoDto.setAdmissTimes(hospitalTwoDto.getAdmissTimes());
            ShouMaCollector shouMaCollector = new ShouMaCollector(shouMaViewMapper, patientInfoDto, extraCondition);
            List<ShouMaCollectResult> shouMaCollectResultList = shouMaCollector.collect();
            int size = shouMaCollectResultList.size();
            log.info("住院号：{}，住院次数：{} 采集手麻数据：{} 条！", patientInfoDto.getInpatientNo(), patientInfoDto.getAdmissTimes(), size);
            if (size <= 0) {
                return;
            }
            tBasicMapper.invalidFileBySource(downTwoDto.getPatientId(),collectorId);

            ReportDownPatientDto patient = new ReportDownPatientDto();
            patient.setPatientid(downTwoDto.getPatientId());

            ReportDownDto reportDownDto = new ReportDownDto();
            reportDownDto.setAssortid(assortId);
            reportDownDto.setCollectorid(collectorId);
            reportDownDto.setIp("");
            reportDownDto.setPatient(patient);

            for (ShouMaCollectResult result : shouMaCollectResultList) {
                ReportDownScanFileDto reportDownScanFileDto = new ReportDownScanFileDto();
                reportDownScanFileDto.setDownurl(result.getFilePath());
                reportDownScanFileDto.setFiletitle(result.getFileTitle());
                reportDownScanFileDto.setSerialnum(result.getFileNo());
                reportDownScanFileDto.setFilesource(1);
                reportDownScanFileDto.setFiletype(1);
                reportDownScanFileDto.setFilestoragetype(1);
                reportDownScanFileDto.setTaskid(task.getTaskId());
                List<ReportDownScanFileDto> scanFiles = Collections.singletonList(reportDownScanFileDto);
                reportDownDto.setScanfiles(scanFiles);
                downPlatformService.report(reportDownDto);
            }
        }
        log.info(">>>>>>>>>>>>>>>>>>>>>> 顺德人医手麻视图采集任务结束！");
    }

}
