package com.friday.tooldriver.service.rpa.supplier;

import com.alibaba.fastjson.JSON;
import com.friday.tooldriver.dto.infodator.common.InfodatorOriginResult;
import com.friday.tooldriver.dto.infodator.request.*;
import com.friday.tooldriver.dto.infodator.response.*;
import com.friday.tooldriver.dto.infodator.response.Process;
import com.google.common.collect.Lists;
import com.friday.base.common.response.DubboResult;
import com.friday.operation.api.keepalive.dto.taieoc.OcTenantDetailDto;
import com.friday.operation.api.keepalive.dto.taieoc.OcTenantFilterDto;
import com.friday.operation.api.keepalive.service.taieoc.ITenantManage;
import com.friday.operation.api.tooldriver.constant.DriverConstant;
import com.friday.operation.api.tooldriver.dto.common.DriverBase;
import com.friday.operation.api.tooldriver.dto.rpa.FileInfoDto;
import com.friday.tooldriver.constants.infodator.InfodatorApiEnum;
import com.friday.tooldriver.dto.infodator.common.*;
import com.friday.tooldriver.dto.infodator.request.*;
import com.friday.tooldriver.dto.infodator.response.*;
import com.friday.tooldriver.exception.DriverBusinessException;
import com.friday.tooldriver.utils.AESUtils;
import com.friday.tooldriver.util.OkHttpService;
import com.friday.tooldriver.util.TaieHttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.friday.operation.api.tooldriver.constant.DriverConstant.EMPTY_STRING;
import static com.friday.operation.api.tooldriver.constant.DriverConstant.INFODATOR_FORM_FILE_KEY;
import static com.friday.tooldriver.constants.DriverSystemCode.INFODATOR_RESPONSE_DATA_ERROR;
import static com.friday.tooldriver.constants.infodator.InfodatorApiEnum.*;
import static com.friday.tooldriver.constants.uipath.UIPathApiEnum.AUTHENTICATE;

/**
 * @Author GW
 * @Date Created In 09:47 2022/12/20
 * @Description 容智服务类
 * @Modified By
 */
@Slf4j
@Service
public class INFODATORService {

    @Autowired
    private OkHttpService okHttpService;
    @Autowired(required = false)
    private RedissonClient redissonClient;
    @Autowired
    private ITenantManage tenantManage;

    @Value("${infodator.oc.service.url}")
    private String infodatorServiceUrl;
    @Value("${infodator.package.file.path}")
    private String infodatorPkgFilePath;
    @Value("${infodator.expected.time}")
    private Integer expectedTime;

    private static final String groupColor = "#11A4FC";
    private static final String robotClientVersion = "Microsoft Windows 10 企业版 LTSC;10.0.17763;64 位";
    private static final String robotOsInfo = "1.0.0";

    /**
     * 验证信息
     * @param driverBase 入参
     * @return 结果 字符串
     */
    public String authenticate(DriverBase driverBase) throws DriverBusinessException {
        // 根据租户获取用户信息
        OcTenantDetailDto ocTenantInfo = getOcTenantInfo(driverBase);
        LoginRequestDto login = LoginRequestDto.builder()
                .userName(ocTenantInfo.getOcTenantUser())
                .password(ocTenantInfo.getOcTenantPassword())
                .build();
        // 测试账号
//        login.setUserName("admin");
//        login.setPassword("admin1");
        getLoginKey(driverBase, login);
        return getLoginToken(driverBase, login);
    }

