package com.njcn.system.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.system.api.PqDashboardPageFeignClient;
import com.njcn.system.pojo.po.PqDashboardPage;
import com.njcn.system.utils.SystemEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author denghuajun
 * @version 1.0.0
 * @date 2022年01月05日 15:08
 */
@Slf4j
@Component
public class PqDashboardPageFeignClientFallbackFactory implements FallbackFactory<PqDashboardPageFeignClient> {


    /**
     * 输出远程请求接口异常日志
     * @param cause RPC请求异常
     * @return
     */
    @Override
    public PqDashboardPageFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if(cause.getCause() instanceof BusinessException){
            BusinessException businessException = (BusinessException) cause.getCause();
            exceptionEnum = SystemEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new PqDashboardPageFeignClient() {

            @Override
            public HttpResult<List<PqDashboardPage>> getDashboardPageByUserId(String id, boolean state) {
                log.error("{}异常，降级处理，异常为：{}","根据用户id查询用户驾驶舱异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
