package com.njcn.csharmonic.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.csharmonic.api.CsHarmonicPlanFeignClient;
import com.njcn.csharmonic.pojo.po.CsHarmonicPlan;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xy
 */
@Slf4j
@Component
public class CsHarmonicPlanFeignClientFallbackFactory implements FallbackFactory<CsHarmonicPlanFeignClient> {
    @Override
    public CsHarmonicPlanFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (cause.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) cause.getCause();
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new CsHarmonicPlanFeignClient() {

            @Override
            public HttpResult<CsHarmonicPlan> getById(String id) {
                log.error("{}异常，降级处理，异常为：{}","根据ID查询稳态指标方案异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsHarmonicPlan>> getByName(String name) {
                log.error("{}异常，降级处理，异常为：{}","根据名称查询稳态指标方案异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
