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

/**
 * @author xuyang
 */
@Slf4j
@Component
public class HarmOverLimitFeignClientFallbackFactory implements FallbackFactory<HarmOverLimitFeignClient> {

    @Override
    public HarmOverLimitFeignClient 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 HarmOverLimitFeignClient() {

            @Override
            public HttpResult<Boolean> harmonicDetailDay1(LineParam lineParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测点稳态指标超标明细日表1:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> harmonicDetailDay2(LineParam lineParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测点稳态指标超标明细日表2:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> harmonicDetailMonth1(LineParam lineParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测点稳态指标超标明细月表1:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> harmonicDetailMonth2(LineParam lineParam) {
                log.error("{}异常，降级处理，异常为：{}", "监测点稳态指标超标明细月表1:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> harmonicThdDay(LineParam lineParam) {
                log.error("{}异常，降级处理，异常为：{}", "谐波畸变率日表:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
