package org.springblade.thirdparty.wps.service.impl;

import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.thirdparty.wps.constant.WpsConstant;
import org.springblade.thirdparty.wps.exception.WpsException;
import org.springblade.thirdparty.wps.feign.IWpsClient;
import org.springblade.thirdparty.wps.pojo.dto.WpsFilePreviewDTO;
import org.springblade.thirdparty.wps.pojo.enums.WpsFile;
import org.springblade.thirdparty.wps.pojo.vo.WpsFilePreviewVO;
import org.springblade.thirdparty.wps.pojo.vo.WpsResultVO;
import org.springblade.thirdparty.wps.service.IWpsService;
import org.springframework.stereotype.Service;

/**
 * @author bfhuange
 * @date 2024/10/30
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class WpsServiceImpl implements IWpsService {

    private final IWpsClient client;

    @Override
    public String getFilePreviewUrl(String fileId, String fileName) {
        if (StringUtil.isBlank(fileId) || StringUtil.isBlank(fileName)) {
            log.info("获取wps预览链接 参数 fileId：{} fileName：{}", fileId, fileName);
            throw new WpsException("参数不能为空");
        }
        WpsFilePreviewDTO previewDTO = new WpsFilePreviewDTO();
        // 根据文件名获取文件格式和预览模式
        WpsFile.Type type = WpsFile.Type.getByFileName(fileName);
        previewDTO.setType(type.getCode());
        try {
            // get请求，对象参数不支持别名，需要提取出来
            WpsResultVO<WpsFilePreviewVO> resultVO = client.getFilePreviewUrl(fileId, type.getPreviewMode(), WpsConstant.TOKEN_TYPE_YES, previewDTO);
            if (resultVO != null && resultVO.getData() != null && WpsConstant.SUCCESS_CODE.equals(resultVO.getCode())) {
                // 成功，返回预览链接
                String link = resultVO.getData().getLink();
                if (WpsFile.Type.EXCEL.equals(type)) {
                    // excel，去掉simple参数，以便查看单元格的公式
                    return link.replace(WpsConstant.MODE_PARAM, "");
                }
                return link;
            }
            log.error("获取wps预览链接异常 {}", JSONUtil.toJsonStr(resultVO));
            throw new WpsException("获取wps预览链接异常");
        } catch (WpsException e) {
            throw e;
        } catch (Exception e) {
            log.error("获取wps预览链接异常", e);
            throw new WpsException("获取wps预览链接异常");
        }
    }

    @Override
    public String getWpsFileEditUrl(String fileId, String fileName) {
        if (StringUtil.isBlank(fileId) || StringUtil.isBlank(fileName)) {
            log.info("获取wps编辑链接 参数 fileId：{} fileName：{}", fileId, fileName);
            throw new WpsException("参数不能为空");
        }
        WpsFilePreviewDTO previewDTO = new WpsFilePreviewDTO();
        // 根据文件名获取文件格式和预览模式
        WpsFile.Type type = WpsFile.Type.getByFileName(fileName);
        previewDTO.setType(type.getCode());
        try {
            // get请求，对象参数不支持别名，需要提取出来
            WpsResultVO<WpsFilePreviewVO> resultVO = client.getFileEditUrl(fileId, type.getPreviewMode(), WpsConstant.TOKEN_TYPE_YES, previewDTO);
            if (resultVO != null && resultVO.getData() != null && WpsConstant.SUCCESS_CODE.equals(resultVO.getCode())) {
                // 成功，返回预览链接
                return resultVO.getData().getLink();
            }
            log.error("获取wps编辑链接异常 {}", JSONUtil.toJsonStr(resultVO));
            throw new WpsException("获取wps编辑链接异常:"+JSONUtil.toJsonStr(resultVO));
        } catch (WpsException e) {
            throw e;
        } catch (Exception e) {
            log.error("获取wps编辑链接异常", e);
            throw new WpsException("获取wps编辑链接异常");
        }
    }
}
