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

import com.alibaba.fastjson.JSONObject;
import com.docus.core.util.DateUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.server.report.dto.ReportDto;
import com.docus.server.report.service.ReportService;
import com.docus.server.report.util.IdUtil;
import com.docus.server.report.util.JSXMLResult;
import com.docus.server.report.util.TableJsonRead;
import com.docus.server.report.util.XmlUtil;
import com.docus.server.report.webservice.IReportServer;
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
 * @date 2023年3月3日13:42:36
 */
@Service
@Slf4j
public class SdryReportServerImpl implements IReportServer {

    @Resource
    private ReportService reportService;

    @Override
    public String pushSAReport(String saReportMessage) {
        // 待做
        log.info("收到手麻消息：{}", saReportMessage);
        String msgId = "";
        try {
            XmlUtil xmlUtil = XmlUtil.of(saReportMessage);
            Node msgNode = xmlUtil.getNode("/Request/Msg/ID");
            msgId = msgNode.getTextContent();
            ReportDto reportDto = getReportDtoByJSXML(xmlUtil);
            verifyReportDto(reportDto);
            reportService.report(reportDto);
            return JSXMLResult.success(msgId);
        } catch (BaseException baseException) {
            log.error(baseException.getMessage(), baseException);
            return JSXMLResult.failed(msgId, baseException.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return JSXMLResult.failed(msgId, "系统故障！");
        }
    }


    /**
     * 嘉时自定义接收接口使用，从xml工具读取返回文件上报需要的数据
     *
     * @param xmlUtil xml消息工具封装
     * @return 文件上报数据
     */
    public ReportDto getReportDtoByJSXML(XmlUtil xmlUtil) {
        TableJsonRead jsonRead = new TableJsonRead();
        JSONObject tableTypeJson = jsonRead.Read("data-config", "js-table-type.json", JSONObject.class);
        String jsReportConfigXml = jsonRead.ReadContent("data-config", "js-report-config.xml");
        XmlUtil configXmlUtil = XmlUtil.of(jsReportConfigXml);

        Node inpatientNoXpathNode = configXmlUtil.getNode("/REPORT/INPATIENT_NO");
        String inpatientNoXpath = inpatientNoXpathNode.getTextContent();
        Node inpatientNoNode = xmlUtil.getNode(inpatientNoXpath);
        String inpatientNo = inpatientNoNode.getTextContent();

        Node jzhXpathNode = configXmlUtil.getNode("/REPORT/JZH");
        String jzhXpath = jzhXpathNode.getTextContent();
        Node jzhNode = xmlUtil.getNode(jzhXpath);
        String jzh = jzhNode.getTextContent();

        Node admissTimesXpathNode = configXmlUtil.getNode("/REPORT/ADMISS_TIMES");
        String admissTimesXpath = admissTimesXpathNode.getTextContent();
        Node admissTimesNode = xmlUtil.getNode(admissTimesXpath);
        Integer admissTimes = Integer.valueOf(admissTimesNode.getTextContent());

        Node serialnumXpathNode = configXmlUtil.getNode("/REPORT/SERIALNUM");
        String serialnumXpath = serialnumXpathNode.getTextContent();
        Node serialnumNode = xmlUtil.getNode(serialnumXpath);
        String serialnum = serialnumNode.getTextContent();

        Node fileTitleXpathNode = configXmlUtil.getNode("/REPORT/FILE_TITLE");
        String fileTitleXpath = fileTitleXpathNode.getTextContent();
        Node fileTitleNode = xmlUtil.getNode(fileTitleXpath);
        String fileTitle = fileTitleNode.getTextContent();

        Node downUrlXpathNode = configXmlUtil.getNode("/REPORT/DOWNURL");
        String downUrlXpath = downUrlXpathNode.getTextContent();
        Node downUrlNode = xmlUtil.getNode(downUrlXpath);
        String downUrl = downUrlNode.getTextContent();

        Node tableTypeXpathNode = configXmlUtil.getNode("/REPORT/TABLE_TYPE");
        String tableTypeXpath = tableTypeXpathNode.getTextContent();
        Node tableTypeNode = xmlUtil.getNode(tableTypeXpath);
        String tableType = tableTypeNode.getTextContent();


        Node assortIdXpathNode = configXmlUtil.getNode("/REPORT/ASSORT_ID");
        String assortIdXpath = assortIdXpathNode.getTextContent();
        String assortId;
        if (Func.isBlank(assortIdXpath)) {
            // 如果没有配置 取value 属性，如果value也未配置 取 tableType 对应的json配置，最终方案默认 other 分段
            Node assortIdValueNode = assortIdXpathNode.getAttributes().getNamedItem("value");
            if (Func.isEmpty(assortIdValueNode)) {
                assortId = assortIdValueNode.getNodeValue();
            } else {
                assortId = tableTypeJson.getString(tableType);
            }
            if (Func.isBlank(assortId)) {
                assortId = tableTypeJson.getString("other");
            }
        } else {
            Node assortIdNode = xmlUtil.getNode(assortIdXpath);
            assortId = assortIdNode.getTextContent();
        }

        Node sysFlagXpathNode = configXmlUtil.getNode("/REPORT/SYSTEM_FLAG");
        String sysFlagXpath = sysFlagXpathNode.getTextContent();
        Node sysFlagNode = xmlUtil.getNode(sysFlagXpath);
        String sysFlag = sysFlagNode.getTextContent();

        Node fileSourceXpathNode = configXmlUtil.getNode("/REPORT/FILESOURCE");
        String fileSourceXpath = fileSourceXpathNode.getTextContent();
        String fileSource;
        if (Func.isBlank(fileSourceXpath)) {
            Node fileSourceValueNode = fileSourceXpathNode.getAttributes().getNamedItem("value");
            if (Func.isEmpty(fileSourceValueNode)) {
                fileSource = null;
            } else {
                fileSource = fileSourceValueNode.getNodeValue();
            }

        } else {
            Node fileSourceNode = xmlUtil.getNode(fileSourceXpath);
            fileSource = fileSourceNode.getTextContent();
        }


        Node fileStorageTypeXpathNode = configXmlUtil.getNode("/REPORT/FILE_STORAGE_TYPE");
        String fileStorageTypeXpath = fileStorageTypeXpathNode.getTextContent();
        String fileStorageType;
        if (Func.isBlank(fileStorageTypeXpath)) {
            Node filestoragetypeValueNode = fileStorageTypeXpathNode.getAttributes().getNamedItem("value");
            if (Func.isEmpty(filestoragetypeValueNode)) {
                fileStorageType = "1";
            } else {
                fileStorageType = filestoragetypeValueNode.getNodeValue();
            }

        } else {
            Node filestoragetypeNode = xmlUtil.getNode(fileStorageTypeXpath);
            fileStorageType = filestoragetypeNode.getTextContent();
        }

        ReportDto reportDto = new ReportDto();
        reportDto.setAdmisstimes(admissTimes);
        reportDto.setInpatientNo(inpatientNo);
        reportDto.setJzh(jzh);
        reportDto.setSerialnum(serialnum);
        reportDto.setFileTitle(fileTitle);
        reportDto.setDownUrl(downUrl);
        reportDto.setAssortId(assortId);
        reportDto.setSysFlag(sysFlag);
        reportDto.setFileSource(fileSource);
        reportDto.setFilestoragetype(fileStorageType);
        return reportDto;
    }

    @Override
    public String pushICUReport(String icuReportMessage) {
        log.info("收到重症消息：{}", icuReportMessage);
        String msgId = "";
        try {
            XmlUtil xmlUtil = XmlUtil.of(icuReportMessage);
            Node msgNode = xmlUtil.getNode("/Request/Msg/ID");
            msgId = msgNode.getTextContent();
            ReportDto reportDto = getReportDtoByJSXML(xmlUtil);
            verifyReportDto(reportDto);
            reportService.report(reportDto);
            return JSXMLResult.success(msgId);
        } catch (BaseException baseException) {
            log.error(baseException.getMessage(), baseException);
            return JSXMLResult.failed(msgId, baseException.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return JSXMLResult.failed(msgId, "系统故障！");
        }
    }

    @Override
    public String pushAddInspectionReport(String inspectionReportMessage) {
        log.info("收到检查报告新增消息：{}", inspectionReportMessage);
        String msgId = "";
        String sender = "";
        String receiver = "";
        try {
            XmlUtil xmlUtil = XmlUtil.of(inspectionReportMessage);
            msgId = xmlUtil.getNode("/POOR_HIP1008/id/@extension").getNodeValue();
            sender = xmlUtil.getNode("/POOR_HIP1008/sender/device/id/item/@extension").getNodeValue();
            receiver = xmlUtil.getNode("/POOR_HIP1008/receiver/device/id/item/@extension").getNodeValue();

            ReportDto reportDto = getReportDtoByInspectionInsert(xmlUtil);
            verifyReportDto(reportDto);
            reportService.report(reportDto);
            return insertSuccess(msgId, sender, receiver);
        } catch (BaseException baseException) {
            log.error(baseException.getMessage(), baseException);
            return insertFailed(msgId, sender, receiver, baseException.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return insertFailed(msgId, sender, receiver, "系统错误！");
        }
    }


    @Override
    public String pushUpdateInspectionReport(String inspectionReportMessage) {
        log.info("收到检查报告更新消息{}", inspectionReportMessage);
        String msgId = "";
        String sender = "";
        String receiver = "";
        try {
            XmlUtil xmlUtil = XmlUtil.of(inspectionReportMessage);
            msgId = xmlUtil.getNode("/POOR_HIP1009/id/@extension").getNodeValue();
            sender = xmlUtil.getNode("/POOR_HIP1009/sender/device/id/item/@extension").getNodeValue();
            receiver = xmlUtil.getNode("/POOR_HIP1009/receiver/device/id/item/@extension").getNodeValue();

            ReportDto reportDto = getReportDtoByInspectionUpdate(xmlUtil);
            verifyReportDto(reportDto);
            reportService.report(reportDto);
            return updateSuccess(msgId, sender, receiver);
        } catch (BaseException baseException) {
            log.error(baseException.getMessage(), baseException);
            return updateFailed(msgId, sender, receiver, baseException.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return updateFailed(msgId, sender, receiver, "系统错误");
        }

    }

    /**
     * 验证上报文件的信息
     *
     * @param reportDto 上报文件的信息
     * @throws BaseException 验证不通过抛出业务异常
     */
    private void verifyReportDto(ReportDto reportDto) throws BaseException {

        if (Func.isBlank(reportDto.getInpatientNo())) {
            throw new BaseException("住院号不能为空！");
        }

        if (Func.isEmpty(reportDto.getAdmisstimes()) && Func.isBlank(reportDto.getJzh())) {
            throw new BaseException("当住院号与住院次数都为空时，住院流水号/记账号不能为空！");
        }

        if (Func.isBlank(reportDto.getFileTitle())) {
            throw new BaseException("文件名不能为空！");
        }
        if (Func.isBlank(reportDto.getDownUrl())) {
            throw new BaseException("文件下载路径不能为空！");
        }
    }

    public ReportDto getReportDtoByInspectionInsert(XmlUtil inspectionInsertXmlUtil) {

        Node inpatientNoNode = inspectionInsertXmlUtil.getNode("/POOR_HIP1008/controlActProcess/subject/recordTarget/patientRole/id[@root='2.16.156.10011.1.12']/@extension");
        String inpatientNo = inpatientNoNode.getNodeValue();

        Node admissTimesNode = inspectionInsertXmlUtil.getNode("/POOR_HIP1008/controlActProcess/subject/recordTarget/patientRole/item[@root='1.2.156.112635.1.2.1.7']/@extension");
        Integer admissTimes = Integer.valueOf(admissTimesNode.getNodeValue());

        // 检查报告单号标识
        Node reportFlagNode = inspectionInsertXmlUtil.getNode("/POOR_HIP1008/controlActProcess/subject/recordTarget/patientRole/id[@root='2.16.156.10011.1.32']/@extension");
        String reportFlag = reportFlagNode.getNodeValue();
        //电子申请单编号
        Node eafNoNode = inspectionInsertXmlUtil.getNode("/POOR_HIP1008/controlActProcess/subject/recordTarget/patientRole/id[@root='2.16.156.10011.1.24']/@extension");
        String eafNo = eafNoNode.getNodeValue();
        String serialnum = reportFlag + "@" + eafNo;

        Node assortIdNode = inspectionInsertXmlUtil.getNode("/POOR_HIP1008/controlActProcess/subject/component/structuredBody/component[@displayName='检查报告']/section/entry[@displayName='检查类型']/observation/code/@displayName");
        String assortId = assortIdNode.getNodeValue();

        Node sysFlagNode = inspectionInsertXmlUtil.getNode("/POOR_HIP1008/controlActProcess/subject/component/structuredBody/component[@displayName='检查报告']/section/entry[@displayName='检查类型']/observation/code/@displayName");
        String sysFlag = sysFlagNode.getNodeValue();

        Node fileTitleNode = inspectionInsertXmlUtil.getNode("/POOR_HIP1008/controlActProcess/subject/component/structuredBody/component[@displayName='检查报告']/section/entry[@displayName='检查报告类型']/observation/code/@displayName");
        String fileTitle = fileTitleNode.getNodeValue();

        Node downUrlNode = inspectionInsertXmlUtil.getNode("/POOR_HIP1008/controlActProcess/subject/component/structuredBody/component[@displayName='检查报告图像']/report/entry[@displayName='检查报告图像URL地址']/observation/value");
        String downUrl = downUrlNode.getTextContent();

        ReportDto reportDto = new ReportDto();
        reportDto.setAdmisstimes(admissTimes);
        reportDto.setInpatientNo(inpatientNo);
        reportDto.setSerialnum(serialnum);
        reportDto.setFileTitle(fileTitle);
        reportDto.setDownUrl(downUrl);
        reportDto.setAssortId(assortId);
        reportDto.setSysFlag(sysFlag);
        reportDto.setFileSource("1");
        reportDto.setFilestoragetype("1");
        return reportDto;
    }

    public ReportDto getReportDtoByInspectionUpdate(XmlUtil inspectionUpdateXmlUtil) {
        Node inpatientNoNode = inspectionUpdateXmlUtil.getNode("/POOR_HIP1009/controlActProcess/subject/recordTarget/patientRole/id[@root='2.16.156.10011.1.12']/@extension");
        String inpatientNo = inpatientNoNode.getNodeValue();

        Node admissTimesNode = inspectionUpdateXmlUtil.getNode("/POOR_HIP1009/controlActProcess/subject/recordTarget/patientRole/item[@root='1.2.156.112635.1.2.1.7']/@extension");
        Integer admissTimes = Integer.valueOf(admissTimesNode.getNodeValue());

        // 检查报告单号标识
        Node reportFlagNode = inspectionUpdateXmlUtil.getNode("/POOR_HIP1009/controlActProcess/subject/recordTarget/patientRole/id[@root='2.16.156.10011.1.32']/@extension");
        String reportFlag = reportFlagNode.getNodeValue();
        //电子申请单编号
        Node eafNoNode = inspectionUpdateXmlUtil.getNode("/POOR_HIP1009/controlActProcess/subject/recordTarget/patientRole/id[@root='2.16.156.10011.1.24']/@extension");
        String eafNo = eafNoNode.getNodeValue();
        String serialnum = reportFlag + "@" + eafNo;

        Node assortIdNode = inspectionUpdateXmlUtil.getNode("/POOR_HIP1009/controlActProcess/subject/component/structuredBody/component[@displayName='检查报告']/section/entry[@displayName='检查类型']/observation/code/@displayName");
        String assortId = assortIdNode.getNodeValue();

        Node sysFlagNode = inspectionUpdateXmlUtil.getNode("/POOR_HIP1009/controlActProcess/subject/component/structuredBody/component[@displayName='检查报告']/section/entry[@displayName='检查类型']/observation/code/@displayName");
        String sysFlag = sysFlagNode.getNodeValue();

        Node fileTitleNode = inspectionUpdateXmlUtil.getNode("/POOR_HIP1009/controlActProcess/subject/component/structuredBody/component[@displayName='检查报告']/section/entry[@displayName='检查报告类型']/observation/code/@displayName");
        String fileTitle = fileTitleNode.getNodeValue();

        Node downUrlNode = inspectionUpdateXmlUtil.getNode("/POOR_HIP1009/controlActProcess/subject/component/structuredBody/component[@displayName='检查报告图像']/report/entry[@displayName='检查报告图像URL地址']/observation/value");
        String downUrl = downUrlNode.getTextContent();


        ReportDto reportDto = new ReportDto();
        reportDto.setAdmisstimes(admissTimes);
        reportDto.setInpatientNo(inpatientNo);
        reportDto.setSerialnum(serialnum);
        reportDto.setFileTitle(fileTitle);
        reportDto.setDownUrl(downUrl);
        reportDto.setAssortId(assortId);
        reportDto.setSysFlag(sysFlag);
        reportDto.setFileSource("1");
        reportDto.setFilestoragetype("1");
        return reportDto;
    }

    /**
     * 返回检查报告新增成功消息
     *
     * @param msgId    消息id
     * @param sender   发送路由
     * @param receiver 接收路由
     * @return 新增成功消息响应
     */
    public String insertSuccess(String msgId, String sender, String receiver) {
        String createTime = Func.format(new Date(), DateUtil.PATTERN_DATETIME_MINI);
        return "<MCCI_IN000002UV01 ITSVersion=\"XML_1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:hl7-org:v3 ../multicacheschemas/MCCI_IN000002UV01.xsd\" xmlns=\"urn:hl7-org:v3\">\n" +
                "\t<id root=\"2.16.156.10011.0\" extension=\"" + IdUtil.standardUUID() + "\"/>\n" +
                "\t<creationTime value=\"" + createTime + "\"/>\n" +
                "\t<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"MCCI_IN000002UV01\" displayable=\"true\"/>\n" +
                "\t<processingCode code=\"P\"/>\n" +
                "\t<processingModeCode/>\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<item root=\"2.16.156.10011.0.1.1\" extension=\"" + receiver + "\"/>\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<item root=\"2.16.156.10011.0.1.2\" extension=\"" + sender + "\"/>\n" +
                "\t\t\t</id>\n" +
                "\t\t</device>\n" +
                "\t</sender>\n" +
                "\t<acknowledgement typeCode=\"AA\">\n" +
                "\t\t<targetMessage>\n" +
                "\t\t\t<id root=\"2.16.156.10011.0\" extension=\"" + msgId + "\"/>\n" +
                "\t\t</targetMessage>\n" +
                "\t\t<acknowledgementDetail typeCode=\"I\">\n" +
                "      <text value=\"成功！\"/> \n" +
                "\t\t</acknowledgementDetail>\n" +
                "\t</acknowledgement>\n" +
                "</MCCI_IN000002UV01>\n";
    }

    /**
     * 返回检查报告插入失败响应消息
     *
     * @param msgId    消息id
     * @param sender   发送路由
     * @param receiver 接收路由
     * @return 检查报告新增失败响应消息
     */
    public String insertFailed(String msgId, String sender, String receiver, String failedMessage) {
        String createTime = Func.format(new Date(), DateUtil.PATTERN_DATETIME_MINI);
        return "<MCCI_IN000002UV01 ITSVersion=\"XML_1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:hl7-org:v3 ../multicacheschemas/MCCI_IN000002UV01.xsd\" xmlns=\"urn:hl7-org:v3\">\n" +
                "\t<id root=\"2.16.156.10011.0\" extension=\"" + IdUtil.standardUUID() + "\"/>\n" +
                "\t<creationTime value=\"" + createTime + "\"/>\n" +
                "\t<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"MCCI_IN000002UV01\" displayable=\"true\"/>\n" +
                "\t<processingCode code=\"P\"/>\n" +
                "\t<processingModeCode/>\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<item root=\"2.16.156.10011.0.1.1\" extension=\"" + receiver + "\"/>\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<item root=\"2.16.156.10011.0.1.2\" extension=\"" + sender + "\"/>\n" +
                "\t\t\t</id>\n" +
                "\t\t</device>\n" +
                "\t</sender>\n" +
                "\t<acknowledgement typeCode=\"AE\">\n" +
                "\t\t<targetMessage>\n" +
                "\t\t\t<id root=\"2.16.156.10011.0\" extension=\"" + msgId + "\"/>\n" +
                "\t\t</targetMessage>\n" +
                "\t\t<acknowledgementDetail typeCode=\"I\">\n" +
                "      <text value=\""+failedMessage+"\"/> \n" +
                "\t\t</acknowledgementDetail>\n" +
                "\t</acknowledgement>\n" +
                "</MCCI_IN000002UV01>\n";
    }

    /**
     * 返回检查报告更新成功响应消息
     *
     * @param msgId    消息id
     * @param sender   发送路由
     * @param receiver 接收路由
     * @return 更新成功响应消息
     */
    public String updateSuccess(String msgId, String sender, String receiver) {
        String createTime = Func.format(new Date(), DateUtil.PATTERN_DATETIME_MINI);
        return "<MCCI_IN000002UV01 ITSVersion=\"XML_1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:hl7-org:v3 ../multicacheschemas/MCCI_IN000002UV01.xsd\" xmlns=\"urn:hl7-org:v3\">\n" +
                "\t<id root=\"2.16.156.10011.0\" extension=\"" + IdUtil.standardUUID() + "\"/>\n" +
                "\t<creationTime value=\"" + createTime + "\"/>\n" +
                "\t<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"MCCI_IN000002UV01\" displayable=\"true\"/>\n" +
                "\t<processingCode code=\"P\"/>\n" +
                "\t<processingModeCode/>\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<item root=\"2.16.156.10011.0.1.1\" extension=\"" + receiver + "\"/>\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<item root=\"2.16.156.10011.0.1.2\" extension=\"" + sender + "\"/>\n" +
                "\t\t\t</id>\n" +
                "\t\t</device>\n" +
                "\t</sender>\n" +
                "\t<acknowledgement typeCode=\"AA\">\n" +
                "\t\t<targetMessage>\n" +
                "\t\t\t<id root=\"2.16.156.10011.0\" extension=\"" + msgId + "\"/>\n" +
                "\t\t</targetMessage>\n" +
                "\t\t<acknowledgementDetail typeCode=\"I\">\n" +
                "      <text value=\"成功！\"/> \n" +
                "\t\t</acknowledgementDetail>\n" +
                "\t</acknowledgement>\n" +
                "</MCCI_IN000002UV01>\n";
    }

    /**
     * 检查报告更新失败响应
     *
     * @param msgId         消息id
     * @param sender        发送路由
     * @param receiver      接收路由
     * @param failedMessage 失败信息
     * @return 更新失败响应信息
     */
    public String updateFailed(String msgId, String sender, String receiver, String failedMessage) {
        String createTime = Func.format(new Date(), DateUtil.PATTERN_DATETIME_MINI);
        return "<MCCI_IN000002UV01 ITSVersion=\"XML_1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:hl7-org:v3 ../multicacheschemas/MCCI_IN000002UV01.xsd\" xmlns=\"urn:hl7-org:v3\">\n" +
                "\t<id root=\"2.16.156.10011.0\" extension=\"" + IdUtil.standardUUID() + "\"/>\n" +
                "\t<creationTime value=\"" + createTime + "\"/>\n" +
                "\t<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"MCCI_IN000002UV01\" displayable=\"true\"/>\n" +
                "\t<processingCode code=\"P\"/>\n" +
                "\t<processingModeCode/>\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<item root=\"2.16.156.10011.0.1.1\" extension=\"" + receiver + "\"/>\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<item root=\"2.16.156.10011.0.1.2\" extension=\"" + sender + "\"/>\n" +
                "\t\t\t</id>\n" +
                "\t\t</device>\n" +
                "\t</sender>\n" +
                "\t<acknowledgement typeCode=\"AE\">\n" +
                "\t\t<targetMessage>\n" +
                "\t\t\t<id root=\"2.16.156.10011.0\" extension=\"" + msgId + "\"/>\n" +
                "\t\t</targetMessage>\n" +
                "\t\t<acknowledgementDetail typeCode=\"I\">\n" +
                "      <text value=\""+failedMessage+"\"/> \n" +
                "\t\t</acknowledgementDetail>\n" +
                "\t</acknowledgement>\n" +
                "</MCCI_IN000002UV01>\n";
    }
}
