|
@@ -1,16 +1,20 @@
|
|
|
package com.sw.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
|
+import com.sw.activemq.producer.IMessageProducerService;
|
|
|
import com.sw.domain.TMask;
|
|
|
import com.sw.service.SnatchMaskService;
|
|
|
import com.sw.service.TMaskService;
|
|
|
import com.sw.service.TOrderService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+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 javax.annotation.Resource;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @Description TODO
|
|
@@ -28,17 +32,23 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
@Resource
|
|
|
private RedisTemplate redisTemplate;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private IMessageProducerService mqService;
|
|
|
+
|
|
|
+ @Value("${amount}")
|
|
|
+ private Integer amount;
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional
|
|
|
public String doSnatch(Integer uid) {
|
|
|
Integer maskId = 1;
|
|
|
//1.get the amount of remaining masks
|
|
|
TMask mask = maskService.getById(maskId);
|
|
|
- if(mask.getMaskStock() > 0){
|
|
|
+ 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() - 5);
|
|
|
+ Boolean flag = maskService.decrease(maskId, mask.getMaskStock() - amount);
|
|
|
if(res > 0 && flag){
|
|
|
|
|
|
//3. add to successUserUids
|
|
@@ -48,6 +58,52 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
|
|
|
return "抢购失败 稍后重试";
|
|
|
}
|
|
|
}
|
|
|
- return "抢购失败 稍后重试";
|
|
|
+ return "库存不足 抢购失败";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查看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;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String doSnatchMQ(Integer uid, Integer mid) {
|
|
|
+ Map<String, Object> agrs = new HashMap<>();
|
|
|
+ agrs.put("uid", uid);
|
|
|
+ agrs.put("mid", mid);
|
|
|
+ mqService.sendMessage(agrs);
|
|
|
+ return "抢购成功 请稍后查看订单";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * mq消费端 调用方法 扣减sql库存 创建未支付订单
|
|
|
+ * @param uid
|
|
|
+ * @param maskId
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void doDecrAndCreateOrder(Integer uid, Integer maskId) {
|
|
|
+ //1.get the amount of remaining masks
|
|
|
+ TMask mask = maskService.getById(maskId);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
}
|
|
|
}
|