package com.ruoyi.web.work.hl7.hl7service;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.*;
import ca.uhn.hl7v2.protocol.ReceivingApplication;
import com.ruoyi.web.work.hl7.service.HL7PatientService;
import com.ruoyi.web.work.hl7.service.HL7RwaMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 接收到hl7消息，业务处理逻辑类
 */
@Component
@Slf4j(topic = "hl7message")
public class HL7Business implements ReceivingApplication {

    @Autowired
    HL7PatientService patientService;

    @Autowired
    HL7RwaMessageService rwaMessageService;


    @Override
    public Message processMessage(Message message, Map map) throws HL7Exception {
        log.info("hl7协议数据解析成功：\n{}", message.encode().replace("\r", System.lineSeparator()));
        Message response = null;
            try {
                //对原始数据进行保存
                rwaMessageService.saveMessage(map,message);
                //入库操作
                response = patientService.savePatientInfo(message);
                return response;  //响应ACK
            } catch (Exception e) {
                log.error("处理HL7消息时发生业务错误：" + e.getMessage());
                log.error(e.getMessage());
                e.printStackTrace();
            }
            return null;
    }

    @Override
    public boolean canProcess(Message message) {
        return true;
    }


}
