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.CsDeviceUserFeignClient;
import com.njcn.csdevice.pojo.param.UserDevParam;
import com.njcn.csdevice.pojo.po.CsDeviceUserPO;
import com.njcn.csdevice.pojo.vo.DevUserVO;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xy
 */
@Slf4j
@Component
public class CsDeviceUserClientFallbackFactory implements FallbackFactory<CsDeviceUserFeignClient> {
    @Override
    public CsDeviceUserFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (cause.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) cause.getCause();
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new CsDeviceUserFeignClient() {
            @Override
            public HttpResult<Boolean> add(List<CsDeviceUserPO> list) {
                log.error("{}异常，降级处理，异常为：{}","批量新增用户设备关系",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

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

            @Override
            public HttpResult<List<String>> findDevByUserId(String id) {
                log.error("{}异常，降级处理，异常为：{}","根据用户Id查询设备集合",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> channelDevByUserId(UserDevParam param) {
                log.error("{}异常，降级处理，异常为：{}","根据用户Id解绑设备集合异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<DevUserVO> queryUserById(String devId) {
                log.error("{}异常，降级处理，异常为：{}","获取设备主/子用户数据异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<CsDeviceUserPO>> getList(UserDevParam param) {
                log.error("{}异常，降级处理，异常为：{}","根据设备集合获取数据异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<String>> getIdList(String param) {
                log.error("{}异常，降级处理，异常为：{}","获取事件id集合数据异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}
