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.PD;
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 : BEH02902
 * @Description : 辅助检查未注明地点
 * @Author : 胡敬
 * @Date: 2020-05-28 09:56
 */
@Component
public class BEH02902 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;
        }
        boolean findPlace = false;
        String pacsText = pacsLabel.getText();
        List<Pacs> pacses = pacsLabel.getPacses();
        for (Pacs pacs : pacses) {
            /* 时间为入院检查的时间，模型输出处理后结构为 name为空，pd不为空 */
            if (pacs.getName() == null && pacs.getPd() != null) {
                /* 模型先找到时间实体，找到后再从这个时间实体开始，往前往后找 */
                PD pd = pacs.getPd();
                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("院")) {
                    findPlace = true;
                    break;
                }
            }
        }

        /* 如果模型没找到时间实体，则直接取辅助检查前20个字，判断有没有院字 */
        if (!findPlace) {
            String prefixText = pacsText.substring(0, Math.min(20, pacsText.length()));
            if (prefixText.contains("院")) {
                findPlace = true;
            }
        }

        if (findPlace) {
            status.set("0");
        }
    }
}