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

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.BeHospitalizedDoc;
import com.lantone.qc.pub.model.doc.FirstCourseRecordDoc;
import com.lantone.qc.pub.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName : FIRC0093
 * @Description :  首次病程未在患者入院8小时内完成
 * @Author : 楼辉荣
 * @Date: 2020-03-06 17:28
 */
@Component
public class FIRC0093 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
        status.set("0");
        BeHospitalizedDoc beHospitalizedDoc = inputInfo.getBeHospitalizedDoc();
        FirstCourseRecordDoc firstCourseRecordDoc = inputInfo.getFirstCourseRecordDoc();
        if (firstCourseRecordDoc != null && beHospitalizedDoc != null) {
            Map<String, String> courseRecordDocStructureMap = firstCourseRecordDoc.getStructureMap();
            Map<String, String> beHospitalizedDocStructureMap = beHospitalizedDoc.getStructureMap();
            if (courseRecordDocStructureMap != null && beHospitalizedDocStructureMap != null) {
                String couDate = courseRecordDocStructureMap.get("记录时间");
                String beDate = beHospitalizedDocStructureMap.get("入院日期");
                if (StringUtils.isNotEmpty(couDate) && StringUtils.isNotEmpty(beDate)) {
                    Date date_in = StringUtil.parseDateTime(beDate);
                    Date date_out = StringUtil.parseDateTime(couDate);
                    if (date_in == null || date_out == null) {
                        return;
                    }
                    boolean firc = CatalogueUtil.compareTime(date_in, date_out, 8 * 60L);
                    if (firc) {
                        status.set("-1");
                    }
                }
            }
        } else {
            if (beHospitalizedDoc != null) {
                Map<String, String> beHospitalizedDocStructureMap = beHospitalizedDoc.getStructureMap();
                if (beHospitalizedDocStructureMap != null) {
                    String beDate = beHospitalizedDocStructureMap.get("入院日期");
                    if (StringUtils.isNotEmpty(beDate)) {
                        Date date_in = StringUtil.parseDateTime(beDate);
                        Date date_out = new Date();
                        if (date_in == null) {
                            return;
                        }
                        boolean firc = CatalogueUtil.compareTime(date_in, date_out, 8 * 60L);
                        if (firc) {
                            status.set("-1");
                        }
                    }
                }
            }
        }
    }
}