package com.docus.server.message.service;

import com.alibaba.fastjson.JSON;
import com.docus.core.util.Func;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.infrastructure.web.api.ResultCode;
import com.docus.server.message.feign.dto.ReportFile;
import com.docus.server.message.feign.service.DownloadPlatformService;
import com.docus.server.message.util.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.Node;

/**
 *
 * @author YongBin Wen
 * @date 2026/1/7 星期三 9:06
 */
@Service
@Slf4j
public class ReportFileMessageServiceImpl implements ReportFileMessageService {
    private DownloadPlatformService downloadPlatformService;

    @Autowired
    public void setDownloadPlatformService(DownloadPlatformService downloadPlatformService) {
        this.downloadPlatformService = downloadPlatformService;
    }

    @Override
    public String commonFile(String message) {
        try {
            ReportFile reportFile = convertReportFile(message);
            log.info("接收报告消息：{}", reportFile.toLogString());
            CommonResult<String> commonResult = downloadPlatformService.reportFile(reportFile);
            if (commonResult.getCode().equals(ResultCode.SUCCESS.getCode())) {
                return success();
            }
            return error(commonResult.getMsg());
        } catch (BaseException e) {
            log.error("接收报告消息处理出错！{}", e.getMessage(), e);
            return error(e.getMessage());
        } catch (Exception e) {
            log.error("接收报告消息处理出错,消息内容：{}", message);
            log.error("接收报告消息处理出错！{}", e.getMessage(), e);
            return error("系统错误！");
        }

    }

    private ReportFile convertReportFile(String message) {
        XmlUtil xmlUtil = XmlUtil.of(message);
        Node formatNode = xmlUtil.getNode("/Request/Format");
        if (formatNode == null) {
            throw new BaseException("Format节点为空！");
        }
        Node reportFileNode = xmlUtil.getNode("/Request/ReportFile");
        if (reportFileNode == null) {
            throw new BaseException("ReportFile节点为空！");
        }

        String format = formatNode.getTextContent();
        if (!"xml".equalsIgnoreCase(format)
                && !"json".equalsIgnoreCase(format)) {
            throw new BaseException("Format节点内容为XML或者JSON！");
        }

        if ("json".equalsIgnoreCase(format)) {
            String reportFileJsonContent = reportFileNode.getTextContent();
            return JSON.parseObject(reportFileJsonContent, ReportFile.class);
        } else {
            String zyh = null;
            Integer zycs = null;
            String jzh = null;
            String fileId = null;
            String fileName = null;
            String fileType = null;
            String fileAfTime = null;
            String fileTime = null;
            String fileUpdateTime = null;
            String source = null;
            String fileModule = null;
            String fileModuleName = null;
            int cancel = 0;
            Integer filePathType = null;
            String filePath = null;

            Node zyhNode = xmlUtil.getNode("/Request/ReportFile/zyh");
            if (zyhNode != null) {
                zyh = zyhNode.getTextContent();
            }
            Node zycsNode = xmlUtil.getNode("/Request/ReportFile/zycs");
            if (zycsNode != null) {
                String zycsStr = zycsNode.getTextContent();
                if (Func.isNotBlank(zycsStr)) {
                    zycs = Integer.parseInt(zycsStr);
                }
            }
            Node jzhNode = xmlUtil.getNode("/Request/ReportFile/jzh");
            if (jzhNode != null) {
                jzh = jzhNode.getTextContent();
            }
            Node fileIdNode = xmlUtil.getNode("/Request/ReportFile/fileId");
            if (fileIdNode != null) {
                fileId = fileIdNode.getTextContent();
            }
            Node fileNameNode = xmlUtil.getNode("/Request/ReportFile/fileName");
            if (fileNameNode != null) {
                fileName = fileNameNode.getTextContent();
            }
            Node fileTypeNode = xmlUtil.getNode("/Request/ReportFile/fileType");
            if (fileTypeNode != null) {
                fileType = fileTypeNode.getTextContent();
            }
            Node fileAftimeNode = xmlUtil.getNode("/Request/ReportFile/fileAfTime");
            if (fileAftimeNode != null) {
                fileAfTime = fileAftimeNode.getTextContent();
            }
            Node fileTimeNode = xmlUtil.getNode("/Request/ReportFile/fileTime");
            if (fileTimeNode != null) {
                fileTime = fileTimeNode.getTextContent();
            }
            Node fileUpdateTimeNode = xmlUtil.getNode("/Request/ReportFile/fileUpdateTime");
            if (fileUpdateTimeNode != null) {
                fileUpdateTime = fileUpdateTimeNode.getTextContent();
            }
            Node sourceNode = xmlUtil.getNode("/Request/ReportFile/source");
            if (sourceNode != null) {
                source = sourceNode.getTextContent();
            }
            Node fileModuleNode = xmlUtil.getNode("/Request/ReportFile/fileModule");
            if (fileModuleNode != null) {
                fileModule = fileModuleNode.getTextContent();
            }
            Node fileModuleNameNode = xmlUtil.getNode("/Request/ReportFile/fileModuleName");
            if (fileModuleNameNode != null) {
                fileModuleName = fileModuleNameNode.getTextContent();
            }
            Node cancelNode = xmlUtil.getNode("/Request/ReportFile/cancel");
            if (cancelNode != null) {
                String cancelStr = cancelNode.getTextContent();
                if (Func.isNotBlank(cancelStr)) {
                    cancel = Integer.parseInt(cancelStr);
                }
            }
            Node filePathTypeNode = xmlUtil.getNode("/Request/ReportFile/filePathType");
            if (filePathTypeNode != null) {
                String filePathTypeStr = filePathTypeNode.getTextContent();
                if (Func.isNotBlank(filePathTypeStr)) {
                    filePathType = Integer.parseInt(filePathTypeStr);
                }
            }
            Node filePathNode = xmlUtil.getNode("/Request/ReportFile/filePath");
            if (filePathNode != null) {
                filePath = filePathNode.getTextContent();
            }

            ReportFile reportFile = new ReportFile();
            reportFile.setZycs(zycs);
            reportFile.setZyh(zyh);
            reportFile.setJzh(jzh);
            reportFile.setFileId(fileId);
            reportFile.setFileName(fileName);
            reportFile.setFileType(fileType);
            reportFile.setFileAfTime(fileAfTime);
            reportFile.setFileTime(fileTime);
            reportFile.setFileUpdateTime(fileUpdateTime);
            reportFile.setSource(source);
            reportFile.setFileModule(fileModule);
            reportFile.setFileModuleName(fileModuleName);
            reportFile.setCancel(cancel);
            reportFile.setFilePathType(filePathType);
            reportFile.setFilePath(filePath);
            return reportFile;
        }


    }


    private String success() {
        return "<Response>" +
                "<Code>0</Code>" +
                "<Msg>上传成功</Msg>" +
                "</Response>";
    }

    private String error(String message) {
        return "<Response>" +
                "<Code>500</Code>" +
                "<Msg>" + message + "</Msg>" +
                "</Response>";
    }
}
