package com.lantone.qc.kernel.catalogue.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.model.InputInfo;
import com.lantone.qc.pub.model.OutputInfo;
import com.lantone.qc.pub.model.doc.ThreeLevelWardDoc;
import com.lantone.qc.pub.model.doc.ward.DirectorDoctorWardDoc;
import com.lantone.qc.pub.util.SpringContextUtil;
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;

/**
 * @ClassName : THR03015
 * @Description : 主任查房记录无审核  1/次（超过一周）
 * @Author : 胡敬
 * @Date: 2020-03-25 10:21
 */
@Component
public class THR03015 extends QCCatalogue {

    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        status.set("0");
        if (inputInfo.getLeaveHospitalDoc() == null || inputInfo.getThreeLevelWardDocs().size() == 0) {
            return;
        }
        Map<String, String> leaveHospitalStructureMap = inputInfo.getLeaveHospitalDoc().getStructureMap();
        String lengthOfStay = leaveHospitalStructureMap.get("住院天数");
        if (StringUtil.isNotBlank(lengthOfStay) && CatalogueUtil.numbersOnly(lengthOfStay)) {
            //如果住院天数小于7天则不判断该条规则
            if (Integer.parseInt(lengthOfStay) <= 7) {
                return;
            }
        }

        boolean findIndications = true;
        ThreeLevelWardDoc threeLevelWardDoc = inputInfo.getThreeLevelWardDocs().get(0);
        List<DirectorDoctorWardDoc> directorDoctorWardDocs = threeLevelWardDoc.getDirectorDoctorWardDocs();
        String jlDateStr, shDateStr, professor;
        for (DirectorDoctorWardDoc directorDoctorWardDoc : directorDoctorWardDocs) {
            jlDateStr = directorDoctorWardDoc.getStructureMap().get("记录时间");
            shDateStr = directorDoctorWardDoc.getStructureMap().get("审核日期");
            if(StringUtils.isBlank(shDateStr)){
                continue;
            }
            professor = getProfessor(directorDoctorWardDoc.getStructureMap().get("审核人"));
            if (StringUtil.isBlank(professor)
//                    || !professor.contains("主任")
                    || CatalogueUtil.compareTime(StringUtil.parseDateTime(jlDateStr), StringUtil.parseDateTime(shDateStr), 7 * 24 * 60L)) {
                findIndications = false;
                break;
            }
        }
        if (!findIndications) {
            status.set("-1");
        }
    }

    private String getProfessor(String doctorSign) {
        String professor = "";
        if (StringUtil.isBlank(doctorSign)) {
            return professor;
        }
        SpecialStorageUtil specialStorageUtil = SpringContextUtil.getBean("specialStorageUtil");
        Map<String, Map<String, String>> 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;
    }

}