package com.lantone.qc.kernel.catalogue.threelevelward;

import com.google.common.collect.Lists;
import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
import com.lantone.qc.pub.model.doc.LisDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HUJING
 * @create 2020-08-14 15:52
 * @desc 住院48小时以上无血尿常规化验结果
 **/
@Component
public class THR03065 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        List<LisDoc> lisDocs = inputInfo.getLisDocs();
        if (lisDocs == null) {
            return;
        }
        if (leaveHospitalDoc != null) {
            String beginDateStr = leaveHospitalDoc.getStructureMap().get("入院时间");
            String endDateStr = leaveHospitalDoc.getStructureMap().get("出院时间");
            if (StringUtil.isBlank(beginDateStr) || StringUtil.isBlank(endDateStr)) {
                return;
            }
            Date beginDate = StringUtil.parseDateTime(beginDateStr);
            Date endDate = StringUtil.parseDateTime(endDateStr);
            if (beginDate == null || endDate == null) {
                return;
            }
            if (!CatalogueUtil.compareTime(beginDate, endDate, 48 * 60L)) {
                return;
            }
        }
        List<String> lisName = lisDocs.stream().map(LisDoc::getStructureMap).map(x -> x.get("报告名称")).collect(Collectors.toList());
        //      血常规                尿常规
        boolean findCBC = false, findUrineTest = false;
        for (String lis : lisName) {
            if (lis.contains("血常规") || lis.contains("CBC")) {
                findCBC = true;
            }
            if (lis.contains("尿常规")) {
                findUrineTest = true;
            }
        }

        List<String> miss = Lists.newArrayList();
        if (!findCBC) {
            miss.add("血常规");
        }
        if (!findUrineTest) {
            miss.add("尿常规");
        }

        if (miss.size() > 0) {
            status.set("-1");
            info.set(miss.toString().replaceAll("[\\[\\]]", ""));
        }
    }

}
