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.Operation;
import com.lantone.qc.pub.model.label.PastLabel;
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;

/**
 * @Description: 手术史未填写
 * @author: rengb
 * @time: 2020/3/10 14:02
 */
@Component
public class BEH0025 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }
        //台州结构化
        Map<String, String> behStructureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
        if (StringUtils.isNotEmpty(behStructureMap.get("手术外伤史"))) {
            status.set("0");
            return;
        }
        PastLabel pastLabel = inputInfo.getBeHospitalizedDoc().getPastLabel();
        /* 如果既往史为空或者既往史文本为空，则不报错 */
        if (pastLabel == null || StringUtil.isBlank(pastLabel.getText())) {
            status.set("0");
            return;
        }
        List<Operation> operations = pastLabel.getOperations();
        if (ListUtil.isNotEmpty(operations)) {
            if (operations.stream().map(i -> i.getName()).filter(i -> StringUtil.isNotBlank(i)).count() > 0) {
                status.set("0");
                return;
            }
        }
        /* 疾病名称：**术后**，算是有手术史 */
        List<Diag> diags = pastLabel.getDiags();
        for (Diag diag : diags) {
            String hospitalDiagName = diag.getHospitalDiagName();
            if (StringUtil.isBlank(hospitalDiagName)) {
                continue;
            }
            if (hospitalDiagName.contains("术后")) {
                status.set("0");
                return;
            }
        }
        //规则硬匹配
        String pastLabelText = pastLabel.getText();
        if (pastLabelText.contains("手术") || pastLabelText.contains("详见原病历")
                || pastLabelText.contains("体外碎石")) {
            status.set("0");
        }
    }

}