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

import com.lantone.qc.dbanaly.util.KernelConstants;
import com.lantone.qc.dbanaly.util.SpecialStorageUtil;
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.MedicalRecordInfoDoc;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.model.doc.operation.OperationDoc;
import com.lantone.qc.pub.model.doc.ward.AttendingDoctorWardDoc;
import com.lantone.qc.pub.util.DateUtil;
import com.lantone.qc.pub.util.SpringContextUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName : THR0601
 * @Description : 整份病历无主治医师查房记录
 * @Author : 胡敬
 * @Date: 2020-03-25 10:21
 */
@Component
public class THR0601 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        MedicalRecordInfoDoc medicalRecordInfoDoc = inputInfo.getMedicalRecordInfoDoc();
        if (medicalRecordInfoDoc != null && medicalRecordInfoDoc.getStructureMap() != null) {
            //入院日期
            String admisTime = medicalRecordInfoDoc.getStructureMap().get("behospitalDate");
            //出院日期
            String dischargeTime = medicalRecordInfoDoc.getStructureMap().get("leaveHospitalDate");
            if (CatalogueUtil.isEmpty(admisTime) || CatalogueUtil.isEmpty(dischargeTime)) {
                return;
            }
            if (!CatalogueUtil.compareTime(
                    StringUtil.parseDateTime(admisTime),
                    StringUtil.parseDateTime(DateUtil.nowString()),
                    Long.valueOf(48 * 60))) {//如果入院未超过48小时，规则不判断
                return;
            }
            //如果住院天数小于2天则不判断该条规则
            if (DateUtil.parseDate(dischargeTime) != null &&
                    !CatalogueUtil.compareTime(StringUtil.parseDateTime(admisTime), StringUtil.parseDateTime(dischargeTime), (long) (48 * 60))) {
                return;
            } else {
                if (inputInfo.getThreeLevelWardDocs().size() == 0) {
                    status.set("-1");
                    return;
                }
            }
        }

        if (inputInfo.getLeaveHospitalDoc() != null) {
            Map<String, String> leaveHospitalStructureMap = inputInfo.getLeaveHospitalDoc().getStructureMap();
            String lengthOfStay = leaveHospitalStructureMap.get("住院天数");
            if (StringUtil.isNotBlank(lengthOfStay) && CatalogueUtil.numbersOnly(lengthOfStay)) {
                //如果住院天数小于2天则不判断该条规则
                if (Integer.parseInt(lengthOfStay) <= 2) {
                    return;
                }
            }
        }

        /* 如果存在手术记录，判断主刀医生是否为主治医生 */
        String operatorName = "";
        List<OperationDoc> operationDocs = inputInfo.getOperationDocs();
        if (operationDocs != null) {
            for (OperationDoc operationDoc : operationDocs) {
                if (operationDoc.getOperationRecordDoc() != null) {
                    Map<String, String> operationDocStructureMap = operationDoc.getOperationRecordDoc().getStructureMap();
                    if (StringUtil.isBlank(operatorName)) {
                        operatorName = operationDocStructureMap.get("主刀医师");
                    }
                }
            }
        }

        List<AttendingDoctorWardDoc> attendingDoctorWardDocs = inputInfo.getThreeLevelWardDocs().get(0).getAttendingDoctorWardDocs();//主治查房记录
        if (attendingDoctorWardDocs != null && attendingDoctorWardDocs.size() > 0) {
            return;
        }

        List<ThreeLevelWardDoc> allDoctorWradDocs = inputInfo.getThreeLevelWardDocs().get(0).getAllDoctorWradDocs();//查房记录
        String title, record;
        boolean findIndications = false;
        for (ThreeLevelWardDoc threeLevelWardDoc : allDoctorWradDocs) {
            Map<String, String> rescueStructureMap = threeLevelWardDoc.getStructureMap();
            title = CatalogueUtil.subTitle(rescueStructureMap.get("查房标题"));
            record = CatalogueUtil.subTitle(rescueStructureMap.get("病情记录"));
            if (StringUtil.isNotBlank(title) && title.contains(Content.attend)) {
                findIndications = true;
                break;
            }
            if (StringUtil.isNotBlank(record) && record.contains(Content.attend)) {
                findIndications = true;
                break;
            }
            if (StringUtil.isNotBlank(operatorName) && (title.contains("主刀") || record.contains("主刀"))) {
                String operationProfessor = getCourseProfessor(operatorName);
                if (operationProfessor.contains("主治")) {
                    findIndications = true;
                    break;
                }
            }
        }
        if (!findIndications) {
            status.set("-1");
        }
    }

    private String getCourseProfessor(String operatorName) {
        String professor = "";
        if (StringUtil.isBlank(operatorName)) {
            return professor;
        }
        SpecialStorageUtil specialStorageUtil = SpringContextUtil.getBean("specialStorageUtil");
        Map<String, Object> surgeon = specialStorageUtil.getJsonStringValue(KernelConstants.HOSPITAL_DOCTOR_MAP);
        if (surgeon != null) {
            Map<String, String> doctor = (Map) surgeon.get(operatorName);
            if (doctor != null) {
                professor = doctor.get("professor");
            }

        }
        return professor;
    }
}