package com.ruoyi.web.work.hl7.service.impl;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.message.ADT_A01;
import ca.uhn.hl7v2.model.v24.message.ADT_A03;
import ca.uhn.hl7v2.model.v24.message.ORU_R01;
import ca.uhn.hl7v2.model.v24.segment.PV1;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.work.domain.RwaMessage;
import com.ruoyi.web.work.hl7.service.HL7RwaMessageService;
import com.ruoyi.web.work.mapper.RwaMessageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 原始数据
 */
@Service
@Transactional
public class HL7RwaMessageServiceImpl extends ServiceImpl<RwaMessageMapper, RwaMessage> implements HL7RwaMessageService {

    @Override
    public void saveMessage(Map map, Message message) {
        String cardId = null;
        if (message instanceof ADT_A03){
            ADT_A03 a03 = (ADT_A03) message;
            cardId = a03.getPV1().getVisitNumber().getID().getValue(); //就诊卡号
        }
        if (message instanceof ADT_A01){
            ADT_A01 a01 = (ADT_A01) message;
            cardId = a01.getPV1().getVisitNumber().getID().getValue(); //就诊卡号
        }
        if (message instanceof ORU_R01){
            ORU_R01 oruR01 = (ORU_R01) message;
            PV1 pv1 = oruR01.getPATIENT_RESULT().getPATIENT().getVISIT().getPV1();
            cardId = pv1.getVisitNumber().getID().getValue(); //就诊卡号
        }
        String messageID= map.get("/MSH-10").toString();
        String messageType = message.getName();
        String SendIP = map.get("SENDING_IP").toString();
        String SendPort = map.get("SENDING_PORT").toString();
        String rawMessage = map.get("raw-message").toString();
        RwaMessage rwaMessage = new RwaMessage();
        rwaMessage.setCardId(Long.valueOf(cardId));
        rwaMessage.setMessageId(messageID);
        rwaMessage.setMessageType(messageType);
        rwaMessage.setRawMessage(rawMessage);
        rwaMessage.setSendPort(SendPort);
        rwaMessage.setSendIp(SendIP);
        baseMapper.insert(rwaMessage);
    }
}
