package com.njcn.energy.pojo.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.energy.pojo.api.EleIntegrityFeignClient;
import com.njcn.energy.pojo.utils.EnergyEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2022/4/20 15:09
 */
@Slf4j
@Component
public class EleIntegrityFallbackFactory implements FallbackFactory<EleIntegrityFeignClient>  {

    @Override
    public EleIntegrityFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if(cause.getCause() instanceof BusinessException){
            BusinessException businessException = (BusinessException) cause.getCause();
            exceptionEnum = EnergyEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new EleIntegrityFeignClient() {
            @Override
            public HttpResult<List<String>> getPowerLineId() {
                log.error("{}异常，降级处理，异常为：{}","获取在线用采监测点",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getAirLineId() {
                log.error("{}异常，降级处理，异常为：{}","获取在线空调监测点",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
