package com.njcn.prepare.harmonic.api.newalgorithm.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.prepare.harmonic.api.newalgorithm.RMpEventDetailFeignClient;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RMpEventDetailFeignClientFallbackFactory implements FallbackFactory<RMpEventDetailFeignClient> {
    @Override
    public RMpEventDetailFeignClient 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 RMpEventDetailFeignClient() {
            @Override
            public HttpResult<String> eventdetailHandler(LineParam lineParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测点暂态指标处理:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
