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.doc.BeHospitalizedDoc;
import com.lantone.qc.pub.model.label.ChiefLabel;
import com.lantone.qc.pub.model.label.PresentLabel;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName : BEH03050
 * @Description : 主诉与现病史发病部位不一致
 * @Author : 胡敬
 * @Date: 2020-07-28 09:52
 */
@Component
public class BEH03050 extends QCCatalogue {
    private List<String> positions = Lists.newArrayList("左上", "左下", "右上", "右下", "上", "下", "左", "右");
    private List<String> containList = Arrays.asList("检查", "术后", "药物", "发现", "误服", "确诊", "经", "异常", "诊断"
            , "示", "超", "伤", "术", "复查", "体检", "血透", "血液透析", "诱因下");

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getBeHospitalizedDoc() == null) {
            return;
        }

        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        ChiefLabel chiefLabel = beHospitalizedDoc.getChiefLabel();
        PresentLabel presentLabel = beHospitalizedDoc.getPresentLabel();
        if (chiefLabel == null || presentLabel == null) {
            return;
        }

        List<String> chiefPosition = new ArrayList<>();
        List<String> presentPosition = new ArrayList<>();


        String chiefText = chiefLabel.getText();
        String presentText = presentLabel.getText();
        if (StringUtil.isBlank(chiefText) || StringUtil.isBlank(presentText)) {
            return;
        }
        //主诉如果包括相关词，不报错
        for (String word : containList) {
            if (chiefText.contains(word)) {
                return;
            }
        }

        //取主诉的方位词
        for (String position : positions) {
            if (chiefText.contains(position)) {
                chiefPosition.add(position);
                chiefText = chiefText.replace(position, "");
            }
        }

        //取现病史的方位词,只取现病史前20个字
        if (presentText.length() > 20) {
            presentText = presentText.substring(0, 20);
        }
        for (String position : positions) {
            if (presentText.contains(position)) {
                presentPosition.add(position);
                presentText = presentText.replace(position, "");
            }
        }

        if (chiefPosition.size() == 0 || presentPosition.size() == 0) {
            return;
        }

        int matchNum = 0;
        for (String chiefP : chiefPosition) {
            for (String presentP : presentPosition) {
                if (chiefP.equals(presentP)) {
                    matchNum++;
                    break;
                }
            }
        }

        if (matchNum == 0) {
            status.set("-1");
        }
    }
}