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

import java.util.List;

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

            @Override
            public HttpResult<Boolean> add(List<CsDeviceRegistry> list) {
                log.error("{}异常，降级处理，异常为：{}","批量新增设备注册记录异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> update(CsDeviceRegistry csDeviceRegistry) {
                log.error("{}异常，降级处理，异常为：{}","修改设备注册记录异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> delete(String id) {
                log.error("{}异常，降级处理，异常为：{}","删除设备注册记录异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsDeviceRegistry>> queryByCurrentNdid(String currentNdid) {
                log.error("{}异常，降级处理，异常为：{}","根据currentNdid查询设备注册记录异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<CsDeviceRegistry> queryByCurrentNdidAndClDid(String currentNdid, Integer clDid) {
                log.error("{}异常，降级处理，异常为：{}","根据currentNdid和clDid查询设备注册记录数据异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> updateIsAccessByCurrentNdid(String currentNdid, Integer isAccess) {
                log.error("{}异常，降级处理，异常为：{}","根据currentNdid修改isAccess异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
