package com.njcn.prepare.harmonic.api.line.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.prepare.harmonic.api.line.HarmOverLimitFeignClient;
import com.njcn.prepare.harmonic.api.line.TargetWarnFeignClient;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.utils.PrepareEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author xuyang
 */
@Slf4j
@Component
public class TargetWarnFeignClientFallbackFactory implements FallbackFactory<TargetWarnFeignClient> {

    @Override
    public TargetWarnFeignClient create(Throwable throwable) {
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException)throwable.getCause();
            exceptionEnum = PrepareEnumUtil.getExceptionEnum(businessException.getResult());
        }

        Enum<?> finalExceptionEnum = exceptionEnum;
        return new TargetWarnFeignClient() {

            @Override
            public HttpResult<Boolean> targetDiff(LineParam lineParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测点稳态指标差值日统计:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> alarmDay(LineParam lineParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测点指标告警日统计(新):  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> alarmMonth(LineParam lineParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测点指标告警月统计(新):  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
