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

import com.google.common.collect.Lists;
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.Family;
import com.lantone.qc.pub.model.label.FamilyLabel;
import com.lantone.qc.pub.model.label.MaritalLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 配偶死亡原因未描述
 * @author: rengb
 * @time: 2020/3/10 14:02
 */
@Component
public class BEH0378 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getBeHospitalizedDoc() == null) {
            return;
        }
        FamilyLabel familyLabel = inputInfo.getBeHospitalizedDoc().getFamilyLabel();
        MaritalLabel maritalLabel = inputInfo.getBeHospitalizedDoc().getMaritalLabel();

        if (familyLabel == null && maritalLabel == null) {
            return;
        }
        List<Family> familiesFl = null;
        List<Family> familiesMl = null;

        if (familyLabel != null) {
            familiesFl = familyLabel.getFamilies();
            //规则匹配
            String familyText = familyLabel.getText();
            if (StringUtil.isNotBlank(familyText) && familyText.contains("自然死亡")) {
                status.set("0");
                return;
            }
        }
        if (maritalLabel != null) {
            familiesMl = maritalLabel.getFamily();
        }
        List<Family> familyList = Lists.newArrayList();

        if (familiesFl != null && familiesFl.size() > 0) {
            familyList.addAll(familiesFl);
        }
        if (familiesMl != null && familiesMl.size() > 0) {
            familyList.addAll(familiesMl);
        }

        familyList = familyList
                .stream()
                .filter(
                        i -> i != null
                                && StringUtil.isNotBlank(i.getName())
                                && i.getName().contains("配偶")
                                && i.getDead() != null
                                && StringUtil.isNotBlank(i.getDead().getName())
                )
                .collect(Collectors.toList());

        if (familyList.size() > 0) {
            status.set("-1");
            long count = familyList
                    .stream()
                    .filter(
                            i -> (i.getDead().getDeadReason() != null && StringUtil.isNotBlank(i.getDead().getDeadReason().getName())) || StringUtil.isNotBlank(i.getDead().getUnknow().getName())
                    )
                    .count();
            if (count > 0) {
                status.set("0");
            }
        }
    }
}