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

import java.util.List;


/**
 * @ClassName : BEH0491
 * @Description : 辅助检查未注明时间和地点
 * @Author : 胡敬
 * @Date: 2020-06-03 13:34
 */
@Component
public class BEH0491 extends QCCatalogue {
    @Override
    protected void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getBeHospitalizedDoc() == null) {
            status.set("0");
            return;
        }

        PacsLabel pacsLabel = inputInfo.getBeHospitalizedDoc().getPacsLabel();
        if (pacsLabel == null || StringUtil.isBlank(pacsLabel.getText())) {
            status.set("0");
            return;
        }

        String pacsText = pacsLabel.getText();
        List<Pacs> pacses = pacsLabel.getPacses();
        boolean findTime = false, findPlace = false;
        for (Pacs pacs : pacses) {
            /* 时间为入院检查的时间，模型输出处理后结构为 name为空，pd不为空 */
            if (pacs.getName() == null && pacs.getPd() != null) {
                findTime = true;
                break;
            }
            /*
            else {
                PD pd = pacs.getPd();
                if (pd == null) {
                    continue;
                }
                String name = pd.getName();
                int timeIndex = pacsText.indexOf(name);
                String beforeTimeText = pacsText.substring(Math.max(timeIndex - 5, 0), timeIndex);
                String afterTimeText = pacsText.substring(timeIndex + name.length(), timeIndex + name.length() + 10);
                if (!beforeTimeText.contains("院") && !afterTimeText.contains("院")) {
                    status.set("-1");
                    return;
                }
            }
             */
        }
        String prefixText = pacsText.substring(0, Math.min(20, pacsText.length()));
        if (prefixText.contains("院")) {
            findPlace = true;
        }
        //内容硬匹配
        if (prefixText.contains("缺") || prefixText.contains("未查") || prefixText.contains("无")) {
            findPlace = true;
        }
        if (findTime || findPlace) {
            status.set("0");
        }
    }
}