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

@Slf4j
@Component
public class RQualityParameterMFeignClientFallbackFactory implements FallbackFactory<RQualityParameterMFeignClient> {
    @Override
    public RQualityParameterMFeignClient create(Throwable throwable) {
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException)throwable.getCause();
            exceptionEnum = PrepareEnumUtil.getExceptionEnum(businessException.getResult());
        }

        Enum<?> finalExceptionEnum = exceptionEnum;
        return new RQualityParameterMFeignClient() {

            @Override
            public HttpResult<Boolean> handlerMonitorAbnormal(LineParam jobParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测指标数据质量问题汇总统计(r_quality_parameter_m表数据):  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
