package com.lantone.qc.kernel.catalogue.beilun.clinicalblood;

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.ClinicBloodEffectDoc;
import com.lantone.qc.pub.model.doc.ClinicalBloodDoc;
import com.lantone.qc.pub.util.DateUtil;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName : CLI0309
 * @Description : 输血后效果评价未在24小时完成
 * @Author : 王宇
 * @Date: 2020-08-14 10:18
 */
@Component
public class CLI03063 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        List<ClinicalBloodDoc> clinicalBloodDocs = inputInfo.getClinicalBloodDocs();//输血记录
        List<ClinicBloodEffectDoc> clinicBloodEffectDocs = inputInfo.getClinicBloodEffectDocs();//输血效果评价
        if (ListUtil.isEmpty(clinicalBloodDocs)) {
            return;
        }
        Iterator<ClinicalBloodDoc> clinicalBloodDocIterator = clinicalBloodDocs.iterator();
        Iterator<ClinicBloodEffectDoc> clinicBloodEffectDocIterator = clinicBloodEffectDocs.iterator();
        while (clinicalBloodDocIterator.hasNext()) {
            if (!CatalogueUtil.compareTime(
                    StringUtil.parseDateTime(clinicalBloodDocIterator.next().getStructureMap().get("记录时间")),
                    StringUtil.parseDateTime(DateUtil.nowString()),
                    Long.valueOf(24 * 60))) {//如果接收未超过24小时，规则不判断
                clinicalBloodDocIterator.remove();
            }
        }
        while (clinicBloodEffectDocIterator.hasNext()) {
            if (!CatalogueUtil.compareTime(
                    StringUtil.parseDateTime(clinicBloodEffectDocIterator.next().getStructureMap().get("记录时间")),
                    StringUtil.parseDateTime(DateUtil.nowString()),
                    Long.valueOf(24 * 60))) {//如果接收未超过24小时，规则不判断
                clinicBloodEffectDocIterator.remove();
            }
        }
        Collections.sort(clinicalBloodDocs, new Comparator<ClinicalBloodDoc>() {
            public int compare(ClinicalBloodDoc o1, ClinicalBloodDoc o2) {
                return o1.getStructureMap().get("记录时间")
                        .compareTo(o2.getStructureMap().get("记录时间"));
            }
        });
        Collections.sort(clinicBloodEffectDocs, new Comparator<ClinicBloodEffectDoc>() {
            public int compare(ClinicBloodEffectDoc o1, ClinicBloodEffectDoc o2) {
                return o1.getStructureMap().get("记录时间")
                        .compareTo(o2.getStructureMap().get("记录时间"));
            }
        });
        if (clinicalBloodDocs.size() != clinicBloodEffectDocs.size()) {
            status.set("-1");
            return;
        }
        for (ClinicalBloodDoc clinicalBloodDoc : clinicalBloodDocs) {
            for (ClinicBloodEffectDoc clinicBloodEffectDoc : clinicBloodEffectDocs) {
                if (CatalogueUtil.compareTime(
                        StringUtil.parseDateTime(clinicalBloodDoc.getStructureMap().get("记录时间")),
                        StringUtil.parseDateTime(clinicBloodEffectDoc.getStructureMap().get("记录时间")),
                        Long.valueOf(1))) {//结果单时间要比申请单大的
                    if (CatalogueUtil.compareTime(
                            StringUtil.parseDateTime(clinicalBloodDoc.getStructureMap().get("记录时间")),
                            StringUtil.parseDateTime(clinicBloodEffectDoc.getStructureMap().get("记录时间")),
                            Long.valueOf(24 * 60))) {//未超过24小时
                        status.set("-1");
                        return ;
                    }
                }
            }
        }
        /*
        if (clinicalBloodDocs != null && clinicalBloodDocs.size() > 0) {
            for (ClinicBloodEffectDoc cliB : clinicBloodEffectDocs) {
                Map<String, String> cliBStructureMap = cliB.getStructureMap();
                if (StringUtils.isEmpty(cliBStructureMap.get("输注后效果评价"))) {
                    status.set("-1");
                    break;
                }
            }
        }

         */
    }
}