Browse Source

去除synchronized;success增加mid区分

dinglan 2 years ago
parent
commit
7ded25a273

+ 1 - 1
src/main/java/com/sw/controller/SnatchMaskController.java

@@ -28,7 +28,7 @@ public class SnatchMaskController {
     private String snatch(Integer uid){
         if(redisTemplate.opsForSet().isMember("userUids", uid)){
             if(redisTemplate.opsForSet().isMember("successUserUids", uid)){
-                return "你已经抢到口罩了";
+                return "你已经抢到了";
             }else{
                 return snatchMaskService.doSnatch(uid);
             }

+ 1 - 1
src/main/java/com/sw/controller/SpikeController.java

@@ -33,7 +33,7 @@ public class SpikeController {
             return "用户不存在";
         }
         //2.是否已经抢购成功
-        Boolean successUser = redisTemplate.opsForSet().isMember("success", uid);
+        Boolean successUser = redisTemplate.opsForSet().isMember("success-" + mid, uid);
         if (successUser) {
             return "一个用户只能抢购一次";
         }

+ 1 - 1
src/main/java/com/sw/controller/TestController.java

@@ -31,7 +31,7 @@ public class TestController {
         return "test";
     }
     @RequestMapping("/get/{id}")
-    public String getUser(@PathVariable int id){
+    public Integer getUser(@PathVariable int id){
         SysUser user = sysUserService.getById(id);
         return user.getId();
     }

+ 1 - 1
src/main/java/com/sw/domain/SysUser.java

@@ -18,5 +18,5 @@ import java.io.Serializable;
 public class SysUser implements Serializable {
     private static final long serialVersionUID = 616589298019846813L;
 
-    private String id;
+    private Integer id;
 }

+ 10 - 4
src/main/java/com/sw/service/impl/SnatchMaskServiceImpl.java

@@ -6,6 +6,7 @@ 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;
@@ -28,17 +29,22 @@ public class SnatchMaskServiceImpl implements SnatchMaskService {
     @Resource
     private RedisTemplate redisTemplate;
 
+    @Value("${amount}")
+    private Integer amount;
+
+    @Value("${maskid}")
+    private Integer maskId;
+
     @Override
     @Transactional
-    public String doSnatch(Integer uid) {
-        Integer maskId = 1;
+    public synchronized String doSnatch(Integer uid) {
         //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

+ 9 - 4
src/main/java/com/sw/service/impl/TMaskServiceImpl.java

@@ -8,6 +8,7 @@ import com.sw.dao.TOrderDao;
 import com.sw.domain.TMask;
 import com.sw.domain.TOrder;
 import com.sw.service.TMaskService;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
@@ -22,6 +23,7 @@ import java.util.Date;
  * @author yegang
  * @since 2022-02-18 10:27:46
  */
+@Slf4j
 @Service("tMaskService")
 public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TMaskService {
     @Resource
@@ -41,15 +43,16 @@ public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TM
      * @date: 2022/2/18
      */
     @Transactional(rollbackFor = Exception.class)
-    public synchronized String spike(Integer mid, Integer uid) {
+    public String spike(Integer mid, Integer uid) {
         //1.判断库存
         TMask tMask = tMaskDao.selectById(mid);
         if(tMask.getMaskStock()<5){
             return "库存不够,抢购失败";
         }
         //2.减库存
-        boolean b = tMaskDao.decreaseStock(5, tMask.getVersion(), 2);
+        boolean b = tMaskDao.decreaseStock(5, tMask.getVersion(), mid);
         if(!b){
+//            log.error("减库存成功");
             return "抢购失败,请重试";
         }
         //3.下订单
@@ -58,9 +61,11 @@ public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TM
         tOrder.setState("1");
         tOrder.setMid(mid);
         tOrder.setUid(uid);
+//        log.error("insert前");
         int insert = tOrderDao.insert(tOrder);
+//        log.error("insert后");
         //4.设置成功缓存
-        redisTemplate.opsForSet().add("sucess",uid);
+        redisTemplate.opsForSet().add("sucess-" + mid,uid);
         return "抢购成功,订单号为"+tOrder.getId();
     }
     @Override
@@ -70,4 +75,4 @@ public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TM
                 .set("mask_stock", remain);
         return this.update(update);
     }
-}
+}

+ 6 - 2
src/main/resources/application.yml

@@ -27,7 +27,11 @@ spring:
     user: admin    # 连接用户名
     password: admin   # 连接密码
     broker-url: tcp://192.168.20.76:28085 # 消息组件的连接主机信息
-queuename: mask
+
+#自定义属性
+queuename: mask #mq中队列名称
+maskid: 1 #抢购的口罩记录主键
+amount: 5 #每次抢购口罩的数量
 
 mybatis-plus:
   # 指向实体类包路径
@@ -48,4 +52,4 @@ mybatis-plus:
     variables:
       nextTermDays: 90
       #mybatis-plus配置控制台打印完整带参数SQL语句
-    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
+#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

+ 1 - 1
src/test/java/com/sw/AddUserTest.java

@@ -24,7 +24,7 @@ public class AddUserTest {
     @Test
     private void addUser(){
         for (int i = 0;i < 1000; i++){
-            SysUser user = new SysUser("" + i + 1);
+            SysUser user = new SysUser( i + 1);
             userDao.insert(user);
         }
     }