package com.docus.server.report.api.impl;

import cn.hutool.http.HttpUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.infrastructure.web.api.ResultCode;
import com.docus.server.report.api.TaskDistributeService;
import com.docus.server.report.api.dto.ReportDownTwoDto;
import com.docus.server.report.config.ApplicationBusinessConfig;
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 ApplicationBusinessConfig applicationBusinessConfig;


    @Override
    public ReportDownTwoDto getNoViewTaskByCollectorId(String collectorId) {
        String getTaskUrl = applicationBusinessConfig.getGetTaskUrl();
        if (Func.isBlank(getTaskUrl)) {
            log.warn("获取任务信息地址未配置！");
            return null;
        }
        getTaskUrl = applicationBusinessConfig.getGetTaskUrl() + "?collectid=" + collectorId;
        String result = HttpUtil.get(getTaskUrl);
        CommonResult<ReportDownTwoDto> reportDownTwoDtoCommonResult = Func.readJson(result, new TypeReference<CommonResult<ReportDownTwoDto>>() {
        });
        if (reportDownTwoDtoCommonResult == null || reportDownTwoDtoCommonResult.getCode().equals(ResultCode.FAILED.getCode()) || reportDownTwoDtoCommonResult.getData() == null) {
            log.debug("未查询到任务数据！");
            return null;
        }
        return reportDownTwoDtoCommonResult.getData();
    }

    public static void main(String[] args) {
        String path="111.jpg";
        String fh=".jpg";
        System.out.println(path.endsWith(fh));
    }
    @Override
    public CommonResult<String> cancel(Long taskId) {
        String taskCancelUrl = applicationBusinessConfig.getGetTaskCancelUrl();
        if (Func.isBlank(taskCancelUrl)) {
            log.warn("任务作废地址未配置！");
            return null;
        }
        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>>() {
        });
    }
}
