|
@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.HashMap;
|
|
@@ -65,11 +66,19 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
* 查看redis库存是否充足
|
|
|
*/
|
|
|
@Override
|
|
|
- public Boolean checkAndDecrRemaining(Integer mid) {
|
|
|
- Integer remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());
|
|
|
- Long res = redisTemplate.opsForHash().increment("usableMasks", "mask-" + mid, -amount);
|
|
|
- System.out.println("increment res : "+res);//扣减之后的库存数量
|
|
|
- return remaining.compareTo(amount) > 0;
|
|
|
+ public Boolean checkAndDecrRemaining(Integer mid) {
|
|
|
+ Object o = new Object();
|
|
|
+ Integer remaining = 0;
|
|
|
+// synchronized (o) {
|
|
|
+ remaining = Integer.valueOf(redisTemplate.opsForHash().get("usableMasks", "mask-" + mid).toString());
|
|
|
+ if(remaining.compareTo(amount) >= 0) {
|
|
|
+ Long res = redisTemplate.opsForHash().increment("usableMasks", "mask-" + mid, -amount);
|
|
|
+ System.out.println("increment res : "+res +"---" + Thread.currentThread());//扣减之后的库存数量
|
|
|
+ return true;
|
|
|
+ }else{
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -91,7 +100,7 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
public void doDecrAndCreateOrder(Integer uid, Integer maskId) {
|
|
|
//1.get the amount of remaining masks
|
|
|
TMask mask = maskService.getById(maskId);
|
|
|
- if(mask.getMaskStock() > amount) {
|
|
|
+ if(mask.getMaskStock() >= amount) {
|
|
|
//3.create order
|
|
|
int res = orderService.creatOrder(uid, maskId);
|
|
|
//4.decrease the stock of mask
|