package com.lantone.qc.kernel.catalogue.beilun.leavehospital;

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.LeaveHospitalDoc;
import com.lantone.qc.pub.util.DateUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 出院记录未在患者出院前完成
 * @author: rengb
 * @time: 2020/3/10 13:53
 */
@Component
public class LEA0148 extends QCCatalogue {
    @Override
    public void start(InputInfo inputInfo, OutputInfo outputInfo) {
        LeaveHospitalDoc leaveHospitalDoc = inputInfo.getLeaveHospitalDoc();
        if (leaveHospitalDoc == null) {
            status.set("0");
            return;
        }
        if (inputInfo.getDeathRecordDoc() != null || inputInfo.getDeathCaseDiscussDoc() != null) {
            status.set("0");
            return;
        }
        String timeCy = null;
        if (inputInfo.getMedicalRecordInfoDoc() != null) {
            Map<String, String> medicalRecordInfoStructureMap = inputInfo.getMedicalRecordInfoDoc().getStructureMap();
            timeCy = medicalRecordInfoStructureMap.get("leaveHospitalDate");
        }
        String timeQm = leaveHospitalDoc.getStructureMap().get("记录时间");
        if (StringUtil.isNotBlank(timeQm) && DateUtil.parseDate(timeQm) != null && StringUtil.isNotBlank(timeCy)) {
            if (!CatalogueUtil.compareTime(
                    StringUtil.parseDateTime(timeCy),
                    StringUtil.parseDateTime(DateUtil.nowString()),
                    Long.valueOf(24 * 60))) {//如果出院未超过24小时，规则不判断
                status.set("0");
                return;
            }
            Date timeQmDate = StringUtil.parseDateTime(timeQm);
            Date timeCyDate = StringUtil.parseDateTime(timeCy);
            if (!CatalogueUtil.compareTime(timeCyDate, timeQmDate, 24 * 60L)) {
                status.set("0");
            }
        } else {
            if (StringUtil.isNotEmpty(timeCy)) {
                if (!CatalogueUtil.compareTime(
                        StringUtil.parseDateTime(timeCy),
                        StringUtil.parseDateTime(DateUtil.nowString()),
                        Long.valueOf(24 * 60))) {//如果出院未超过24小时，规则不判断
                    status.set("0");
                    return;
                }
            }
            status.set("0");
            return;
        }
    }
}