package com.njcn.advance.api.fallback;

import com.njcn.advance.api.EventWaveAnalysisFeignClient;
import com.njcn.advance.pojo.dto.waveAnalysis.EntityAdvancedData;
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.event.utils.EventlEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


/**
 * @author wr
 */
@Slf4j
@Component
public class EventWaveAnalysisFeignClientFallbackFactory implements FallbackFactory<EventWaveAnalysisFeignClient> {
    @Override
    public EventWaveAnalysisFeignClient 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 EventWaveAnalysisFeignClient() {
            @Override
            public HttpResult<EntityAdvancedData> analysis(String eventIndex) {
                log.error("{}异常，降级处理，异常为：{}", "波形高级分析", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<EntityAdvancedData> analysisWlEvent(String eventIndex) {
                log.error("{}异常，降级处理，异常为：{}", "物联测点波形高级分析异常", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
