package com.njcn.device.pq.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.device.biz.utils.DeviceEnumUtil;
import com.njcn.device.pq.api.DeviceUnitClient;
import com.njcn.device.biz.pojo.po.PqsDeviceUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author denghuajun
 * @date 2022/2/28
 */
@Slf4j
@Component
public class DeviceUnitFallbackFactory implements feign.hystrix.FallbackFactory<DeviceUnitClient> {
    @Override
    public DeviceUnitClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = DeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new DeviceUnitClient() {

            @Override
            public HttpResult<PqsDeviceUnit> lineUnitDetail(String lineID) {
                log.error("{}异常，降级处理，异常为：{}", "根据监测点id获取数据单位", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
