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.ThreeLevelWardDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @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;
        }
        DeathRecordDoc deathRecordDoc = inputInfo.getDeathRecordDoc();
        DeathCaseDiscussDoc deathCaseDiscussDoc = inputInfo.getDeathCaseDiscussDoc();
        if (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("查房标题");
            if (StringUtil.isBlank(title) || title.contains("病理报告")) {
                continue;
            }
            conditionRecord = StringUtil.isBlank(conditionRecord) ? "" : conditionRecord;
            treatmentPlan = StringUtil.isBlank(treatmentPlan) ? "" : treatmentPlan;
            //主任或主治查房 标题需要包含“主任”或“主治”，内容需要包含“出院”
            if ((conditionRecord.contains("出院") || treatmentPlan.contains("出院") || conditionRecord.contains("转上级医院"))
                    && (CatalogueUtil.subTitle(title).contains(Content.attend)
                    || CatalogueUtil.subTitle(title).contains(Content.director)
                    || CatalogueUtil.subTitle(title).contains("主刀"))) {
                status.set("0");
                return;
            }
            //普通查房 内容需要包含“上级”和“出院”
            if (StringUtil.isBlank(title)
                    || (title.contains("日常查房记录") || title.contains("普通查房记录") || title.contains("日常病程记录"))
                    && conditionRecord.contains("上级")
                    && (conditionRecord.contains("出院") || treatmentPlan.contains("出院"))) {
                status.set("0");
                return;
            }
        }
    }
}