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.AlarmClient;
import com.njcn.device.pq.api.LineIntegrityClient;
import com.njcn.device.pq.pojo.dto.LineDataQualityDTO;
import com.njcn.device.pq.pojo.param.LineBaseQueryParam;
import com.njcn.device.pq.pojo.param.LineDataQualityParam;
import com.njcn.device.pq.pojo.po.RStatIntegrityD;
import com.njcn.device.pq.pojo.vo.AlarmStrategyVO;
import com.njcn.device.pq.pojo.vo.OnlineMonitorVo;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 告警管理熔断降级
 * @author yzh
 * @date 2022/9/19
 */
@Slf4j
@Component
public class LineIntegrityClientFallbackFactory implements FallbackFactory<LineIntegrityClient> {
    @Override
    public LineIntegrityClient 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 LineIntegrityClient() {
            @Override
            public HttpResult<List<RStatIntegrityD>> getIntegrityByLineIds(List<String> lineIds,String startTime,String endTime) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点数据完整性", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Float> getTotalIntegrityByLineIds(LineBaseQueryParam param) {
                log.error("{}异常，降级处理，异常为：{}", "通过监测点集合查询总监测点数据完整性", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<LineDataQualityDTO>> getLineDataQuality(LineDataQualityParam lineDataQualityParam) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点的在线率和数据完成性", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<OnlineMonitorVo>> getNoData(LineBaseQueryParam param) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点数据完成性为0的数据", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}

