|
@@ -53,8 +53,7 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public String doSnatch(Integer uid) {
|
|
|
- Integer maskId = 1;
|
|
|
+ public String doSnatch(Integer uid, Integer maskId) {
|
|
|
//1.get the amount of remaining masks
|
|
|
TMask mask = maskService.getById(maskId);
|
|
|
if (mask.getMaskStock() > amount) {
|
|
@@ -164,22 +163,7 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
try {
|
|
|
while (lock.tryLock(1000, TimeUnit.MILLISECONDS)) {
|
|
|
log.info("线程:{} 获取锁", threadName);
|
|
|
- //1.get the amount of remaining masks
|
|
|
- TMask mask = maskService.getById(maskId);
|
|
|
- System.out.println(mask.getMaskStock() + "----" + Thread.currentThread());
|
|
|
- if (mask.getMaskStock() >= amount) {
|
|
|
- //3.create order
|
|
|
- int res = orderService.creatOrder(uid, maskId);
|
|
|
- //4.decrease the stock of mask
|
|
|
- Boolean flag = maskService.decrease(maskId, mask.getMaskStock() - amount);
|
|
|
- if (res > 0 && flag) {
|
|
|
- //3. add to successUserUids
|
|
|
- redisTemplate.opsForSet().add("successUserUids", uid);
|
|
|
- }
|
|
|
- } else {
|
|
|
- // TODO 如果sql中库存不够 扣减失败之后 消息已经消费了 或者是事物回滚了 如何处理???
|
|
|
- return;
|
|
|
- }
|
|
|
+ log.info("doSnatch方法执行结果:{}", this.doSnatch(uid, maskId));
|
|
|
lock.unlock();
|
|
|
log.info("{}:业务执行完成", threadName);
|
|
|
break;
|