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.CsOverLimitClient;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.biz.utils.DeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 告警管理熔断降级
 * @author yzh
 * @date 2022/9/19
 */
@Slf4j
@Component
public class CsOverLimitFallbackFactory implements FallbackFactory<CsOverLimitClient> {
    @Override
    public CsOverLimitClient 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 CsOverLimitClient() {
            @Override
            public HttpResult<List<Overlimit>> getOverLimitByLineIds(List<String> lineIds) {
                log.error("{}异常，降级处理，异常为：{}", "监测点限值", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<Map<String, Object>>> getLimitMapsByLineIds(List<String> lineIds) {
                log.error("{}异常，降级处理，异常为：{}", "获取监测点限值map", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}

