package com.ruoyi.dxp.utils;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.dxp.domain.ServiceIntegration;
import com.ruoyi.dxp.dto.LicenseDto;
import com.ruoyi.dxp.dto.ResApiDbListDto;
import com.ruoyi.dxp.dto.ResApiPageDto;
import com.ruoyi.dxp.dto.ResApiResultDto;
import com.ruoyi.dxp.dto.database.ServiceUtilsApiCallParam;

import java.util.List;

/**
 * 三方服务联调帮助类
 */
public class ServiceUtils {
    /**
     * 远程接口联调
     *
     * @param req 请求参数
     * @return 调用结果
     */
    public static Object apiCall(ServiceUtilsApiCallParam req) {
        String url = OutServiceConfig.ApiUrl + "/dxp/outapi/base/apiCall";
        String param = JSON.toJSONString(req);
        return HttpUtils.sendPost(url, param);
    }


    /**
     * 用户端查询服务
     */
    public static R<ResApiPageDto<List<Object>>> customerApiCall(String accessId, String param) {
        String url = OutServiceConfig.ApiUrl + "/dxp/outapi/base/proxyToService/any/" + accessId;
        String resApiResult = HttpUtils.sendPost(url, param);
        if (StringUtils.isEmpty(resApiResult)) {
            return R.fail("未获取到接口结果");
        }

        ResApiResultDto<ResApiPageDto<List<Object>>> result = JSON.parseObject(resApiResult, ResApiResultDto.class);
        if (result != null) {
            if (result.getCode().equals(0)) {
                return R.ok(result.getData());
            } else {
                return R.fail(result.getCode().intValue(), result.getMsg());
            }
        }

        return R.ok();
    }

    /**
     * 获取版权信息
     *
     * @return
     */
    public static Object getLicenseInfo() {
        String url = OutServiceConfig.ApiUrl + "/dxp/core/getLicenseInfo";
        String resApiResult = HttpUtils.sendPost(url, null);
        ResApiResultDto<LicenseDto> result = JSON.parseObject(resApiResult, ResApiResultDto.class);
        if (StringUtils.isEmpty(resApiResult)) {
            LicenseDto dto = new LicenseDto();
            dto.setCompany("未知公司");
            dto.setExpire_days(0);
            dto.setRet(false);
            dto.setSw_system("未知系统名称");
            return R.fail(dto);
        }


        return R.ok(result.getData());
    }
}
