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.DeviceMessageFeignClient;
import com.njcn.csdevice.param.DeviceMessageParam;
import com.njcn.csdevice.param.LineInfoParam;
import com.njcn.user.pojo.po.User;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

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

            @Override
            public HttpResult<List<String>> getEventUserByDeviceId(String devId, Boolean isAdmin) {
                log.error("{}异常，降级处理，异常为：{}","根据设备获取需要推送的用户id数据异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<User>> getSendUserByType(DeviceMessageParam param) {
                log.error("{}异常，降级处理，异常为：{}","根据事件类型和用户id查询打开推送的用户信息数据异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> getLineInfo(LineInfoParam param) {
                log.error("{}异常，降级处理，异常为：{}","获取监测点信息数据异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
