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.CsLineTopologyFeignClient;
import com.njcn.csdevice.pojo.po.AppLineTopologyDiagramPO;
import com.njcn.csdevice.pojo.vo.AppTopologyDiagramVO;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

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

            @Override
            public HttpResult<String> addList(List<AppLineTopologyDiagramPO> list) {
                log.error("{}异常，降级处理，异常为：{}","新增监测点拓扑图关系表",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<AppTopologyDiagramVO> queryTopologyDiagram(String devId) {
                log.error("{}异常，降级处理，异常为：{}","查询装置拓扑图异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}
