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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.entity.Diag;
import com.lantone.qc.pub.model.entity.Family;
import com.lantone.qc.pub.model.label.FamilyLabel;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : BEH0070
 * @Description : 家族史未描述是否有家族性遗传病史及传染病史
 * @Author : 胡敬
 * @Date: 2020-03-11 15:09
 */
@Component
public class BEH0070 extends QCCatalogue {
    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        Map<String, String> structureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
        String familyText = structureMap.get("家族史");
        if (StringUtil.isNotBlank(familyText)) {
            familyText = StringUtil.removeBlank(familyText);
            if (familyText.contains("家族遗传病") || familyText.contains("结核")) {
                status.set("0");
                return;
            }
        }
        if (inputInfo.getBeHospitalizedDoc().getFamilyLabel() != null) {
            FamilyLabel familyLabel = inputInfo.getBeHospitalizedDoc().getFamilyLabel();
            List<Family> families = familyLabel.getFamilies();
            if (ListUtil.isEmpty(families)) {
                status.set("0");
                return;
            }
            boolean isFindGeneticDiseaseKeyword = false, isFindDiagInfectious = false;
            for (Family family : families) {
                if (family.getGeneticDiseaseKeyword() != null && StringUtil.isNotBlank(family.getGeneticDiseaseKeyword().getName())) {
                    isFindGeneticDiseaseKeyword = true;
                }
                if (family.getDiagInfectious() != null && StringUtil.isNotBlank(family.getDiagInfectious().getName())) {
                    isFindDiagInfectious = true;
                }
                if (family.getDiags() != null) {
                    List<Diag> diags = family.getDiags().stream().filter(diag ->
                            (diag.getHospitalDiagName() != null && diag.getHospitalDiagName().contains("炎"))
                                    || (diag.getHospitalDiagName() != null && diag.getHospitalDiagName().contains("核"))).collect(Collectors.toList());
                    if (diags.size() > 0) {
                        isFindDiagInfectious = true;
                    }
                }
                if (isFindGeneticDiseaseKeyword || isFindDiagInfectious) {
                    status.set("0");
                    return;
                }
            }
        }
    }
}