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.PqDataVerifyCountClient;
import com.njcn.device.pq.api.PqDevTypeClient;
import com.njcn.device.pq.pojo.param.LineBaseQueryParam;
import com.njcn.device.pq.pojo.po.PqDevType;
import com.njcn.device.pq.pojo.vo.OnlineMonitorVo;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class dataVerifyCountFallbackFactory implements FallbackFactory<PqDataVerifyCountClient> {
    @Override
    public PqDataVerifyCountClient 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 PqDataVerifyCountClient()
        {

            @Override
            public HttpResult<List<OnlineMonitorVo>> getAnomalousData(LineBaseQueryParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取有异常数据的监测点相关信息异常", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };


    }
}
