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

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.DoctorAdviceDoc;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.util.DateUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : THR03090
 * @Description : 手术患者无术前主刀医师查房记录
 * @Author : wsy
 * @Date: 2021-01-11 10:39
 */
@Component
public class THR03090 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        List<ThreeLevelWardDoc> threeLevelWardDocs = inputInfo.getThreeLevelWardDocs();
        long cou = inputInfo.getOperationDocs().stream().map(OperationDoc::getOperationRecordDoc).filter(Objects::nonNull).count();
        if (cou == 0) {
            return;
        }
        if (threeLevelWardDocs.size() == 0 && cou > 0) {
            status.set("-1");
            return;
        }
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        if (operationDocs == null || operationDocs.size() == 0) {
            return;
        }
        String operationStartDate = "";
        if (operationDocs.get(operationDocs.size() - 1).getOperationRecordDoc() != null) {
            Map<String, String> operationDocStructureMap = operationDocs.get(operationDocs.size() - 1).getOperationRecordDoc().getStructureMap();
            operationStartDate = operationDocStructureMap.get("手术日期");
            if (StringUtil.isNotBlank(operationStartDate)) {
                if (operationStartDate.contains("结束")) {
                    operationStartDate = operationStartDate.substring(0, operationStartDate.indexOf("结束")).replace("开始:", "");
                }
                if (operationStartDate.contains("年月日")) {
                    operationStartDate = DateUtil.nowString();
                }
                //取不到时间不判断规则
                if (StringUtil.parseDateTime(operationStartDate) == null) {
                    return;
                }
            }
        }

        List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDocs.get(0).getAllDoctorWradDocs();
        for (ThreeLevelWardDoc threeLevelWard : allDoctorWradDocs) {
            Map<String, String> structureMap = threeLevelWard.getStructureMap();
            String makeTitle = structureMap.get("查房标题");
            String writTitle = structureMap.get("文书标题");
            String makeDate = structureMap.get("查房日期");
            if (StringUtil.isNotBlank(makeDate) && StringUtil.parseDateTime(makeDate).before(StringUtil.parseDateTime(operationStartDate))) {
                if (((StringUtil.isNotBlank(makeTitle) && makeTitle.contains("主刀")) || (StringUtil.isNotBlank(writTitle) && writTitle.contains("主刀"))
                        || ((StringUtil.isNotBlank(makeTitle) && makeTitle.contains("术前")) || (StringUtil.isNotBlank(writTitle) && writTitle.contains("术前"))))) {
                    return;
                }
            }
        }

        //判断医嘱里有无手术
        List<DoctorAdviceDoc> doctorAdviceDocs = inputInfo.getDoctorAdviceDocs();
        if (doctorAdviceDocs.size() == 0) {
            return;
        }
        for (DoctorAdviceDoc dad : doctorAdviceDocs) {
            String name = dad.getStructureMap().get("医嘱项目名称");
            if (name.contains("非手术") || name.contains("手术室") || (name.contains("手术") && name.contains("取消")) || (name.contains("暂停") && name.contains("手术")) || name.contains("静脉穿刺置管术") || name.startsWith("停") || name.contains("前一次")
                    || name.contains("特殊病人手术使用一次性卫生材料") || name.contains("人免疫缺陷病毒抗体检测免费")) {
                continue;
            }
            if (name.contains("手术")) {
                status.set("-1");
                return;
            }
        }

        if (cou > 0) {
            status.set("-1");
            return;
        }
    }
}
