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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.pub.Content;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.BeHospitalizedDoc;
import com.lantone.qc.pub.model.entity.ConjugalRelation;
import com.lantone.qc.pub.model.label.MaritalLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description: 夫妻关系未描述
 * @author: rengb
 * @time: 2020/3/10 14:02
 */
@Component
public class BEH0056 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        Map<String, String> structureMap = beHospitalizedDoc.getStructureMap();
        if (StringUtils.isNotEmpty(structureMap.get("夫妻关系"))) {
            status.set("0");
            return;
        }
        //婚育史未填写，直接返回；
        MaritalLabel maritalLabel = beHospitalizedDoc.getMaritalLabel();
        if (maritalLabel == null || StringUtils.isEmpty(maritalLabel.getText())) {
            status.set("0");
            return;
        }
        //硬规则 匹配未婚
        String marry = structureMap.get(Content.marry);
        String text = maritalLabel.getText();
        if (StringUtil.isNotBlank(text)) {
            if (text.contains("未婚") || text.contains("离异") || text.contains("离婚") || text.contains("丧偶")
                    || "未婚".equals(marry) || text.contains("详见原病历")) {
                status.set("0");
                return;
            }
        }
        ConjugalRelation conjugalRelation = inputInfo.getBeHospitalizedDoc().getMaritalLabel().getConjugalRelation();
        if (conjugalRelation != null) {
            if (StringUtil.isNotBlank(conjugalRelation.getName())) {
                status.set("0");
                return;
            }
        }
        if (inputInfo.getBeHospitalizedDoc().getMaritalLabel().getFamily() != null) {
            long cont = inputInfo.getBeHospitalizedDoc().getMaritalLabel()
                    .getFamily().stream()
                    .filter(family -> family.getName().contains("配偶") && family.getDead() != null).count();
            if (cont > 0) {
                status.set("0");
                return;
            }
        }
    }

}