package com.lantone.qc.kernel.catalogue.leavehospital;

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.DeathCaseDiscussDoc;
import com.lantone.qc.pub.model.doc.DeathRecordDoc;
import com.lantone.qc.pub.model.doc.DoctorAdviceDoc;
import com.lantone.qc.pub.model.doc.LeaveHospitalDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName : LEA02987
 * @Description : 出院带药与医嘱不一致
 * @Author : 胡敬
 * @Date: 2020-06-23 10:54
 */
@Component
public class LEA02987 extends QCCatalogue {

    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        List<DoctorAdviceDoc> doctorAdviceDocs = inputInfo.getDoctorAdviceDocs();
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        if (doctorAdviceDocs.size() == 0 || leaveHospitalDoc == null) {
            return;
        }
        DeathRecordDoc deathRecordDoc = inputInfo.getDeathRecordDoc();
        DeathCaseDiscussDoc deathCaseDiscussDoc = inputInfo.getDeathCaseDiscussDoc();
        if (deathRecordDoc != null || deathCaseDiscussDoc != null) {
            return;
        }
        Map<String, String> structureMap = leaveHospitalDoc.getStructureMap();
        String dischargeOrder = structureMap.get("出院医嘱");
        if (StringUtil.isBlank(dischargeOrder)) {
            return;
        }
        Set<String> drugs = new HashSet<>();
        for (DoctorAdviceDoc adviceDoc : doctorAdviceDocs) {
            Map<String, String> adviceDocStructureMap = adviceDoc.getStructureMap();
            String name = adviceDocStructureMap.get("医嘱项目名称");
            String type = adviceDocStructureMap.get("医嘱类型判别");
            if (StringUtil.isNotBlank(type) && type.equals("出院带药")) {
                if (StringUtil.isNotBlank(name)) {
                    drugs.add(name);
                }
            }
        }

        String infoStr = "";
        List<String> notContainsDrugs = new ArrayList<>();
        for (String drug:drugs) {
            if (!dischargeOrder.contains(drug)){
                notContainsDrugs.add(drug);
            }
        }

        //没有包含的药品再次查看商品名或化学名在出院医嘱书写
        for (String drug : notContainsDrugs) {
            Set<String> splitDrugs = CatalogueUtil.getRegexWords(drug, "[(（\\[][^\\[\\]()（）]+[\\]）)]");
            boolean isFind = false;
            for (String sd : splitDrugs) {
                if (dischargeOrder.contains(sd)){
                    isFind = true;
                }
            }
            if (!isFind) {
                infoStr = CatalogueUtil.concatInfo(infoStr, drug);
            }
        }
        if (StringUtil.isNotBlank(infoStr)){
            status.set("-1");
            info.set(infoStr);
        }
    }

}