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

@Slf4j
@Component
public class ROperatingIndexFeignClientFallbackFactory implements FallbackFactory<ROperatingIndexFeignClient> {
    @Override
    public ROperatingIndexFeignClient 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 ROperatingIndexFeignClient() {

            @Override
            public HttpResult<Boolean> primaryGridDataHanlder(PrimaryGridParam primaryGridParam) {
                log.error("{}异常，降级处理，异常为：{}", "单位标数据质量_统计:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> primaryGridDataIntegrityHanlder(PrimaryGridDataIntegrityParam primaryGridDataIntegrityParam) {
                log.error("{}异常，降级处理，异常为：{}", "终端通信管理_日表:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
