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

import com.docus.core.util.DateUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.server.report.api.dto.ReportDto;
import com.docus.server.report.service.ReportService;
import com.docus.server.report.util.IdUtil;
import com.docus.server.report.util.XmlUtil;
import com.docus.server.report.webservice.WebserviceMessageType;
import com.docus.server.report.webservice.WebserviceReceiveServerHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.w3c.dom.Node;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 重症症接收
 *
 * @author wyb
 */
@Service(WebserviceMessageType.HIP1166)
@Slf4j
public class HIP1166PdfUploadHandler implements WebserviceReceiveServerHandler {
    @Resource
    private ReportService reportService;

    @Override
    public String handle(String receiveMessage) {
        log.info("收到pdf上传信息{}", receiveMessage);
        String msgId = "";
        String sender = "";
        String receiver = "";
        try {
            XmlUtil xmlUtil = XmlUtil.of(receiveMessage);
            msgId = xmlUtil.getNode("/POOR_HIP1166/id/@extension").getNodeValue();
            sender = xmlUtil.getNode("/POOR_HIP1166/sender/device/id/item/@extension").getNodeValue();
            receiver = xmlUtil.getNode("/POOR_HIP1166/receiver/device/id/item/@extension").getNodeValue();
            ReportDto reportDto = getReportDtoByPdfUpload(xmlUtil);
            reportService.reportBuffer(reportDto);
            return uploadSuccess(msgId, sender, receiver);
        } catch (BaseException baseException) {
            log.error(baseException.getMessage(), baseException);
            return uploadFailed(msgId, sender, receiver, baseException.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return uploadFailed(msgId, sender, receiver, "系统错误！");
        }
    }

    private  ReportDto getReportDtoByPdfUpload(XmlUtil xmlUtil) {
        // 检查报告单号标识
        Node reportSnNode = xmlUtil.getNode("/POOR_HIP1166/controlActProcess/component/structuredBody/component[@displayName='文档描述信息']/section/entry/reportInfomation/reportSn/@extension");
        String reportSn = reportSnNode.getNodeValue();

        Node downUrlNode = xmlUtil.getNode("/POOR_HIP1166/controlActProcess/component/structuredBody/component[@displayName='文档描述信息']/section/entry/reportInfomation/reportPath/@extension");
        String downUrl = downUrlNode.getTextContent();

        ReportDto reportDto = new ReportDto();
        reportDto.setReportSn(reportSn);
        reportDto.setDowntype(1);
        reportDto.setDownUrl(downUrl);
        return reportDto;
    }
    private String uploadFailed(String msgId, String sender, String receiver, String message) {
        String createTime = Func.format(new Date(), DateUtil.PATTERN_DATETIME_MINI);
        return "<MCCI_IN000002UV01 xmlns=\"urn:hl7-org:v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ITSVersion=\"XML_1.0\" xsi:schemaLocation=\"urn:hl7-org:v3  file:///E:/hl7/HL7/v3ballot_fullsite_2011MAY/v3ballot/html/processable/multicacheschemas/MCCI _IN000002UV01.xsd\">" +
                "<id extension=\""+ IdUtil.standardUUID()+"\"/>" +
                "<creationTime value=\""+createTime+"\"/>" +
                "<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"MCCI_IN000002UV01\"/>" +
                "<processingCode code=\"P\"/>" +
                "<processingModeCode/>" +
                "<acceptAckCode code=\"AL\"/>" +
                "<receiver typeCode=\"RCV\">" +
                "<device classCode=\"DEV\" determinerCode=\"INSTANCE\">" +
                "<id>" +
                "<item extension=\"Orion-001\"/>" +
                "</id>" +
                "</device>" +
                "</receiver>" +
                "<sender typeCode=\"SND\">" +
                "<device classCode=\"DEV\" determinerCode=\"INSTANCE\">" +
                "<id>" +
                "<item extension=\"PaperlessManagementMRIS-001\"/>" +
                "</id>" +
                "</device>" +
                "</sender>" +
                "<acknowledgement typeCode=\"AE\">" +
                "<targetMessage>" +
                "<id extension=\""+msgId+"\"/>" +
                "</targetMessage>" +
                "<acknowledgementDetail>" +
                "<text value=\""+message+"\"/>" +
                "</acknowledgementDetail>" +
                "</acknowledgement>" +
                "</MCCI_IN000002UV01>";
    }
    private String uploadSuccess(String msgId, String sender, String receiver) {
        String createTime = Func.format(new Date(), DateUtil.PATTERN_DATETIME_MINI);
        return "<MCCI_IN000002UV01 xmlns=\"urn:hl7-org:v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ITSVersion=\"XML_1.0\" xsi:schemaLocation=\"urn:hl7-org:v3  file:///E:/hl7/HL7/v3ballot_fullsite_2011MAY/v3ballot/html/processable/multicacheschemas/MCCI _IN000002UV01.xsd\">" +
                "<id extension=\""+IdUtil.standardUUID()+"\"/>" +
                "<creationTime value=\""+createTime+"\"/>" +
                "<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"MCCI_IN000002UV01\"/>" +
                "<processingCode code=\"P\"/>" +
                "<processingModeCode/>" +
                "<acceptAckCode code=\"AL\"/>" +
                "<receiver typeCode=\"RCV\">" +
                "<device classCode=\"DEV\" determinerCode=\"INSTANCE\">" +
                "<id>" +
                "<item extension=\"Orion-001\"/>" +
                "</id>" +
                "</device>" +
                "</receiver>" +
                "<sender typeCode=\"SND\">" +
                "<device classCode=\"DEV\" determinerCode=\"INSTANCE\">" +
                "<id>" +
                "<item extension=\"PaperlessManagementMRIS-001\"/>" +
                "</id>" +
                "</device>" +
                "</sender>" +
                "<acknowledgement typeCode=\"AA\">" +
                "<targetMessage>" +
                "<id extension=\""+msgId+"\"/>" +
                "</targetMessage>" +
                "<acknowledgementDetail>" +
                "<text value=\"成功\"/>" +
                "</acknowledgementDetail>" +
                "</acknowledgement>" +
                "</MCCI_IN000002UV01>";
    }
}
