package com.njcn.csdevice.api.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.csdevice.api.ProcessFeignClient;
import com.njcn.csdevice.pojo.po.CsEquipmentProcessPO;
import com.njcn.csdevice.utils.CsDeviceEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/4/10 20:09
 */
@Slf4j
@Component
public class ProcessFeignClientFallbackFactory implements FallbackFactory<ProcessFeignClient> {
    @Override
    public ProcessFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (cause.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) cause.getCause();
            exceptionEnum = CsDeviceEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new ProcessFeignClient() {

            @Override
            public HttpResult<String> add(CsEquipmentProcessPO csEquipmentProcess) {
                log.error("{}异常，降级处理，异常为：{}","新增记录",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
