package com.njcn.supervision.api.fallback;

import com.njcn.bpm.pojo.dto.BpmInstanceInfo;
import com.njcn.bpm.pojo.vo.TodoVO;
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.BusinessCommonFeignClient;
import com.njcn.supervision.utils.SupervisionEnumUtil;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;


@Slf4j
@Component
public class BusinessCommonFeignClientFallbackFactory implements FallbackFactory<BusinessCommonFeignClient> {
    @Override
    public BusinessCommonFeignClient 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 BusinessCommonFeignClient() {
            @Override
            public HttpResult<Object> updateProcessStatus(String processKey, String businessId, String reason, Integer status) {
                log.error("{}异常，降级处理，异常为：{}", "更新流程状态", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<BpmInstanceInfo> getInstanceInfo(String processKey, String businessId) {
                log.error("{}异常，降级处理，异常为：{}", "获取流程额外信息", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Set<String>> getAllRejectInstanceId() {
                log.error("{}异常，降级处理，异常为：{}", "获取所有流程不通过实例id", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<List<TodoVO>> getTodoBusiness() {
                log.error("{}异常，降级处理，异常为：{}", "获取当前用户部分待办业务", throwable.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
