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

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

/**
 * @ClassName : THR03021
 * @Description : 入院48小时内无Fellow以上的医生查房记录
 * 添加条件，如果记录医师为A，则不报
 * 添加条件，如果记录医师为F，则不报
 * @Author : 胡敬
 * @Date: 2020-03-19 15:50
 */
@Component
public class THR03021 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;
            }
            ThreeLevelWardDoc threeLevelWardDoc = inputInfo.getThreeLevelWardDocs().get(0);
            List<AttendingDoctorWardDoc> attendingDoctorWardDocs = threeLevelWardDoc.getAttendingDoctorWardDocs();
            List<DirectorDoctorWardDoc> directorDoctorWardDocs = threeLevelWardDoc.getDirectorDoctorWardDocs();
            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");
                    return;
                }
            }
            if (directorDoctorWardDocs.size() > 0) {
                DirectorDoctorWardDoc firstDirector = directorDoctorWardDocs.get(0);
                String wardDateStr = firstDirector.getStructureMap().get("查房日期");
                if (!CatalogueUtil.compareTime(StringUtil.parseDateTime(admisTime), StringUtil.parseDateTime(wardDateStr), 48 * 60L)) {
                    status.set("0");
                    return;
                }
            }

            List<ThreeLevelWardDoc> allDoctorWradDocs = threeLevelWardDoc.getAllDoctorWradDocs();
            if(allDoctorWradDocs.size() >0){
                for (ThreeLevelWardDoc tll:allDoctorWradDocs
                     ) {
                    String job = tll.getStructureMap().get("记录医师个人职称");
                    String decoDa = tll.getStructureMap().get("查房日期");
                    if (!CatalogueUtil.compareTime(StringUtil.parseDateTime(admisTime), StringUtil.parseDateTime(decoDa), 48 * 60L)
                    && (job.equals("A")|| job.equals("F"))) {
                        status.set("0");
                        return;
                    }
                }
            }

        } else {
            //没有出院小结，这条直接不报错
            status.set("0");
        }
    }

}