package com.lyms.talkonlineweb.aop;
import com.lyms.talkonlineweb.annotation.Resubmit;
import com.lyms.talkonlineweb.domain.BaseModel;
import com.lyms.talkonlineweb.lock.ResubmitLock;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.util.JsonUtil;
import com.lyms.talkonlineweb.util.StringUtil;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author lqy
* 重复提交处理
*/
@Log4j2
@Aspect
@Component
public class ResubmitDataAspect {
private final static Object PRESENT = new Object();
@Around("@annotation(resubmit)")
public Object handleResubmit(ProceedingJoinPoint joinPoint, Resubmit resubmit) throws Throwable {
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
//获取注解信息
Resubmit annotation = method.getAnnotation(Resubmit.class);
int delaySeconds = annotation.delaySeconds();
Object[] pointArgs = joinPoint.getArgs();
StringBuilder key = new StringBuilder();
if (pointArgs != null && pointArgs.length > 0)
{
for (Object param : pointArgs)
{
if (param instanceof BaseModel) {
String paramStr = JsonUtil.obj2Str(param);
//生成加密参数 使用了content_MD5的加密方式
key.append(ResubmitLock.handleKey(paramStr));
}
}
if (StringUtil.isNotEmpty(key.toString()))
{
//执行锁
boolean lock = false;
try
{
//设置解锁key
lock = ResubmitLock.getInstance().lock(key.toString(), PRESENT);
if (lock)
{ //放行
return joinPoint.proceed();
}
else
{ //响应重复提交异常
BaseResponse response = new BaseResponse();
response.setErrorcode(1);
response.setErrormsg("请勿重复提交");
return response;
}
}
finally
{
//设置解锁key和解锁时间
ResubmitLock.getInstance().unLock(lock, key.toString(), delaySeconds);
}
}
}
return joinPoint.proceed();
}
}