package com.njcn.event.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.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.event.api.ReportFeignClient;
import com.njcn.event.utils.EventlEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * @author wr
 * @description
 * @date 2024/1/3 10:02
 */
@Slf4j
@Component
public class ReportFeignClientFallbackFactory implements FallbackFactory<ReportFeignClient> {
    @Override
    public ReportFeignClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = EventlEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new ReportFeignClient() {

            @Override
            public HttpResult<String> appEventExport(DeviceInfoParam.BusinessParam businessParam) {
                log.error("{}异常，降级处理，异常为：{}", "暂态报告导出", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
