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.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName : BEH0496
 * @Description :  待查诊断未列出可能性较大诊断
 * @Author : Mark
 * @Date: 2020-04-05 13:33
 */
@Component
public class BEH0496 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getBeHospitalizedDoc() == null || inputInfo.getBeHospitalizedDoc().getInitialDiagLabel() == null) {
            return;
        }

        List<Diag> diags = inputInfo.getBeHospitalizedDoc().getInitialDiagLabel().getDiags();
        String initDiagText = inputInfo.getBeHospitalizedDoc().getInitialDiagLabel().getText();
        if (ListUtil.isEmpty(diags) || StringUtil.isBlank(initDiagText)) {
            return;
        }
        String todo = "待查";
        int startWordIndex = 0, endWordIndex = 0;
        for (int i = 0; i < diags.size(); i++) {
            String diagName = getDiagName(diags.get(i));
            if (diagName.contains(todo)) {
                /* 如果诊断待查是最后一个，说明之后肯定没有可能性较大诊断，则报错 */
                if (i == diags.size() - 1) {
                    status.set("-1");
                    return;
                }
                startWordIndex = initDiagText.indexOf(diagName);
                endWordIndex = initDiagText.indexOf(getDiagName(diags.get(i + 1)));
                break;
            }
        }

        String midText = initDiagText.substring(startWordIndex, endWordIndex);
        if (midText.contains("?") || midText.contains("？")) {
            status.set("-1");
        }
    }

    private String getDiagName(Diag diag) {
        String hospitalDiagName = diag.getHospitalDiagName();
        String name = diag.getName();
        return StringUtil.isBlank(hospitalDiagName) ? name : hospitalDiagName;
    }
}