package com.docus.server.collect.report;

import com.alibaba.fastjson.JSONObject;
import com.docus.core.util.Func;
import com.docus.core.util.ParamsUtils;
import com.docus.core.util.XmlUtil;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.server.archivefile.infrastructure.dto.ReportDTO;
import com.docus.server.collect.IConverter;
import com.docus.server.collect.web.utils.TableJsonRead;
import org.springframework.stereotype.Component;
import org.w3c.dom.Node;

@Component("reportConverter")
public class ReportConverter implements IConverter<ReportDTO> {

    @Override
    public ReportDTO convert(String message, String group) {
        //TODO： 根据协议进行解析。
        XmlUtil xmlUtil = XmlUtil.of(message);
        Node msgNode = xmlUtil.getNode("/Request/Msg/ID");
        String msgId = msgNode.getTextContent();

        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);

        reportDTO.setParams(ParamsUtils.addParam("msgId", msgId).param());

        valid(reportDTO);
        return reportDTO;
    }

    private void valid(ReportDTO reportDTO) {

        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("文件下载路径不能为空！");
        }
    }
}
