package com.njcn.prepare.harmonic.api.line.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.line.RAlarmCountFeignClient;
import com.njcn.prepare.harmonic.api.line.RStatEventFeignClient;
import com.njcn.prepare.harmonic.pojo.param.OrgParam;
import com.njcn.prepare.harmonic.utils.PrepareEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author zbj
 */
@Slf4j
@Component
public class RAlarmCountFeignClientFallbackFactory implements FallbackFactory<RAlarmCountFeignClient> {

    @Override
    public RAlarmCountFeignClient 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 RAlarmCountFeignClient() {

            @Override
            public HttpResult<Boolean> rAlarmCountHandler(OrgParam orgParam) {
                log.error("{}异常，降级处理，异常为：{}", "指标告警统计:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}
