package com.njcn.prepare.harmonic.api.specialanalysis.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.specialanalysis.SpecialAnalysisFeignClient;
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;

/**
 * Description:
 * Date: 2023/11/7  9:29【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@Component
public class SpecialAnalysisFeignClientFallbackFactory implements FallbackFactory<SpecialAnalysisFeignClient> {
    @Override
    public SpecialAnalysisFeignClient 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 SpecialAnalysisFeignClient() {

            @Override
            public HttpResult<Boolean> hanlder(LineParam jobParam) {
                log.error("{}异常，降级处理，异常为：{}", "专项分析指标总览:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
