package com.njcn.csdevice.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.csdevice.api.AppProjectFeignClient;
import com.njcn.csdevice.pojo.param.AppProjectAddParm;
import com.njcn.csdevice.pojo.po.AppProjectPO;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

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

            @Override
            public HttpResult<List<AppProjectPO>> queryProjectById(List<String> ids) {
                log.error("{}异常，降级处理，异常为：{}","获取项目详情接口异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<AppProjectPO> getProjectByName(String name) {
                log.error("{}异常，降级处理，异常为：{}","根据项目名称获取项目信息异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<AppProjectPO> addPortableProject(AppProjectAddParm appProjectAddParm) {
                log.error("{}异常，降级处理，异常为：{}","新增便携式项目",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
