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.CsEdDataFeignClient;
import com.njcn.csdevice.pojo.param.CsEdDataAddParm;
import com.njcn.csdevice.pojo.param.CsEdDataQueryParm;
import com.njcn.csdevice.pojo.po.CsEdDataPO;
import com.njcn.csdevice.pojo.vo.CsEdDataVO;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

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

            @Override
            public HttpResult<CsEdDataVO> findByDevTypeId(String devType) {
                log.error("{}异常，降级处理，异常为：{}","根据装置型号获取装置类型",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsEdDataPO>> getAll(String devType) {
                log.error("{}异常，降级处理，异常为：{}","根据装置型号获取装置类型",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsEdDataPO> getById(String id) {
                log.error("{}异常，降级处理，异常为：{}","根据id获取装置类型",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsEdDataPO> addEdDataFirst(CsEdDataAddParm param) {
                log.error("{}异常，降级处理，异常为：{}","首次录入版本信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsEdDataPO> findByCondition(CsEdDataQueryParm param) {
                log.error("{}异常，降级处理，异常为：{}","根据条件查询是否存在数据",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsEdDataPO> findByPath(String filePath) {
                log.error("{}异常，降级处理，异常为：{}","根据文件路径获取版本信息",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
