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.HarmonicGeneralFeignClient;
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;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author xiaoyao
 * @version 1.0.0
 * @createTime 2022/11/7 15:17
 */
@Slf4j
@Component
public class HarmonicGeneralFeignClientFallbackFactory implements FallbackFactory<HarmonicGeneralFeignClient> {
    @Override
    public HarmonicGeneralFeignClient 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 HarmonicGeneralFeignClient() {
            @Override
            public HttpResult<String> generalData(@RequestBody @Validated LineParam lineParam){
                log.error("{}异常，降级处理，异常为：{}", "稳态综合评估:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<String> generalDataToMysql(LineParam lineParam) {
                log.error("{}异常，降级处理，异常为：{}", "稳态综合评估:  ", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
