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

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

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

/**
 * @ClassName : DEAR0561
 * @Description : 死亡记录未在患者离院后24小时内完成
 * @Author : 胡敬
 * @Date: 2020-03-18 18:38
 */
@Component
public class DEAR0561 extends QCCatalogue {
    public void start(InputInfo inputInfo, OutputInfo outputInfo) throws ParseException {
        status.set("0");
        if (inputInfo.getDeathRecordDoc() != null && inputInfo.getDeathRecordDoc().getStructureMap() != null) {
            Map<String, String> deathRecordStructureMap = inputInfo.getDeathRecordDoc().getStructureMap();
            if (!CatalogueUtil.isEmpty(deathRecordStructureMap.get("死亡时间"))) {
                String deathDate = deathRecordStructureMap.get("死亡时间");
                FirstPageRecordDoc firstPageRecordDoc = inputInfo.getFirstPageRecordDoc();
                if (firstPageRecordDoc != null && firstPageRecordDoc.getStructureMap() != null) {
                    Map<String, String> firstPageMap = firstPageRecordDoc.getStructureMap();
                    String outDate = firstPageMap.get("出院时间");
                    if (outDate != null && deathDate != null) {
                        Date date_in = StringUtil.parseDateTime(deathDate);
                        Date date_out = StringUtil.parseDateTime(outDate);
                        if (date_in == null || date_out == null) {
                            return;
                        }
                        int hours = (int) ((date_out.getTime() - date_in.getTime()) / (1000 * 3600));
                        if (hours > 24) {
                            status.set("-1");
                        }
                    }
                }
            }
        }
    }
}