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

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.LeaveHospitalDoc;
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 : THR0144
 * @Description : 患者出院前无上级医师(主治及以上)同意出院的病程记录
 * @Author : 胡敬
 * @Date: 2020-03-19 17:20
 */
@Component
public class THR0144 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getThreeLevelWardDocs().size() == 0) {
            status.set("0");
            return;
        }
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        DeathRecordDoc deathRecordDoc = inputInfo.getDeathRecordDoc();
        DeathCaseDiscussDoc deathCaseDiscussDoc = inputInfo.getDeathCaseDiscussDoc();
        /* 如果没有出院小结，就不报         如果有死亡记录、死亡讨论记录，也不报 */
        if (leaveHospitalDoc == null || deathRecordDoc != null || deathCaseDiscussDoc != null) {
            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("查房标题");
            String ob = structureMap.get("记录医师个人职称");
            if ( StringUtils.isNotBlank(title) && title.contains("病理报告")) {
                continue;
            }
            conditionRecord = StringUtil.isBlank(conditionRecord) ? "" : conditionRecord;
            treatmentPlan = StringUtil.isBlank(treatmentPlan) ? "" : treatmentPlan;
            //主任或主治查房 标题需要包含“主任”或“主治”，内容需要包含“出院”
            if (StringUtils.isNotBlank(title) && (conditionRecord.contains("出院") || treatmentPlan.contains("出院") || leave_method(treatmentPlan))
                    && (CatalogueUtil.subTitle(title).contains(Content.attend)
                    || CatalogueUtil.subTitle(title).contains(Content.director)
                    || CatalogueUtil.subTitle(title).contains("主刀"))) {
                status.set("0");
                return;
            }
            //treatmentPlan里包含出院或转院，记录医师职称为A/F
            if(StringUtils.isNotBlank(ob) && (conditionRecord.contains("出院") || treatmentPlan.contains("出院") || leave_method(treatmentPlan)
                    || leave_method(conditionRecord))
            && (ob.equals("A") || ob.equals("F"))){
                status.set("0");
                return;
            }
            //treatmentPlan前20个字包含A或F，并且包含出院或转院
            boolean treat_boolean = false;
            if(treatmentPlan.length()>10){
                String sub_treat = treatmentPlan.substring(0, 10);
                if(sub_treat.contains("A") || sub_treat.contains("F")){
                    treat_boolean = true;
                }
            }
            boolean record_boolean = false;
            if(conditionRecord.length()>10){
                String sub_treat = conditionRecord.substring(0, 10);
                if(sub_treat.contains("A") || sub_treat.contains("F")){
                    record_boolean = true;
                }
            }
            if((conditionRecord.contains("出院") || treatmentPlan.contains("出院") || leave_method(treatmentPlan))
            && (record_boolean || treat_boolean)){
                status.set("0");
                return;
            }
            //普通查房 内容需要包含“上级”和“出院”
            if (StringUtil.isBlank(title)
                    || (title.contains("日常查房记录") || title.contains("普通查房记录") || title.contains("日常病程记录"))
                    && conditionRecord.contains("上级")
                    && (conditionRecord.contains("出院") || treatmentPlan.contains("出院"))) {
                status.set("0");
                return;
            }
        }
    }

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