package com.njcn.device.pq.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.biz.utils.DeviceEnumUtil;
import com.njcn.device.pq.api.NewStationClient;
import com.njcn.device.pq.pojo.po.NewStation;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 新能源场站高低电压穿越管理熔断降级
 * @author guofeihu
 * @date 2024-08-14
 */
@Slf4j
@Component
public class NewStationClientFallbackFactory implements FallbackFactory<NewStationClient> {
    @Override
    public NewStationClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = DeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new NewStationClient() {
            @Override
            public HttpResult<NewStation> selectById(String id) {
                log.error("{}异常，降级处理，异常为：{}", "根据ID获取新能源场站高低电压穿越表信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}

