提交 7ffc2bdb 作者: 张小凤

NotRepeatSubmitConfig(update)

上级 e69c7c5b
package com.mmc.oms.controller.demand.commit; package com.mmc.oms.controller.demand.commit;
import com.alibaba.fastjson.JSONObject;
import com.mmc.oms.common.result.ResultBody;
import com.mmc.oms.controller.BaseController; import com.mmc.oms.controller.BaseController;
import com.mmc.oms.dao.demand.DemandReleaseOrderDao;
import com.mmc.oms.entity.demand.GetOrderNumberDO;
import com.mmc.oms.entity.demand.GetOrderNumberDTO;
import com.mmc.oms.model.vo.demand.CommonPaymentVO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RLock; import org.redisson.api.RLock;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
...@@ -28,6 +41,12 @@ public class NotRepeatSubmitConfig extends BaseController { ...@@ -28,6 +41,12 @@ public class NotRepeatSubmitConfig extends BaseController {
@Autowired @Autowired
private RedissonClient redissonClient; private RedissonClient redissonClient;
@Autowired
private DemandReleaseOrderDao releaseOrderDao;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Pointcut("@within(notRepeatSubmit)||@annotation(notRepeatSubmit)") @Pointcut("@within(notRepeatSubmit)||@annotation(notRepeatSubmit)")
public void pointcut(NotRepeatSubmit notRepeatSubmit) { public void pointcut(NotRepeatSubmit notRepeatSubmit) {
...@@ -44,6 +63,7 @@ public class NotRepeatSubmitConfig extends BaseController { ...@@ -44,6 +63,7 @@ public class NotRepeatSubmitConfig extends BaseController {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest(); HttpServletRequest request = attributes.getRequest();
Integer userAccountId = this.getCurrentAccount(request).getUserAccountId(); Integer userAccountId = this.getCurrentAccount(request).getUserAccountId();
String accountUriLockKey = userAccountId + "-" + request.getServletPath(); String accountUriLockKey = userAccountId + "-" + request.getServletPath();
// 设置锁定资源名称,accountUriLock改为userid+uri作为标识,作为测试写死 // 设置锁定资源名称,accountUriLock改为userid+uri作为标识,作为测试写死
// String accountUriLockKey = "accountUriLock"; // String accountUriLockKey = "accountUriLock";
...@@ -51,13 +71,36 @@ public class NotRepeatSubmitConfig extends BaseController { ...@@ -51,13 +71,36 @@ public class NotRepeatSubmitConfig extends BaseController {
RLock accountUriLock = redissonClient.getLock(accountUriLockKey); RLock accountUriLock = redissonClient.getLock(accountUriLockKey);
boolean tryLock; boolean tryLock;
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = signature.getMethod();
String name = method.getDeclaringClass().getName();
String[] parameterNames = signature.getParameterNames();
Map<String, Object> map = new HashMap<String, Object>();
Object[] values = proceedingJoinPoint.getArgs();
String[] names = ((CodeSignature) proceedingJoinPoint.getSignature()).getParameterNames();
for (int i = 0; i < names.length; i++) {
map.put(names[i], values[i]);
}
log.info(map+"");
CommonPaymentVO commonPaymentsVO = (CommonPaymentVO) map.get("commonPaymentVO");
//尝试获取分布式锁 //尝试获取分布式锁
//-1为永久 leaseTime 最多等待几秒 上锁以后leaseTime秒自动解锁 //-1为永久 leaseTime 最多等待几秒 上锁以后leaseTime秒自动解锁
tryLock = accountUriLock.tryLock(-1, leaseTime, TimeUnit.MILLISECONDS); tryLock = accountUriLock.tryLock(-1, leaseTime, TimeUnit.MILLISECONDS);
log.info("tryLock:" + tryLock); log.info("tryLock:" + tryLock);
if (tryLock) { if (tryLock) {
try { try {
String orderNumber = commonPaymentsVO.getOrderNumber();
String json = stringRedisTemplate.opsForValue().get(orderNumber);
GetOrderNumberDTO getOrderNumberDTO = JSONObject.parseObject(json, GetOrderNumberDTO.class);
GetOrderNumberDO getOrderNumberDO = new GetOrderNumberDO(getOrderNumberDTO);
if (getOrderNumberDTO.getRequirementsInfoId()!=null){
int i = releaseOrderDao.selectOrderNumberDO(getOrderNumberDO.getRequirementsInfoId());
if (i!=0){
return ResultBody.error("当前需求已有人支付");
}
}
// 查询订单库存判断是否大于0 // 查询订单库存判断是否大于0
// 大于0表示还有库存可以更新订单库存将库存数字减一更新到数据库中 // 大于0表示还有库存可以更新订单库存将库存数字减一更新到数据库中
// 不大于0表示没有库存了本次请求就终止 // 不大于0表示没有库存了本次请求就终止
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论