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

import com.lantone.qc.kernel.catalogue.QCCatalogue;
import com.lantone.qc.kernel.util.CatalogueUtil;
import com.lantone.qc.pub.Content;
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.ThreeLevelWardDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @ClassName : FIRP0195
 * @Description :离院方式选择错误
 * @Author : 胡敬
 * @Date: 2020-06-03 16:54
 */
@Component
public class FIRP0195 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
//        status.set("0");
        DeathRecordDoc deathRecordDoc = inputInfo.getDeathRecordDoc();
        DeathCaseDiscussDoc deathCaseDiscussDoc = inputInfo.getDeathCaseDiscussDoc();
        if (inputInfo.getFirstPageRecordDoc() == null) {
            status.set("0");
            return;
        }
        Map<String, String> firstpageStructureMap = inputInfo.getFirstPageRecordDoc().getStructureMap();
        String outWay = firstpageStructureMap.get(Content.outWay);
        if (StringUtil.isBlank(outWay)) {
            status.set("0");
            return;
        }
        String wardOutWay;
        if (deathRecordDoc != null || deathCaseDiscussDoc != null) {
            wardOutWay = "死亡";
            if (match(outWay, wardOutWay)) {
                status.set("0");
                return;
            }
        }
        if(inputInfo.getThreeLevelWardDocs().size() == 0){
            status.set("0");
            return;
        }

        List<ThreeLevelWardDoc> allDoctorWradDocs = inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs();
        if (allDoctorWradDocs.size() == 0) {
            status.set("0");
            return;
        }
        int j = 0; //最后两次查房记录
        for (int i = allDoctorWradDocs.size() - 1; i >= 0 && j < 2; i--) {
            j++;
            ThreeLevelWardDoc lastWardDoc = allDoctorWradDocs.get(i);
            Map<String, String> structureMap = lastWardDoc.getStructureMap();
            String conditionRecord = structureMap.get("病情记录");
            String treatmentPlan = structureMap.get("治疗计划和措施");
            String title = structureMap.get("查房标题");
            if (title != null && title.contains("病理报告")) {
                continue;
            }
            conditionRecord = StringUtil.isBlank(conditionRecord) ? "" : conditionRecord;
            treatmentPlan = StringUtil.isBlank(treatmentPlan) ? "" : treatmentPlan;
            if ((conditionRecord.contains("出院") || treatmentPlan.contains("出院"))
                    && outWay.equals("医嘱离院")) {
                status.set("0");
                return;
            }
            if ((leave_method(conditionRecord) || leave_method(treatmentPlan)) && outWay.equals("医嘱转院")) {
                status.set("0");
                return;
            }
        }

    }

    private boolean match(String str1, String str2) {
        return str1.equals(str2) || str1.contains(str2) || str2.contains(str1);
    }

    public boolean leave_method(String content){
        String compile = "(?<=转)(.+)(?=院)";
        Pattern p = Pattern.compile(compile);
        boolean b = p.matcher(content).find();
        return b;
    }
}