Pārlūkot izejas kodu

Merge remote-tracking branch 'origin/master'

dinglan 2 gadi atpakaļ
vecāks
revīzija
925713c13d

+ 43 - 0
src/main/java/com/sw/controller/SpikeController.java

@@ -0,0 +1,43 @@
+package com.sw.controller;
+
+import com.sw.service.SysUserService;
+import com.sw.service.TMaskService;
+import com.sw.service.TOrderService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * 秒杀服务
+ *
+ * @author yegang
+ * @create 2022-02-18 13:16
+ **/
+@RestController
+
+public class SpikeController {
+    @Autowired
+    private SysUserService sysUserService;
+    @Autowired
+    private TOrderService tOrderService;
+    @Autowired
+    private TMaskService tMaskService;
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    @GetMapping("spike/{mid}/{uid}")
+    public  String spike(@PathVariable("mid") Integer mid, @PathVariable("uid") Integer uid) {
+        //判断用户是否存在
+        Boolean HasUserUid = redisTemplate.opsForSet().isMember("userUid", uid);
+        if (!HasUserUid) {
+            return "用户不存在";
+        }
+        //2.是否已经抢购成功
+        Boolean successUser = redisTemplate.opsForSet().isMember("success", uid);
+        if (successUser) {
+            return "一个用户只能抢购一次";
+        }
+        String res = tMaskService.spike(mid, uid);
+        return res;
+    }
+}

+ 10 - 0
src/main/java/com/sw/controller/TestController.java

@@ -1,9 +1,12 @@
 package com.sw.controller;
 
 import com.sw.domain.SysTest;
+import com.sw.domain.SysUser;
 import com.sw.service.SysTestService;
+import com.sw.service.SysUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -19,10 +22,17 @@ import java.util.List;
 public class TestController {
     @Autowired
     SysTestService sysTestService;
+    @Autowired
+    SysUserService sysUserService;
     @RequestMapping("/test")
     public  String  test(){
         List<SysTest> list = sysTestService.list();
         System.out.println(list);
         return "test";
     }
+    @RequestMapping("/get/{id}")
+    public Integer getUser(@PathVariable int id){
+        SysUser user = sysUserService.getById(id);
+        return user.getId();
+    }
 }

+ 8 - 0
src/main/java/com/sw/service/TMaskService.java

@@ -10,6 +10,14 @@ import com.sw.domain.TMask;
  * @since 2022-02-18 10:27:45
  */
 public interface TMaskService extends IService<TMask> {
+    /*
+     * 方法描述:秒杀service
+     * @param: Integer mid, Integer uid
+     * @return: String
+     * @author: yegang
+     * @date: 2022/2/18
+     */
+    String spike(Integer mid, Integer uid);
 
 
     boolean decrease(Integer maskId, Integer mid);

+ 45 - 2
src/main/java/com/sw/service/impl/TMaskServiceImpl.java

@@ -2,12 +2,19 @@ package com.sw.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.sw.dao.SysUserDao;
 import com.sw.dao.TMaskDao;
+import com.sw.dao.TOrderDao;
 import com.sw.domain.TMask;
+import com.sw.domain.TOrder;
 import com.sw.service.TMaskService;
+import org.springframework.beans.factory.annotation.Autowired;
+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.Date;
 
 /**
  * (TMask)表服务实现类
@@ -17,7 +24,43 @@ import javax.annotation.Resource;
  */
 @Service("tMaskService")
 public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TMaskService {
-
+    @Resource
+    private TMaskDao tMaskDao;
+    @Autowired
+    private SysUserDao sysUserDao;
+    @Autowired
+    private TOrderDao tOrderDao;
+    @Autowired
+    private RedisTemplate redisTemplate;
+    @Override
+    /*
+     * 方法描述:秒杀service
+     * @param: Integer mid, Integer uid
+     * @return: String
+     * @author: yegang
+     * @date: 2022/2/18
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public synchronized String spike(Integer mid, Integer uid) {
+        //1.判断库存
+        TMask tMask = tMaskDao.selectById(mid);
+        if(tMask.getMaskStock()<5){
+            return "库存不够,抢购失败";
+        }
+        //2.减库存
+        tMask.setMaskStock(tMask.getMaskStock()-5);
+        tMaskDao.updateById(tMask);
+        //3.下订单
+        TOrder tOrder =new TOrder();
+        tOrder.setCreateTime(new Date());
+        tOrder.setState("1");
+        tOrder.setMid(mid);
+        tOrder.setUid(uid);
+        int insert = tOrderDao.insert(tOrder);
+        //4.设置成功缓存
+        redisTemplate.opsForSet().add("sucess",uid);
+        return "抢购成功,订单号为"+tOrder.getId();
+    }
     @Override
     public boolean decrease(Integer maskId, Integer remain) {
         UpdateWrapper<TMask> update = new UpdateWrapper<TMask>();
@@ -25,4 +68,4 @@ public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TM
                 .set("mask_stock", remain);
         return this.update(update);
     }
-}
+}