package com.docus.server.report.webservice;

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.config.ZdAssortConfig;
import com.docus.server.report.util.TableJsonRead;
import com.docus.server.report.util.XmlUtil;
import org.w3c.dom.Node;

/**
 * 报告处理
 * @author jiashi
 */
public interface WebserviceReceiveServerHandler {
    /**
     * 对接收到的消息进行业务处理，返回结果
     * @param receiveMessage 接收到的消息
     * @return 返回消息
     */
    String handle(String receiveMessage);

    /**
     * 验证上报文件的信息
     *
     * @param reportDto 上报文件的信息
     * @throws BaseException 验证不通过抛出业务异常
     */
     default void verifyFileInfo(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("文件名不能为空！");
        }
    }
    /**
     * 验证上报文件的信息
     *
     * @param reportDto 上报文件的信息
     * @throws BaseException 验证不通过抛出业务异常
     */
    default void verifyReportDto(ReportDto reportDto) throws BaseException {
        verifyFileInfo(reportDto);
        if (Func.isBlank(reportDto.getDownUrl())) {
            throw new BaseException("文件下载地址不能为空！");
        }
    }


    /**
     * 嘉时自定义接收接口使用，从xml工具读取返回文件上报需要的数据
     *
     * @param xmlUtil xml消息工具封装
     * @return 文件上报数据
     */
    default ReportDto convert(XmlUtil xmlUtil) {
        TableJsonRead jsonRead = new TableJsonRead();
        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= ZdAssortConfig.getZdAssortId(tableType);
            }
            if (Func.isBlank(assortId)) {
                assortId = ZdAssortConfig.getOtherAssortId();
            }
        } 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;
    }
}
