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.DirectorDoctorWardDoc;
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 : THR0125
 * @Description : 副主任医师/主任医师查房首次查房记录未在患者入院72h内完成
 * @Author : 胡敬
 * @Date: 2020-03-19 13:51
 */
@Component
public class THR0125 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) {
            Map<String, String> leaveHospitalStructureMap = inputInfo.getLeaveHospitalDoc().getStructureMap();
            String admisTime = leaveHospitalStructureMap.get(Content.admisTime);
            String dischargeTime = leaveHospitalStructureMap.get(Content.dischargeTime);
            if (CatalogueUtil.isEmpty(admisTime) || CatalogueUtil.isEmpty(dischargeTime)) {
                status.set("0");
                return;
            }
            //如果住院天数小于3天则不判断该条规则
            if (!CatalogueUtil.compareTime(StringUtil.parseDateTime(admisTime), StringUtil.parseDateTime(dischargeTime), (long) (72 * 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;
            }
            ThreeLevelWardDoc threeLevelWardDoc = inputInfo.getThreeLevelWardDocs().get(0);
            List<DirectorDoctorWardDoc> directorDoctorWardDocs = threeLevelWardDoc.getDirectorDoctorWardDocs();
            if (directorDoctorWardDocs.size() > 0) {
                DirectorDoctorWardDoc firstDirector = directorDoctorWardDocs.get(0);
                String wardDateStr = firstDirector.getStructureMap().get("查房日期");
                if (!CatalogueUtil.compareTime(StringUtil.parseDateTime(admisTime), StringUtil.parseDateTime(wardDateStr), 72 * 60L)) {
                    status.set("0");
                }
            }
        }
    }
}