    /**
     * 获取加密秘钥
     * @param driverBase 基类
     * @param login 登录信息
     */
    private void getLoginKey(DriverBase driverBase, LoginRequestDto login){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, false);
            InfodatorOriginResult keyResult = this.doRequest(LOGIN_KEY, headerMap, JSON.toJSONString(login), null);
            if (keyResult.success()) {
                DataResponseDto<LoginResponseDto> keyResponse = (DataResponseDto<LoginResponseDto>) keyResult.getObject();
                if (this.resultSuccess(keyResponse.getCode())) {
                    LoginResponseDto responseDto = keyResponse.getData();
                    login.setSecTitle(responseDto.getTitle());
                    String password = AESUtils.encrypt(login.getPassword(), responseDto.getKey());
                    login.setPassword(password);
                    return;
                } else {
                    log.error(LOGIN_KEY.getSystemCode().getMessage() + keyResponse.getMsg());
                }
            }
            log.error(LOGIN_KEY.getSystemCode().getMessage(), keyResult.getMessage());
        } catch (Exception e) {
            log.error(LOGIN_KEY.getSystemCode().getMessage(), e);
        }
    }

    /**
     * 登录接口创建token
     * @param driverBase 基类
     * @param login 登录信息
     * @return token信息
     */
    private String getLoginToken(DriverBase driverBase, LoginRequestDto login){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, false);
            InfodatorOriginResult loginResult = this.doRequest(LOGIN, headerMap, JSON.toJSONString(login), null);
            if (loginResult.success()) {
                DataResponseDto<LoginResponseDto> loginResponse = (DataResponseDto<LoginResponseDto>) loginResult.getObject();
                if (this.resultSuccess(loginResponse.getCode())) {
                    return loginResponse.getData().getToken();
                } else {
                    log.error(LOGIN.getSystemCode().getMessage() + loginResponse.getMsg());
                }
            }
            log.error(LOGIN.getSystemCode().getMessage(), loginResult.getMessage());
        } catch (Exception e) {
            log.error(LOGIN.getSystemCode().getMessage(), e);
        }
        return EMPTY_STRING;
    }

    /**
     * 上传代码包
     * @param driverBase 基类
     * @param file 文件
     * @return 结果
     */
    public DubboResult<UploadFileResponseDto> uploadPackage(DriverBase driverBase, File file) {
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult uploadResult = this.doFormRequest(PROCESS_UPLOAD, headerMap, file, null);
            if (uploadResult.success()) {
                DataResponseDto<UploadFileResponseDto> fileResponse = (DataResponseDto<UploadFileResponseDto>) uploadResult.getObject();
                if (this.resultSuccess(fileResponse.getCode())) {
                    return DubboResult.newSuccess(fileResponse.getData());
                }
                return DubboResult.newFailure(PROCESS_UPLOAD.getSystemCode().getCode(), fileResponse.getMsg());
            } else {
                return DubboResult.newFailure(PROCESS_UPLOAD.getSystemCode().getCode(), uploadResult.getMessage());
            }
        } catch (Exception e) {
            log.error(PROCESS_UPLOAD.getSystemCode().getMessage(), e);
            return DubboResult.newFailure(PROCESS_UPLOAD.getSystemCode().getCode(), e.getMessage());
        }
    }

    /**
     * 容智不支持删除代码包
     * @param driverBase 基类
     * @param key 文件key
     * @return 结果
     */
    public DubboResult deletePackageByKey(DriverBase driverBase, String key) {
        return DubboResult.newFailure(PACKAGE_DELETE.getSystemCode().getCode(), PACKAGE_DELETE.getSystemCode().getMessage());
    }

    /**
     * 下载任务发布包
     * @param driverBase 基类
     * @param logicFileId 上传文件返回的文件id
     * @return 结果
     */
    public DubboResult<FileInfoDto> downloadPackageByKey(DriverBase driverBase, String logicFileId, String fileName) {
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("logicFileId", logicFileId);
            InfodatorOriginResult<String> downloadResult = this.doDownloadRequest(TASK_DOWNLOAD, headerMap, uriParams, infodatorPkgFilePath, fileName);
            if (downloadResult.success()) {
                return DubboResult.newSuccess(FileInfoDto.builder().path(infodatorPkgFilePath + fileName).fileName(fileName).build());
            } else {
                return DubboResult.newFailure(TASK_DOWNLOAD.getSystemCode().getCode(), downloadResult.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_DOWNLOAD.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_DOWNLOAD.getSystemCode().getCode(), TASK_DOWNLOAD.getSystemCode().getMessage());
    }

    /**
     * 修改流程默认版本
     * @param driverBase 基类
     * @param templateId 流程id
     * @param versionId 版本id
     * @return 结果
     */
    public DubboResult releaseProcess(DriverBase driverBase, String templateId, String versionId) {
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("templateId", templateId);
            uriParams.put("versionId", versionId);
            InfodatorOriginResult processResult = this.doRequest(PROCESS_CHANGE_DEFAULT, headerMap, null, uriParams);
            if (processResult.success()) {
                DataResponseDto<Process> processDataResponseDto = (DataResponseDto<Process>) processResult.getObject();
                if (this.resultSuccess(processDataResponseDto.getCode())) {
                    return DubboResult.newSuccess();
                }
                return DubboResult.newFailure(PROCESS_CHANGE_DEFAULT.getSystemCode().getCode(), processDataResponseDto.getMsg());
            } else {
                return DubboResult.newFailure(PROCESS_CHANGE_DEFAULT.getSystemCode().getCode(), processResult.getMessage());
            }
        } catch (Exception e) {
            log.error(PROCESS_CHANGE_DEFAULT.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(PROCESS_CHANGE_DEFAULT.getSystemCode().getCode(), PROCESS_CHANGE_DEFAULT.getSystemCode().getMessage());
    }

    /**
     * 获取流程版本
     * @param driverBase 基类
     * @param queryRequestDto 入参
     * @return 结果
     */
    public DubboResult<ArrayList<HistoryVersionDto>> getProcessVersions(DriverBase driverBase, QueryRequestDto queryRequestDto) {
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult versionResult = this.doRequest(PROCESS_HISTORY_VERSION, headerMap, JSON.toJSONString(queryRequestDto), null);
            if (versionResult.success()) {
                DataResponseDto<ResponseDto<HistoryVersionDto>> versionDataResponseDto = (DataResponseDto<ResponseDto<HistoryVersionDto>>) versionResult.getObject();
                if (this.resultSuccess(versionDataResponseDto.getCode())) {
                    ResponseDto<HistoryVersionDto> responseDto = versionDataResponseDto.getData();
                    return DubboResult.newSuccess(Lists.newArrayList(responseDto.getValue()));
                }
                return DubboResult.newFailure(PROCESS_HISTORY_VERSION.getSystemCode().getCode(), versionDataResponseDto.getMsg());
            } else {
                return DubboResult.newFailure(PROCESS_HISTORY_VERSION.getSystemCode().getCode(), versionResult.getMessage());
            }
        } catch (Exception e) {
            log.error(PROCESS_HISTORY_VERSION.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(PROCESS_HISTORY_VERSION.getSystemCode().getCode(), PROCESS_HISTORY_VERSION.getSystemCode().getMessage());
    }

    /**
     * 删除流程
     * @param driverBase 基类
     * @param templateId 流程id
     * @return 结果
     */
    public DubboResult deleteProcessReleaseByKey(DriverBase driverBase, String templateId) {
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("templateId", templateId);
            InfodatorOriginResult processResult = this.doRequest(PROCESS_DELETE, headerMap, null, uriParams);
            if (processResult.success()) {
                DataResponseDto<Integer> processDataResponseDto = (DataResponseDto<Integer>) processResult.getObject();
                if (this.resultSuccess(processDataResponseDto.getCode())) {
                    return DubboResult.newSuccess(processDataResponseDto.getData());
                }
                return DubboResult.newFailure(PROCESS_DELETE.getSystemCode().getCode(), processDataResponseDto.getMsg());
            } else {
                return DubboResult.newFailure(PROCESS_DELETE.getSystemCode().getCode(), processResult.getMessage());
            }
        } catch (Exception e) {
            log.error(PROCESS_DELETE.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(PROCESS_DELETE.getSystemCode().getCode(), PROCESS_DELETE.getSystemCode().getMessage());
    }

    /**
     * 获取流程列表
     * @param driverBase 基类
     * @param queryRequestDto 查询条件
     * @return 结果
     */
    public DubboResult<ArrayList<Process>> getProcesses(DriverBase driverBase, QueryRequestDto queryRequestDto) {
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult result = this.doRequest(PROCESS_LIST, headerMap, JSON.toJSONString(queryRequestDto), null);
            if (result.success()) {
                DataResponseDto<ResponseDto<Process>> processResponse = (DataResponseDto<ResponseDto<Process>>) result.getObject();
                if (this.resultSuccess(processResponse.getCode())) {
                    List<Process> processList = processResponse.getData().getValue();
                    return DubboResult.newSuccess((ArrayList<Process>) processList);
                }
                return DubboResult.newFailure(PROCESS_LIST.getSystemCode().getCode(), processResponse.getMsg());
            } else {
                return DubboResult.newFailure(PROCESS_LIST.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(PROCESS_LIST.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(PROCESS_LIST.getSystemCode().getCode(), PROCESS_LIST.getSystemCode().getMessage());
    }

    /**
     * 获取一个流程的信息
     * @param driverBase 基类
     * @param templateId 流程id
     * @return 结果
     */
    public DubboResult<Process> getProcessByKey(DriverBase driverBase, Integer templateId) {
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("templateId", templateId);
            InfodatorOriginResult result = this.doRequest(PROCESS_INFO, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto<Process> processResponse = (DataResponseDto<Process>) result.getObject();
                if (this.resultSuccess(processResponse.getCode())) {
                    return DubboResult.newSuccess(processResponse.getData());
                }
                return DubboResult.newFailure(PROCESS_INFO.getSystemCode().getCode(), processResponse.getMsg());
            } else {
                return DubboResult.newFailure(PROCESS_INFO.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(PROCESS_INFO.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(PROCESS_LIST.getSystemCode().getCode(), PROCESS_LIST.getSystemCode().getMessage());
    }

    /**
     * 创建流程
     * @param driverBase 基类
     * @param createProcessDto 流程对象
     * @return 结果
     */
    public DubboResult<Process> addProcess(DriverBase driverBase, CreateProcessDto createProcessDto){
        try {
            createProcessDto.setItemExpectedTime(expectedTime);
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult processResult = this.doRequest(PROCESS_ADD, headerMap, JSON.toJSONString(createProcessDto), null);
            if (processResult.success()) {
                DataResponseDto<Process> processResponse = (DataResponseDto<Process>) processResult.getObject();
                if (this.resultSuccess(processResponse.getCode())) {
                    return DubboResult.newSuccess(processResponse.getData());
                }
                return DubboResult.newFailure(PROCESS_ADD.getSystemCode().getCode(), processResponse.getMsg());
            } else {
                return DubboResult.newFailure(PROCESS_ADD.getSystemCode().getCode(), processResult.getMessage());
            }
        } catch (Exception e) {
            log.error(PROCESS_ADD.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(PROCESS_ADD.getSystemCode().getCode(), PROCESS_ADD.getSystemCode().getMessage());
    }

    /**
     * 创建新流程版本
     * @param driverBase 基类
     * @param addVersionRequestDto 流程版本对象
     * @return 结果
     */
    public DubboResult addProcessVersion(DriverBase driverBase, AddVersionRequestDto addVersionRequestDto){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult versionResult = this.doRequest(PROCESS_ADD_VERSION, headerMap, JSON.toJSONString(addVersionRequestDto), null);
            if (versionResult.success()) {
                DataResponseDto<Process> versionProcess = (DataResponseDto<Process>) versionResult.getObject();
                if (this.resultSuccess(versionProcess.getCode())) {
                    return DubboResult.newSuccess();
                }
                return DubboResult.newFailure(PROCESS_ADD_VERSION.getSystemCode().getCode(), versionProcess.getMsg());
            } else {
                return DubboResult.newFailure(PROCESS_ADD_VERSION.getSystemCode().getCode(), versionResult.getMessage());
            }
        } catch (Exception e) {
            log.error(PROCESS_ADD_VERSION.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(PROCESS_ADD_VERSION.getSystemCode().getCode(), PROCESS_ADD_VERSION.getSystemCode().getMessage());
    }

    /**
     * 新建机器人
     * @param driverBase 基类
     * @param robotRequestDto 机器人对象
     * @return 结果
     */
    public DubboResult<RpaRobot> addRobot(DriverBase driverBase, AddRobotRequestDto robotRequestDto){
        try {
            robotRequestDto.setClientVersion(robotClientVersion);
            robotRequestDto.setOsInfo(robotOsInfo);
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult<String> result = this.doRequest(ROBOT_ADD, headerMap, JSON.toJSONString(robotRequestDto), null);
            if (result.success()) {
                String robotResult = result.getObject();
                if (robotResult.contains("code")) {
                    DataResponseDto robotRes = JSON.parseObject(robotResult, DataResponseDto.class);
                    if (!this.resultSuccess(robotRes.getCode())) {
                        return DubboResult.newFailure(ROBOT_ADD.getSystemCode().getCode(), robotRes.getMsg());
                    }
                }
                RpaRobot rpaRobot = JSON.parseObject(robotResult, RpaRobot.class);
                return DubboResult.newSuccess(rpaRobot);
            } else {
                return DubboResult.newFailure(ROBOT_ADD.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(ROBOT_ADD.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(ROBOT_ADD.getSystemCode().getCode(), ROBOT_ADD.getSystemCode().getMessage());
    }

    /**
     * 删除机器人
     * @param driverBase 基类
     * @param agentId 机器人id
     * @return 结果
     */
    public DubboResult deleteRobotByKey(DriverBase driverBase, String agentId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("agentId", agentId);
            InfodatorOriginResult result = this.doRequest(ROBOT_DELETE, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto<Robot> robotResult = (DataResponseDto<Robot>) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    return DubboResult.newSuccess();
                }
                return DubboResult.newFailure(ROBOT_DELETE.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(ROBOT_DELETE.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(ROBOT_DELETE.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(ROBOT_DELETE.getSystemCode().getCode(), ROBOT_DELETE.getSystemCode().getMessage());
    }

    /**
     * 获取robot列表失败
     * @param driverBase 基类
     * @param queryRequestDto 查询入参
     * @return 结果
     */
    public DubboResult<ArrayList<Robot>> queryRobots(DriverBase driverBase, QueryRequestDto queryRequestDto){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult result = this.doRequest(ROBOT_LIST, headerMap, JSON.toJSONString(queryRequestDto), null);
            if (result.success()) {
                DataResponseDto<ResponseDto<Robot>> robotResult = (DataResponseDto<ResponseDto<Robot>>) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    List<Robot> list = robotResult.getData().getValue();
                    return DubboResult.newSuccess((ArrayList<Robot>) list);
                }
                return DubboResult.newFailure(ROBOT_LIST.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(ROBOT_LIST.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(ROBOT_LIST.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(ROBOT_LIST.getSystemCode().getCode(), ROBOT_LIST.getSystemCode().getMessage());
    }

    /**
     * 获取一个robot信息
     * @param driverBase 基类
     * @param robotId 机器人id
     * @return 结果
     */
    public DubboResult<Robot> queryRobotByKey(DriverBase driverBase, String robotId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("robotId", robotId);
            InfodatorOriginResult result = this.doRequest(ROBOT_INFO, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto<Robot> robotResult = (DataResponseDto<Robot>) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    return DubboResult.newSuccess(robotResult.getData());
                }
                return DubboResult.newFailure(ROBOT_INFO.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(ROBOT_INFO.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(ROBOT_INFO.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(ROBOT_INFO.getSystemCode().getCode(), ROBOT_INFO.getSystemCode().getMessage());
    }

    /**
     * 获取一个活动中的robot信息
     * @param driverBase 基类
     * @param id 机器人id
     * @return 结果
     */
    public DubboResult<Robot> queryLiveRobotByKey(DriverBase driverBase, String id){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("id", id);
            InfodatorOriginResult result = this.doRequest(ROBOT_LIVE_INFO, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto<Robot> robotResult = (DataResponseDto<Robot>) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    return DubboResult.newSuccess(robotResult.getData());
                }
                return DubboResult.newFailure(ROBOT_LIVE_INFO.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(ROBOT_LIVE_INFO.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(ROBOT_LIVE_INFO.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(ROBOT_LIVE_INFO.getSystemCode().getCode(), ROBOT_LIVE_INFO.getSystemCode().getMessage());
    }

    /**
     * 更新robot信息
     * @param driverBase 基类
     * @param rpaRobot 机器人信息
     * @return 结果
     */
    public DubboResult<RpaRobot> updateRobot(DriverBase driverBase, RpaRobot rpaRobot){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("robotId", rpaRobot.getId());
            InfodatorOriginResult result = this.doRequest(ROBOT_UPDATE, headerMap, JSON.toJSONString(rpaRobot), uriParams);
            if (result.success()) {
                DataResponseDto<RpaRobot> robotResult = (DataResponseDto<RpaRobot>) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    return DubboResult.newSuccess(robotResult.getData());
                }
                return DubboResult.newFailure(ROBOT_UPDATE.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(ROBOT_UPDATE.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(ROBOT_UPDATE.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(ROBOT_UPDATE.getSystemCode().getCode(), ROBOT_UPDATE.getSystemCode().getMessage());
    }

    public DubboResult removeRobotFromGroup () {
        return DubboResult.newFailure(ROBOT_UPDATE.getSystemCode().getCode(), "INFODATOR暂不支持将机器人移除组");
    }

    /**
     * 更新机器人的审批状态
     * @param driverBase 基类
     * @param robotId 机器人id
     * @param approveStatus 要修改的机器状态,0-等待中，1-启用，2-禁用
     * @return 结果
     */
    public DubboResult changeRobotStatus(DriverBase driverBase, String robotId, Integer approveStatus){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("robotId", robotId);
            uriParams.put("approveStatus", approveStatus);
            InfodatorOriginResult result = this.doRequest(ROBOT_CHANGE_STATUS, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto<Robot> robotResult = (DataResponseDto<Robot>) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    return DubboResult.newSuccess();
                }
                return DubboResult.newFailure(ROBOT_CHANGE_STATUS.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(ROBOT_CHANGE_STATUS.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(ROBOT_CHANGE_STATUS.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(ROBOT_CHANGE_STATUS.getSystemCode().getCode(), ROBOT_CHANGE_STATUS.getSystemCode().getMessage());
    }

    /**
     * 启动任务
     * @param driverBase 基类
     * @param taskId 任务id
     * @return 结果
     */
    public DubboResult<Integer> startTask(DriverBase driverBase, Integer taskId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("taskId", taskId);
            InfodatorOriginResult result = this.doRequest(TASK_START, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto<List<Integer>> taskResult = (DataResponseDto<List<Integer>>) result.getObject();
                if (this.resultSuccess(taskResult.getCode())) {
                    List<Integer> taskExecutionQueueId = taskResult.getData();
                    return DubboResult.newSuccess(taskExecutionQueueId.get(0));
                }
                return DubboResult.newFailure(TASK_START.getSystemCode().getCode(), taskResult.getMsg());
            } else {
                return DubboResult.newFailure(TASK_START.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_START.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_START.getSystemCode().getCode(), TASK_START.getSystemCode().getMessage());
    }

    /**
     * 取消任务
     * @param driverBase 基类
     * @param taskExecutionQueueId 任务启动时返回的id
     * @return 结果
     */
    public DubboResult cancelTask(DriverBase driverBase, String taskExecutionQueueId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("taskExecutionQueueId", taskExecutionQueueId);
            InfodatorOriginResult result = this.doRequest(TASK_QUEUE_CANCEL, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto<ResponseDto<Task>> taskResult = (DataResponseDto<ResponseDto<Task>>) result.getObject();
                if (this.resultSuccess(taskResult.getCode())) {
                    return DubboResult.newSuccess();
                }
                return DubboResult.newFailure(TASK_QUEUE_CANCEL.getSystemCode().getCode(), taskResult.getMsg());
            } else {
                return DubboResult.newFailure(TASK_QUEUE_CANCEL.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_QUEUE_CANCEL.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_QUEUE_CANCEL.getSystemCode().getCode(), TASK_QUEUE_CANCEL.getSystemCode().getMessage());
    }

    /**
     * 新建任务
     * @param driverBase 基类
     * @param task 任务对象
     * @return 结果
     */
    public DubboResult<Task> addTask(DriverBase driverBase, Task task){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult result = this.doRequest(TASK_ADD_EASY, headerMap, JSON.toJSONString(task), null);
            if (result.success()) {
                DataResponseDto<Task> taskResult = (DataResponseDto<Task>) result.getObject();
                if (this.resultSuccess(taskResult.getCode())) {
                    return DubboResult.newSuccess(taskResult.getData());
                }
                return DubboResult.newFailure(TASK_ADD_EASY.getSystemCode().getCode(), taskResult.getMsg());
            } else {
                return DubboResult.newFailure(TASK_ADD_EASY.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_ADD_EASY.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_ADD_EASY.getSystemCode().getCode(), TASK_ADD_EASY.getSystemCode().getMessage());
    }

    /**
     * 获取任务列表
     * @param driverBase 基类
     * @param requestDto 查询入参
     * @return 结果
     */
    public DubboResult<ArrayList<Task>> getTaskList(DriverBase driverBase, QueryRequestDto requestDto){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult result = this.doRequest(TASK_LIST, headerMap, JSON.toJSONString(requestDto), null);
            if (result.success()) {
                DataResponseDto<ResponseDto<Task>> taskResult = (DataResponseDto<ResponseDto<Task>>) result.getObject();
                if (this.resultSuccess(taskResult.getCode())) {
                    ResponseDto<Task> taskResponseDto = taskResult.getData();
                    return DubboResult.newSuccess((ArrayList<Task>) taskResponseDto.getValue());
                }
                return DubboResult.newFailure(TASK_LIST.getSystemCode().getCode(), taskResult.getMsg());
            } else {
                return DubboResult.newFailure(TASK_LIST.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_LIST.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_LIST.getSystemCode().getCode(), TASK_LIST.getSystemCode().getMessage());
    }

    /**
     * 获取历史任务列表
     * @param driverBase 基类
     * @param requestDto 查询入参
     * @return 结果
     */
    public DubboResult<ArrayList<Task>> getTaskHistoryList(DriverBase driverBase, QueryRequestDto requestDto){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult result = this.doRequest(TASK_HISTORY_LIST, headerMap, JSON.toJSONString(requestDto), null);
            if (result.success()) {
                DataResponseDto<ResponseDto<Task>> taskResult = (DataResponseDto<ResponseDto<Task>>) result.getObject();
                if (this.resultSuccess(taskResult.getCode())) {
                    ResponseDto<Task> taskResponseDto = taskResult.getData();
                    return DubboResult.newSuccess((ArrayList<Task>) taskResponseDto.getValue());
                }
                return DubboResult.newFailure(TASK_HISTORY_LIST.getSystemCode().getCode(), taskResult.getMsg());
            } else {
                return DubboResult.newFailure(TASK_HISTORY_LIST.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_HISTORY_LIST.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_HISTORY_LIST.getSystemCode().getCode(), TASK_HISTORY_LIST.getSystemCode().getMessage());
    }

    /**
     * 获取一个任务信息
     * @param driverBase 基类
     * @param taskId 任务id
     * @return 结果
     */
    public DubboResult<Task> getTaskInfo(DriverBase driverBase, String taskId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("taskId", taskId);
            InfodatorOriginResult result = this.doRequest(TASK_INFO, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto<Task> robotResult = (DataResponseDto<Task>) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    return DubboResult.newSuccess(robotResult.getData());
                }
                return DubboResult.newFailure(TASK_INFO.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(TASK_INFO.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_INFO.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_INFO.getSystemCode().getCode(), TASK_INFO.getSystemCode().getMessage());
    }

    /**
     * 获取任务的执行状态信息
     * @param driverBase 基类
     * @param requestDto 入参
     * @return 结果
     */
    public DubboResult<ArrayList<TaskStatus>> getJobListStatus(DriverBase driverBase, QueryRequestDto requestDto){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult result = this.doRequest(TASK_QUEUE_LIST, headerMap, JSON.toJSONString(requestDto), null);
            if (result.success()) {
                DataResponseDto<ResponseDto<TaskStatus>> robotResult = (DataResponseDto<ResponseDto<TaskStatus>>) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    ResponseDto<TaskStatus> taskStatusResponse = robotResult.getData();
                    List<TaskStatus> taskStatusList = taskStatusResponse.getValue();
                    return DubboResult.newSuccess(Lists.newArrayList(taskStatusList));
                }
                return DubboResult.newFailure(TASK_QUEUE_LIST.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(TASK_QUEUE_LIST.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_QUEUE_LIST.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_QUEUE_LIST.getSystemCode().getCode(), TASK_QUEUE_LIST.getSystemCode().getMessage());
    }

    /**
     * 获取一个执行中的任务信息
     * @param driverBase 基类
     * @param taskQueueId 队列中任务id
     * @return 结果
     */
    public DubboResult<TaskStatus> getJobStatus(DriverBase driverBase, String taskQueueId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("taskQueueId", taskQueueId);
            InfodatorOriginResult result = this.doRequest(TASK_QUEUE_INFO, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto<TaskStatus> robotResult = (DataResponseDto<TaskStatus>) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    return DubboResult.newSuccess(robotResult.getData());
                }
                return DubboResult.newFailure(TASK_QUEUE_INFO.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(TASK_QUEUE_INFO.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_QUEUE_INFO.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_QUEUE_INFO.getSystemCode().getCode(), TASK_QUEUE_INFO.getSystemCode().getMessage());
    }

    /**
     * 暂停任务
     * @param driverBase 基类
     * @param taskExecutionQueueId 启动任务后的id
     * @return 结果
     */
    public DubboResult pauseJob(DriverBase driverBase, String taskExecutionQueueId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("taskExecutionQueueId", taskExecutionQueueId);
            InfodatorOriginResult result = this.doRequest(TASK_PAUSE, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto robotResult = (DataResponseDto) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    return DubboResult.newSuccess();
                }
                return DubboResult.newFailure(TASK_PAUSE.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(TASK_PAUSE.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_PAUSE.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_PAUSE.getSystemCode().getCode(), TASK_PAUSE.getSystemCode().getMessage());
    }

    /**
     * 恢复暂停任务
     * @param driverBase 基类
     * @param taskExecutionQueueId 启动任务后的id
     * @return 结果
     */
    public DubboResult resumeJob(DriverBase driverBase, String taskExecutionQueueId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("taskExecutionQueueId", taskExecutionQueueId);
            InfodatorOriginResult result = this.doRequest(TASK_RESUME, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto robotResult = (DataResponseDto) result.getObject();
                if (this.resultSuccess(robotResult.getCode())) {
                    return DubboResult.newSuccess();
                }
                return DubboResult.newFailure(TASK_RESUME.getSystemCode().getCode(), robotResult.getMsg());
            } else {
                return DubboResult.newFailure(TASK_RESUME.getSystemCode().getCode(), result.getMessage());
            }
        } catch (Exception e) {
            log.error(TASK_RESUME.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(TASK_RESUME.getSystemCode().getCode(), TASK_RESUME.getSystemCode().getMessage());
    }

    /**
     * 新建组
     * @param driverBase 基类
     * @param groupRequestDto 组对象
     * @return 结果
     */
    public DubboResult<GroupResponseDto> addGroup(DriverBase driverBase, GroupRequestDto groupRequestDto){
        try {
            groupRequestDto.setColorExpress(groupColor);
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            InfodatorOriginResult result = this.doRequest(GROUP_ADD, headerMap, JSON.toJSONString(groupRequestDto), null);
            if (result.success()) {
                DataResponseDto<GroupResponseDto> groupResponse = (DataResponseDto<GroupResponseDto>) result.getObject();
                if (this.resultSuccess(groupResponse.getCode())) {
                    return DubboResult.newSuccess(groupResponse.getData());
                }
                return DubboResult.newFailure(GROUP_ADD.getSystemCode().getCode(), groupResponse.getMsg());
            }
            return DubboResult.newFailure(GROUP_ADD.getSystemCode().getCode(), result.getMessage());
        } catch (Exception e) {
            log.error(GROUP_ADD.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(GROUP_ADD.getSystemCode().getCode(), GROUP_ADD.getSystemCode().getMessage());
    }

    /**
     * 查询一个组信息
     * @param driverBase 基类
     * @param groupId 组id
     * @return 结果
     */
    public DubboResult<GroupResponseDto> getGroupByKey(DriverBase driverBase, Integer groupId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("groupId", groupId);
            InfodatorOriginResult result = this.doRequest(GROUP_INFO, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto<GroupResponseDto> groupResponse = (DataResponseDto<GroupResponseDto>) result.getObject();
                if (this.resultSuccess(groupResponse.getCode())) {
                    return DubboResult.newSuccess(groupResponse.getData());
                }
                return DubboResult.newFailure(GROUP_INFO.getSystemCode().getCode(), groupResponse.getMsg());
            }
            return DubboResult.newFailure(GROUP_INFO.getSystemCode().getCode(), result.getMessage());
        } catch (Exception e) {
            log.error(GROUP_INFO.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(GROUP_INFO.getSystemCode().getCode(), GROUP_INFO.getSystemCode().getMessage());
    }

    /**
     * 插叙所有组信息
     * @param driverBase 基类
     * @param queryRequestDto 查询入参
     * @param uuid
     * @return 结果
     */
    public DubboResult<ArrayList<GroupResponseDto>> getAllGroupName(DriverBase driverBase, QueryRequestDto queryRequestDto, String uuid){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("uuid", uuid);
            InfodatorOriginResult result = this.doRequest(GROUP_GET_ALL_NAME, headerMap, JSON.toJSONString(queryRequestDto), uriParams);
            if (result.success()) {
                DataResponseDto<ResponseDto<GroupResponseDto>> groupResponse = (DataResponseDto<ResponseDto<GroupResponseDto>>) result.getObject();
                if (this.resultSuccess(groupResponse.getCode())) {
                    List<GroupResponseDto> groupList = groupResponse.getData().getValue();
                    return DubboResult.newSuccess((ArrayList<GroupResponseDto>) groupList);
                }
                return DubboResult.newFailure(GROUP_GET_ALL_NAME.getSystemCode().getCode(), groupResponse.getMsg());
            }
            return DubboResult.newFailure(GROUP_GET_ALL_NAME.getSystemCode().getCode(), result.getMessage());
        } catch (Exception e) {
            log.error(GROUP_GET_ALL_NAME.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(GROUP_GET_ALL_NAME.getSystemCode().getCode(), GROUP_GET_ALL_NAME.getSystemCode().getMessage());
    }

    /**
     * 更新组
     * @param driverBase 基类
     * @param groupRequestDto 组对象
     * @return 结果
     */
    public DubboResult<GroupResponseDto> updateGroup(DriverBase driverBase, GroupRequestDto groupRequestDto){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("groupId", groupRequestDto.getId());
            InfodatorOriginResult result = this.doRequest(GROUP_UPDATE, headerMap, JSON.toJSONString(groupRequestDto), uriParams);
            if (result.success()) {
                DataResponseDto<GroupResponseDto> groupResponse = (DataResponseDto<GroupResponseDto>) result.getObject();
                if (this.resultSuccess(groupResponse.getCode())) {
                    return DubboResult.newSuccess(groupResponse.getData());
                }
                return DubboResult.newFailure(GROUP_UPDATE.getSystemCode().getCode(), groupResponse.getMsg());
            }
            return DubboResult.newFailure(GROUP_UPDATE.getSystemCode().getCode(), result.getMessage());
        } catch (Exception e) {
            log.error(GROUP_UPDATE.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(GROUP_UPDATE.getSystemCode().getCode(), GROUP_UPDATE.getSystemCode().getMessage());
    }

    /**
     * 删除组
     * @param driverBase 基类
     * @param groupId 组id
     * @return 结果
     */
    public DubboResult deleteGroup(DriverBase driverBase, String groupId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("groupId", groupId);
            InfodatorOriginResult result = this.doRequest(GROUP_DELETE, headerMap, null, uriParams);
            if (result.success()) {
                DataResponseDto groupResponse = (DataResponseDto) result.getObject();
                if (this.resultSuccess(groupResponse.getCode())) {
                    return DubboResult.newSuccess(groupResponse.getMsg());
                }
                return DubboResult.newFailure(GROUP_DELETE.getSystemCode().getCode(), groupResponse.getMsg());
            }
            return DubboResult.newFailure(GROUP_DELETE.getSystemCode().getCode(), result.getMessage());
        } catch (Exception e) {
            log.error(GROUP_DELETE.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(GROUP_DELETE.getSystemCode().getCode(), GROUP_DELETE.getSystemCode().getMessage());
    }

    /**
     * 转移组
     * @param driverBase 基类
     * @param groupId 组id
     * @param newGroupId 新组id
     * @return 结果
     */
    public DubboResult deleteAndRemoveGroup(DriverBase driverBase, String groupId, String newGroupId){
        try {
            Map<String, Object> headerMap = this.getHeaderMap(driverBase, true);
            Map<String, Object> uriParams = new HashMap<>();
            uriParams.put("groupId", groupId);
            uriParams.put("newGroupId ", newGroupId);
            InfodatorOriginResult result = this.doRequest(GROUP_DELETE_REMOVE, headerMap, null, uriParams);
            if (result.success()) {
                return DubboResult.newSuccess(result.getObject());
            }
            return DubboResult.newFailure(GROUP_DELETE_REMOVE.getSystemCode().getCode(), result.getMessage());
        } catch (Exception e) {
            log.error(GROUP_DELETE_REMOVE.getSystemCode().getMessage(), e);
        }
        return DubboResult.newFailure(GROUP_DELETE_REMOVE.getSystemCode().getCode(), GROUP_DELETE_REMOVE.getSystemCode().getMessage());
    }

    private Map<String, Object> getHeaderMap(DriverBase driverBase, boolean needToken) throws DriverBusinessException {
        Map<String, Object> headers = new HashMap<>();
        if (StringUtils.isNotEmpty(driverBase.getGroupKey())) {
            headers.put("X-INFODATOR-OrganizationUnitId", driverBase.getGroupKey());
        }
        if (needToken) {
            String token = this.getTenantToken(driverBase);
            if (token.startsWith("Bearer")) {
                headers.put("Authorization", token);
            } else {
                headers.put("Authorization", "Bearer " + token);
            }
        }
        return headers;
    }

    //非form-data请求
    private InfodatorOriginResult doRequest(InfodatorApiEnum api, Map<String, Object> headers, String requestBody, Map<String, Object> uriParams){
        String url = api.getUrl(infodatorServiceUrl, uriParams);
        TaieHttpResponse response = okHttpService.doRequest(api.getHttpMethod(), url, headers, requestBody);
        return getResponseInGeneric(api, response);
    }

    //form-data请求
    private InfodatorOriginResult doFormRequest(InfodatorApiEnum api, Map<String, Object> headers, File file, Map<String, Object> uriParams){
        String url = api.getUrl(infodatorServiceUrl, uriParams);
        TaieHttpResponse response = okHttpService.doFormRequest(api.getHttpMethod(), url, headers, file, INFODATOR_FORM_FILE_KEY);
        return getResponseInGeneric(api, response);
    }

    private InfodatorOriginResult doDownloadRequest(InfodatorApiEnum api, Map<String, Object> headers, Map<String, Object> uriParams, String destFileDir, String destFileName){
        String url = api.getUrl(infodatorServiceUrl, uriParams);
        TaieHttpResponse response = okHttpService.syncDownload(url, headers, destFileDir, destFileName);
        return getResponseInGeneric(api, response);
    }

    private InfodatorOriginResult getResponseInGeneric(InfodatorApiEnum api, TaieHttpResponse response) {
        InfodatorOriginResult rst = InfodatorOriginResult.builder()
                .success(response.success())
                .retCode(response.getHttpRetCode())
                .build();
        try {
            if (response.success()) {
                if(Objects.nonNull(api.getResponseClass())){
                    if(api.getResponseClass().equals(String.class)){
                        rst.setObject(response.getResult());
                    } else {
                        rst.setObject(JSON.parseObject(response.getResult(), api.getResponseClass()));
                    }
                } else if (Objects.nonNull(api.getResponseTypeReference())) {
                    rst.setObject(JSON.parseObject(response.getResult(), api.getResponseTypeReference()));
                }
            } else if (StringUtils.isNotEmpty(response.getResult())) {
                rst.setMessage(response.getResult());
            }
        } catch (Exception e) {
            log.error("结果转换失败！", e);
            rst.setSuccess(false);
            rst.setMessage(INFODATOR_RESPONSE_DATA_ERROR.toString(api.name(), response.getResult()));
        }
        return rst;
    }

    private String getTenantToken(DriverBase driverBase) throws DriverBusinessException {
        RMap<String, String> tenantTokenMap = this.redissonClient.getMap(DriverConstant.INFODATOR_TENANT_TOKEN_CACHE);
        String tenantToken;
        if (!tenantTokenMap.containsKey(driverBase.getTenantKey())) {
            tenantToken = this.authenticate(driverBase);
            tenantTokenMap.put(driverBase.getTenantKey(), tenantToken);
            tenantTokenMap.expire(DriverConstant.INFODATOR_TENANT_TOKEN_CACHE_EXPIRATION, TimeUnit.MINUTES);
        } else {
            tenantToken = tenantTokenMap.get(driverBase.getTenantKey());
        }
        return tenantToken;
    }

    /**
     * 获取中控租户相关信息
     */
    public OcTenantDetailDto getOcTenantInfo(DriverBase driverBase) throws DriverBusinessException {
        OcTenantFilterDto filter = OcTenantFilterDto.ocTenantFilterBuilder()
                .ocTenantId(Long.parseLong(driverBase.getTenantKey()))
                .build();
        DubboResult<ArrayList<OcTenantDetailDto>> result = tenantManage.queryTenantDetail(filter);
        if(!result.success() || CollectionUtils.isEmpty(result.getObject())){
            log.error(AUTHENTICATE.getSystemCode().getMessage(), result.getMessage() + "或根据中控信息查询结果为空");
            throw new DriverBusinessException("查询到的中控租户信息为空:" + JSON.toJSONString(driverBase));
        }
        return result.getObject().get(0);
    }

    private boolean resultSuccess(Integer code){
        return code != null && code == 0;
    }
}
