package com.njcn.supervision.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.supervision.api.UserReportManageOldFeignClient;
import com.njcn.supervision.pojo.param.SensitiveUserParam;
import com.njcn.supervision.pojo.po.user.UserReportPO;
import com.njcn.supervision.utils.SupervisionEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;


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

            @Override
            public HttpResult<List<UserReportPO>> getSensitiveUserByDept(SensitiveUserParam param) {
                log.error("{}异常，降级处理，异常为：{}", "根据部门获取敏感用户信息异常", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
