package com.njcn.process.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.prepare.harmonic.pojo.dto.OverLimitFlagDTO;
import com.njcn.process.api.RStatWorkOrderFeignClient;
import com.njcn.process.utils.ProcessEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2023/2/15  14:15【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@Component
public class RStatWorkOrderFallbackFactory implements FallbackFactory<RStatWorkOrderFeignClient> {

    @Override
    public RStatWorkOrderFeignClient create(Throwable throwable) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if (throwable.getCause() instanceof BusinessException) {
            BusinessException businessException = (BusinessException) throwable.getCause();
            exceptionEnum = ProcessEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new RStatWorkOrderFeignClient() {

            @Override
            public HttpResult<Boolean> createProblem(List<OverLimitFlagDTO> overLimitFlagDTOList) {
                log.error("{}异常，降级处理，异常为：{}", "问题单插入异常", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
