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.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ClassName : BEH0073
 * @Description : 家族中是否有肝炎、结核等病史未描述
 * @Author : 胡敬
 * @Date: 2020-03-11 17:05
 */
@Component
public class BEH0073 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();
        if (StringUtils.isNotEmpty(structureMap.get("病毒性肝炎")) || StringUtils.isNotEmpty(structureMap.get("结核病"))) {
            status.set("0");
            return;
        }
        FamilyLabel familyLabel = inputInfo.getBeHospitalizedDoc().getFamilyLabel();
        if (familyLabel == null || StringUtil.isBlank(familyLabel.getText())) {
            status.set("0");
            return;
        }
        String familyText = familyLabel.getText();
        if (familyText.contains("肝炎") || familyText.contains("结核") || familyText.contains("详见原病历")) {
            status.set("0");
            return;
        }
        List<Family> families = familyLabel.getFamilies();
        if (ListUtil.isNotEmpty(families)) {
            for (Family family : families) {
                if (family.getDiags() == null || family.getDiags().size() == 0) {
                    continue;
                }
                for (Diag diag : family.getDiags()) {
                    String hospitalDiagName = diag.getHospitalDiagName();
                    String name = diag.getName();
                    if (findDiag(hospitalDiagName) || findDiag(name)) {
                        status.set("0");
                        return;
                    }
                }
            }
        }
    }

    private boolean findDiag(String diagName) {
        if (StringUtil.isBlank(diagName)) {
            return false;
        }
        return diagName.contains("肝炎") || diagName.contains("结核");
    }
}