package com.docus.server.archive.rpc.impl;

import cn.hutool.http.HttpUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.server.archive.config.DocusServerUrlConfig;
import com.docus.server.archive.rpc.TaskDistributeService;
import com.docus.server.archive.rpc.dto.ReportDownTwoDto;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 无视图任务服务
 *
 * @author wyb
 */
@Component
@Slf4j
public class TaskDistributeServiceImpl implements TaskDistributeService {
    @Resource
    private DocusServerUrlConfig serverUrlConfig;

    @Override
    public CommonResult<ReportDownTwoDto> getNoViewTaskByCollectorId(String collectorId) {
        String getTaskUrl = serverUrlConfig.getTaskDistributeServerUrl() + "api/noviewtask/GetTask?collectid=" + collectorId;
        String result = HttpUtil.get(getTaskUrl);
        return Func.readJson(result, new TypeReference<CommonResult<ReportDownTwoDto>>() {
        });
    }


    @Override
    public CommonResult<String> cancel(Long taskId) {
        String taskCancelUrl = serverUrlConfig.getTaskDistributeServerUrl() + "api/collector/task/cancel";
        Map<String, Object> param = new HashMap<>(1);
        param.put("taskId", taskId);
        log.info("任务作废 地址{}，参数：{}", taskCancelUrl, param);
        String resp = HttpUtil.post(taskCancelUrl, Func.toJson(param));
        log.info("任务作废结果：{}", resp);
        return Func.readJson(resp, new TypeReference<CommonResult<String>>() {
        });
    }
}
