package com.njcn.cssystem.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.cssystem.api.MsgSendFeignClient;
import com.njcn.cssystem.pojo.param.MsgSendParam;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

            @Override
            public HttpResult<Boolean> appMsgSend(MsgSendParam param) {
                log.error("{}异常，降级处理，异常为：{}","app消息推送异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }

            @Override
            public HttpResult<Boolean> smsMsgSend(MsgSendParam param) {
                log.error("{}异常，降级处理，异常为：{}","短信消息推送异常",cause.toString());
                throw new BusinessException(finalExceptionEnum);
            }
        };
    }
}
