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

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

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

/**
 * @ClassName : THR0126
 * @Description : 主治医师首次查房未在48小时内完成
 * @Author : 胡敬
 * @Date: 2020-03-19 15:50
 */
@Component
public class THR0126 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        if (inputInfo.getThreeLevelWardDocs().size() == 0) {
            status.set("0");
            return;
        }
        if (inputInfo.getLeaveHospitalDoc() != null && inputInfo.getLeaveHospitalDoc().getStructureMap() != null
                && inputInfo.getThreeLevelWardDocs().size() > 0) {
            Map<String, String> getLeaveHospitalStructureMap = inputInfo.getLeaveHospitalDoc().getStructureMap();
            String admisTime = getLeaveHospitalStructureMap.get(Content.admisTime);
            String dischargeTime = getLeaveHospitalStructureMap.get(Content.dischargeTime);
            if (CatalogueUtil.isEmpty(admisTime) || CatalogueUtil.isEmpty(dischargeTime)) {
                status.set("0");
                return;
            }
            //如果住院天数小于2天则不判断该条规则
            if (!CatalogueUtil.compareTime(StringUtil.parseDateTime(admisTime), StringUtil.parseDateTime(dischargeTime), (long) (48 * 60))) {
                status.set("0");
                return;
            }
        }
        if (inputInfo.getBeHospitalizedDoc() != null) {
            Map<String, String> beHospitalStructureMap = inputInfo.getBeHospitalizedDoc().getStructureMap();
            String admisTime = beHospitalStructureMap.get("入院日期");
            if (CatalogueUtil.isEmpty(admisTime)) {
                return;
            }
            //开始时间(入院时间)
            Date beginDate = StringUtil.parseDateTime(admisTime);
            if (beginDate == null) {
                return;
            }
            /* 首次病程录如果是主治或者主任写的，也算主治查房 */
            FirstCourseRecordDoc firstCourseRecordDoc = inputInfo.getFirstCourseRecordDoc();
            if (firstCourseRecordDoc != null) {
                String firstCourseProfessor = getFirstCourseProfessor(firstCourseRecordDoc);
                if (firstCourseProfessor.contains("主治") || firstCourseProfessor.contains("主任")) {
                    String recordDateStr = firstCourseRecordDoc.getStructureMap().get("记录时间");
                    if (StringUtil.isNotBlank(recordDateStr)) {
                        if (!CatalogueUtil.compareTime(StringUtil.parseDateTime(admisTime), StringUtil.parseDateTime(recordDateStr), 48 * 60L)) {
                            status.set("0");
                        }
                    }
                }
            }
            ThreeLevelWardDoc threeLevelWardDoc = inputInfo.getThreeLevelWardDocs().get(0);
            List<AttendingDoctorWardDoc> attendingDoctorWardDocs = threeLevelWardDoc.getAttendingDoctorWardDocs();
            if (attendingDoctorWardDocs.size() > 0) {
                AttendingDoctorWardDoc firstAttending = attendingDoctorWardDocs.get(0);
                String wardDateStr = firstAttending.getStructureMap().get("查房日期");
                if (!CatalogueUtil.compareTime(StringUtil.parseDateTime(admisTime), StringUtil.parseDateTime(wardDateStr), 48 * 60L)) {
                    status.set("0");
                }
            }
        }
    }

    private String getFirstCourseProfessor(FirstCourseRecordDoc firstCourseRecordDoc) {
        String professor = "";
        String doctorSign = firstCourseRecordDoc.getStructureMap().get("记录医师");
        if (StringUtil.isBlank(doctorSign)) {
            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(doctorSign);
            if (doctor != null) {
                professor = doctor.get("professor");
            }

        }
        return professor;
    }
}