...
|
...
|
@@ -5,24 +5,57 @@ import com.yoho.error.ServiceError; |
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yohoufo.dal.order.BlackUserMapper;
|
|
|
import com.yohoufo.dal.order.model.BlackUser;
|
|
|
import com.yohoufo.order.annotation.BlackUserType;
|
|
|
import com.yohoufo.order.common.BlackTypeEnum;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.aspectj.lang.JoinPoint;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
import org.aspectj.lang.annotation.Before;
|
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.BeansException;
|
|
|
import org.springframework.beans.factory.BeanNameAware;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.lang.annotation.Annotation;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
public abstract class BlackUserInterceptor implements ApplicationContextAware {
|
|
|
@Aspect
|
|
|
@Component
|
|
|
public class BlackUserAspect {
|
|
|
|
|
|
Logger logger = LoggerFactory.getLogger(BlackUserInterceptor.class);
|
|
|
private static Map<Integer, AbstractBlackUserInterceptor> blackUserInteceptorMap = Maps.newHashMap();
|
|
|
|
|
|
|
|
|
@Pointcut("@annotation(com.yohoufo.order.annotation.BlackUserType)")
|
|
|
private void pointId(){
|
|
|
|
|
|
}
|
|
|
|
|
|
@Before("pointId()")
|
|
|
private void before(JoinPoint joinPoint) throws Throwable{
|
|
|
|
|
|
|
|
|
Object[] argsa = joinPoint.getArgs();
|
|
|
|
|
|
Annotation[][] parameterTypes = ((MethodSignature)joinPoint.getSignature()).getMethod().getParameterAnnotations();
|
|
|
|
|
|
BlackUserType blackUserAnnotation = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(BlackUserType.class);
|
|
|
|
|
|
blackUserInteceptorMap.get(blackUserAnnotation.blackType()).before((int)argsa[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private abstract static class AbstractBlackUserInterceptor implements ApplicationContextAware {
|
|
|
|
|
|
Logger logger = LoggerFactory.getLogger(AbstractBlackUserInterceptor.class);
|
|
|
|
|
|
@Autowired
|
|
|
BlackUserMapper blackUserMapper;
|
...
|
...
|
@@ -52,22 +85,46 @@ public abstract class BlackUserInterceptor implements ApplicationContextAware { |
|
|
abstract int getBlackType();
|
|
|
|
|
|
|
|
|
private static final Map<Integer, BlackUserInterceptor> blackUserInteceptorMap = Maps.newHashMap();
|
|
|
abstract ServiceException getServiceException();
|
|
|
|
|
|
|
|
|
public static BlackUserInterceptor getBlackUserInteceptor(Integer code){
|
|
|
return blackUserInteceptorMap.get(code);
|
|
|
@Override
|
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
Map<String, AbstractBlackUserInterceptor> map = applicationContext.getBeansOfType(AbstractBlackUserInterceptor.class);
|
|
|
for (String beanName : map.keySet()){
|
|
|
AbstractBlackUserInterceptor abstractBlackUserInterceptor = map.get(beanName);
|
|
|
blackUserInteceptorMap.put(abstractBlackUserInterceptor.getBlackType(), abstractBlackUserInterceptor);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
abstract ServiceException getServiceException();
|
|
|
|
|
|
@Component
|
|
|
private static class BuyBlackUserInterceptor extends AbstractBlackUserInterceptor {
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
// 重构map中的拦截器key,value
|
|
|
Map<String, BlackUserInterceptor> map = applicationContext.getBeansOfType(BlackUserInterceptor.class);
|
|
|
for (String beanName: map.keySet()){
|
|
|
BlackUserInterceptor interceptor = (BlackUserInterceptor) applicationContext.getBean(beanName);
|
|
|
blackUserInteceptorMap.put(interceptor.getBlackType(), interceptor);
|
|
|
int getBlackType() {
|
|
|
return BlackTypeEnum.BUY.getCode();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
ServiceException getServiceException() {
|
|
|
return new ServiceException(ServiceError.NOT_ALLOW_BLACK_UID_BUY);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
@Component
|
|
|
private static class SellBlackUserInterceptor extends AbstractBlackUserInterceptor {
|
|
|
|
|
|
@Override
|
|
|
int getBlackType() {
|
|
|
return BlackTypeEnum.SELL.getCode();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
ServiceException getServiceException() {
|
|
|
return new ServiceException(ServiceError.NOT_ALLOW_BLACK_UID_SELL);
|
|
|
}
|
|
|
|
|
|
}
|
...
|
...
|
|