package com.docus.server.report.converter.impl;

import com.docus.server.report.converter.XmlMessageConverter;
import com.docus.server.report.dto.MaternalInfantRelationshipDTO;
import com.docus.server.report.util.XmlUtil;
import org.w3c.dom.Node;

/**
 * 母婴关系接收信息转换
 *
 * @author wyb
 */
public class HIP1264ReceiveMessageConverter implements XmlMessageConverter {
    private final String message;

    public HIP1264ReceiveMessageConverter(String message) {
        this.message = message;
    }

    public MaternalInfantRelationshipDTO xml2dto() {
        XmlUtil xmlUtil = XmlUtil.of(this.message);
        Node messageIdNode = xmlUtil.getNode("/PRPA_HIP1264/id/@extension");
        Node senderNode = xmlUtil.getNode("/PRPA_HIP1264/sender/device/id/item/@extension");
        Node receiverNode = xmlUtil.getNode("/PRPA_HIP1264/receiver/device/id/item/@extension");
        Node babyIdNode = xmlUtil.getNode("/PRPA_HIP1264/controlActProcess/babyId/@value");
        Node babyHisIdNode = xmlUtil.getNode("/PRPA_HIP1264/controlActProcess/babyHisId/@value");
        Node momIdNode = xmlUtil.getNode("/PRPA_HIP1264/controlActProcess/patientId/@value");
        Node parentTimesNode = xmlUtil.getNode("/PRPA_HIP1264/controlActProcess/parentTimes/@value");
        Node parentHisIdNode = xmlUtil.getNode("/PRPA_HIP1264/controlActProcess/parentHisId/@value");
        Node hospCodeNode = xmlUtil.getNode("/PRPA_HIP1264/controlActProcess/hospInfo/@code");

        String messageId = getNodeValueOrDefault(messageIdNode, "");
        String sender = getNodeValueOrDefault(senderNode, "");
        String receiver = getNodeValueOrDefault(receiverNode, "");
        String babyId = getNodeValueOrDefault(babyIdNode, "");
        String babyHisId = getNodeValueOrDefault(babyHisIdNode, "");
        String momId = getNodeValueOrDefault(momIdNode, "");
        String parentTimes = getNodeValueOrDefault(parentTimesNode, "");
        String parentHisId = getNodeValueOrDefault(parentHisIdNode, "");
        String hospCode = getNodeValueOrDefault(hospCodeNode, "");

        MaternalInfantRelationshipDTO dto = new MaternalInfantRelationshipDTO();
        dto.setMsgId(messageId);
        dto.setSender(sender);
        dto.setReceiver(receiver);
        dto.setBabyId(babyId);
        dto.setBabyHisId(babyHisId);
        dto.setMomId(momId);
        dto.setParentTimes(parentTimes);
        dto.setParentHisId(parentHisId);
        dto.setHospCode(hospCode);
        return dto;
    }

    public String getMessage() {
        return message;
    }

    public static void main(String[] args) {
        String message = "<PRPA_HIP1264 xmlns=\"urn:hl7-org:v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ITSVersion=\"XML_1.0\" xsi:schemaLocation=\"urn:hl7-org:v3\">\n" +
                "\t<!--id-消息流水号 UUID -->\n" +
                "\t<id extension=\"fb8a9cfb-b242-4974-a965-046fa2b4715e\"/>\n" +
                "\t<!--creationTime-消息创建时间 -->\n" +
                "\t<creationTime value=\"20230216090010\"/>\n" +
                "\t<!--interactionId-消息的服务标识 -->\n" +
                "\t<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"PRPA_HIP1264\"/>\n" +
                "\t<!--processingCode-处理代码。标识此消息是否是产品、训练、调试系统的一部分。D：调试；P：产品；T：训练 -->\n" +
                "\t<processingCode code=\"P\"/>\n" +
                "\t<!-- processingModeCode-处理模型代码。定义此消息是一个文档处理还是一个初始装载的一部分。A： 存档；I：初始装载；R：从存档中恢复；T：当前处理，间隔传递。 -->\n" +
                "\t<processingModeCode/>\n" +
                "\t<!-- acceptAckCode-接收确认类型 AL：总是确认；NE：从不确认；ER：仅在错误/或拒绝时确认；SU： 仅在成功完成时确认。 -->\n" +
                "\t<acceptAckCode code=\"AL\"/>\n" +
                "\t<receiver typeCode=\"RCV\">\n" +
                "\t\t<device classCode=\"DEV\" determinerCode=\"INSTANCE\">\n" +
                "\t\t\t<id>\n" +
                "\t\t\t\t<!-- 接收方编号请向集成平台提供商获取 -->\n" +
                "\t\t\t\t<item extension=\"Orion-001\"/>\n" +
                "\t\t\t</id>\n" +
                "\t\t</device>\n" +
                "\t</receiver>\n" +
                "\t<sender typeCode=\"SND\">\n" +
                "\t\t<device classCode=\"DEV\" determinerCode=\"INSTANCE\">\n" +
                "\t\t\t<id>\n" +
                "\t\t\t\t<!-- 发送方编号请向集成平台提供商获取 -->\n" +
                "\t\t\t\t<item extension=\"OPTG-001\"/>\n" +
                "\t\t\t</id>\n" +
                "\t\t</device>\n" +
                "\t</sender>\n" +
                "\t<controlActProcess classCode=\"STC\" moodCode=\"EVN\">\n" +
                "\t\t<code code=\"InpatientEncounterStarted\">\n" +
                "\t\t\t<displayName value=\"患者住院新生儿服务\"/>\n" +
                "\t\t</code>\n" +
                "\t\t<!--\t母亲住院ID\t-->\n" +
                "\t\t<patientId value=\"z666\"/>\n" +
                "\t\t<!--\t新生儿住院ID\t-->\n" +
                "\t\t<babyId value=\"z123\"/>\n" +
                "\t\t<!--\t母亲住院次数\t-->\n" +
                "\t\t<parentTimes value=\"2\"/>\n" +
                "\t\t<!--\t母亲住院ID_住院次数\t-->\n" +
                "\t\t<parentHisId value=\"z666_2\"/>\n" +
                "\t\t<!--\t新生儿住院ID_住院次数\t-->\n" +
                "\t\t<babyHisId value=\"z123_1\"/>\n" +
                "\t\t<!--\t医院信息 code是医院代码  value是医院名称\t-->\n" +
                "\t\t<hospInfo code=\"1008611\" value=\"知名医院\"/>\n" +
                "\t</controlActProcess>\n" +
                "</PRPA_HIP1264>";
        HIP1264ReceiveMessageConverter converter = new HIP1264ReceiveMessageConverter(message);
        System.out.println(converter.xml2dto());
    }
}
