package com.njcn.job.api.fallback;

import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.job.api.JobFeignClient;
import com.njcn.job.model.XxlJobInfo;
import com.xxl.job.core.biz.model.ReturnT;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年04月18日 14:44
 */
@Slf4j
@Component
public class JobFeignClientFallbackFactory implements FallbackFactory<JobFeignClient> {

    /**
     * 输出远程请求接口异常日志
     *
     * @param cause RPC请求异常
     */
    @Override
    public JobFeignClient create(Throwable cause) {

        return new JobFeignClient() {
            @Override
            public HttpResult<String> addJob(XxlJobInfo jobInfo) {
                log.error("{}异常，降级处理，异常为：{}", "新增定时任务", cause.toString());

                return new HttpResult(ReturnT.FAIL_CODE, "新增定时任务降级失败");
            }

            @Override
            public HttpResult<String> updateJob(XxlJobInfo jobInfo) {
                log.error("{}异常，降级处理，异常为：{}", "修改定时任务", cause.toString());

                return new HttpResult(ReturnT.FAIL_CODE, "修改定时任务降级失败");
            }

            @Override
            public HttpResult<String> removeJob(int id) {
                log.error("{}异常，降级处理，异常为：{}", "删除定时任务", cause.toString());

                return new HttpResult(ReturnT.FAIL_CODE, "删除定时任务降级失败");
            }

            @Override
            public HttpResult<String> stopJob(int id) {
                log.error("{}异常，降级处理，异常为：{}", "停止定时任务", cause.toString());

                return new HttpResult(ReturnT.FAIL_CODE, "停止定时任务降级失败");
            }

            @Override
            public HttpResult<String> startTask(int id) {
                log.error("{}异常，降级处理，异常为：{}", "启动任务", cause.toString());
                return new HttpResult(ReturnT.FAIL_CODE, "启动任务降级失败");
            }
        };
    }
}
