package com.njcn.device.pms.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.pms.api.LineIntegrityDataClient;
import com.njcn.device.pms.pojo.param.*;
import com.njcn.device.pms.utils.PmsDeviceEnumUtil;
import com.njcn.device.pq.pojo.po.RStatIntegrityD;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: wr
 * @Date: 2024/5/28 9:41
 */
@Slf4j
@Component
public class LineIntegrityDataFallbackFactory implements FallbackFactory<LineIntegrityDataClient> {

    @Override
    public LineIntegrityDataClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = PmsDeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new LineIntegrityDataClient()
        {
            @Override
            public HttpResult<List<RStatIntegrityD>> getIntegrityData(DataQualityDetailsParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点数据完整率", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }


        };
    }
}
