package com.njcn.system.api.fallback;

import com.njcn.common.pojo.dto.LogInfoDTO;
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.system.api.AreaFeignClient;
import com.njcn.system.api.UserLogFeignClient;
import com.njcn.system.pojo.dto.AreaTreeDTO;
import com.njcn.system.pojo.po.Area;
import com.njcn.system.utils.SystemEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import feign.hystrix.FallbackFactory;


/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年07月12日 15:21
 */
@Slf4j
@Component
public class UserLogFeignClientFallbackFactory  implements FallbackFactory<UserLogFeignClient> {

    /**
     * 输出远程请求接口异常日志
     * @param cause RPC请求异常
     */
    @Override
    public UserLogFeignClient create(Throwable cause) {
        //判断抛出异常是否为解码器抛出的业务异常
        Enum<?> exceptionEnum = CommonResponseEnum.SERVICE_FALLBACK;
        if(cause.getCause() instanceof BusinessException){
            BusinessException businessException = (BusinessException) cause.getCause();
            exceptionEnum = SystemEnumUtil.getExceptionEnum(businessException.getResult());
        }
        Enum<?> finalExceptionEnum = exceptionEnum;
        return new UserLogFeignClient() {
            @Override
            public void addUserLog(LogInfoDTO logInfoDTO) {
                log.error("{}异常，降级处理，异常为：{}","插入审计日志",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

        };
    }
}
