Browse Source

抢购接口第一版完成提交

dinglan 2 years ago
parent
commit
83a9ed8aea

+ 28 - 0
src/main/java/com/sw/activemq/config/ActiveMQConfig.java

@@ -0,0 +1,28 @@
+package com.sw.activemq.config;
+
+import org.apache.activemq.command.ActiveMQQueue;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.jms.annotation.EnableJms;
+import org.springframework.stereotype.Component;
+
+import javax.jms.Queue;
+
+/**
+ * @ClassName ActiveMQConfig
+ * @Description activemq 队列
+ * @Author DINGLAN
+ * @Date 2019/4/24 16:45
+ **/
+@Component
+@EnableJms
+public class ActiveMQConfig {
+    //TODO  如果有多个队列 如何注入?
+    @Value("${queuename}")
+    private String queueName;
+
+    @Bean
+    public Queue queue() {
+        return new ActiveMQQueue(queueName) ;
+    }
+}

+ 17 - 0
src/main/java/com/sw/activemq/consumer/MessageConsumerService.java

@@ -0,0 +1,17 @@
+package com.sw.activemq.consumer;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.jms.annotation.JmsListener;
+import org.springframework.stereotype.Service;
+
+import javax.jms.Message;
+
+@Service
+@Slf4j
+public class MessageConsumerService {
+
+	@JmsListener(destination="mask")
+	public void receiveMessage(Message info) {	// 进行消息接收处理
+		log.info("【*** 接收消息 ***】--->info", info);
+	}
+}

+ 6 - 0
src/main/java/com/sw/activemq/producer/IMessageProducerService.java

@@ -0,0 +1,6 @@
+package com.sw.activemq.producer;
+
+public interface IMessageProducerService {
+
+	void sendMessage(String message);
+}

+ 22 - 0
src/main/java/com/sw/activemq/producer/impl/MessageProducerServiceImpl.java

@@ -0,0 +1,22 @@
+package com.sw.activemq.producer.impl;
+
+import com.sw.activemq.producer.IMessageProducerService;
+import org.springframework.jms.core.JmsMessagingTemplate;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import javax.jms.Queue;
+
+@Service
+public class MessageProducerServiceImpl implements IMessageProducerService {
+	@Resource
+	private JmsMessagingTemplate jmsMessagingTemplate;
+
+	@Resource
+	private Queue queue;
+
+	@Override
+	public void sendMessage(String message) {
+		jmsMessagingTemplate.convertAndSend(this.queue, message);
+	}
+}

+ 39 - 0
src/main/java/com/sw/controller/SnatchMaskController.java

@@ -0,0 +1,39 @@
+package com.sw.controller;
+
+import com.sw.service.SnatchMaskService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+
+/**
+ * @Description 抢口罩入口
+ * @Author DingLan
+ * @Date 2022/2/18 13:13
+ **/
+@RestController
+@RequestMapping("/mask")
+public class SnatchMaskController {
+
+    @Resource
+    private RedisTemplate redisTemplate;
+
+    @Autowired
+    private SnatchMaskService snatchMaskService;
+
+    @GetMapping()
+    private String snatch(Integer uid){
+        if(redisTemplate.opsForSet().isMember("userUids", uid)){
+            if(redisTemplate.opsForSet().isMember("successUserUids", uid)){
+                return "你已经抢到口罩了";
+            }else{
+                return snatchMaskService.doSnatch(uid);
+            }
+        }else{
+            return "非法输入";
+        }
+    }
+}

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

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

+ 6 - 2
src/main/java/com/sw/domain/TOrder.java

@@ -1,7 +1,9 @@
 package com.sw.domain;
 
 import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.NoArgsConstructor;
 
 import java.io.Serializable;
 import java.util.Date;
@@ -13,6 +15,8 @@ import java.util.Date;
  * @since 2022-02-18 10:28:01
  */
 @Data
+@AllArgsConstructor
+@NoArgsConstructor
 @TableName("t_order")
 public class TOrder implements Serializable {
     private static final long serialVersionUID = 456314406106326807L;
@@ -23,7 +27,7 @@ public class TOrder implements Serializable {
     /**
      * 用户标识
      */
-    private String uid;
+    private Integer uid;
 
     private Integer mid;
     /**
@@ -36,4 +40,4 @@ public class TOrder implements Serializable {
     private String state;
 
 
-}
+}

+ 10 - 0
src/main/java/com/sw/service/SnatchMaskService.java

@@ -0,0 +1,10 @@
+package com.sw.service;
+
+/**
+ * @Description TODO
+ * @Author DingLan
+ * @Date 2022/2/18 13:21
+ **/
+public interface SnatchMaskService {
+    String doSnatch(Integer uid);
+}

+ 2 - 1
src/main/java/com/sw/service/TMaskService.java

@@ -12,4 +12,5 @@ import com.sw.domain.TMask;
 public interface TMaskService extends IService<TMask> {
 
 
-}
+    boolean decrease(Integer maskId, Integer mid);
+}

+ 2 - 1
src/main/java/com/sw/service/TOrderService.java

@@ -12,4 +12,5 @@ import com.sw.domain.TOrder;
 public interface TOrderService extends IService<TOrder> {
 
 
-}
+    int creatOrder(Integer uid, Integer maskId);
+}

+ 53 - 0
src/main/java/com/sw/service/impl/SnatchMaskServiceImpl.java

@@ -0,0 +1,53 @@
+package com.sw.service.impl;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+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.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.Resource;
+
+/**
+ * @Description TODO
+ * @Author DingLan
+ * @Date 2022/2/18 13:22
+ **/
+@Service
+public class SnatchMaskServiceImpl implements SnatchMaskService {
+    @Autowired
+    private TMaskService maskService;
+
+    @Autowired
+    private TOrderService orderService;
+
+    @Resource
+    private RedisTemplate redisTemplate;
+
+    @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){
+            //3.create order
+            int res = orderService.creatOrder(uid, maskId);
+            //4.decrease the stock of mask
+            Boolean flag = maskService.decrease(maskId, mask.getMaskStock() - 5);
+            if(res > 0 && flag){
+
+                //3. add to successUserUids
+                redisTemplate.opsForSet().add("successUserUids", uid);
+                return "恭喜你 抢购成功";
+            }else{
+                return  "抢购失败 稍后重试";
+            }
+        }
+        return "抢购失败 稍后重试";
+    }
+}

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

@@ -1,5 +1,6 @@
 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.TMaskDao;
 import com.sw.domain.TMask;
@@ -16,8 +17,12 @@ import javax.annotation.Resource;
  */
 @Service("tMaskService")
 public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TMaskService {
-    @Resource
-    private TMaskDao tMaskDao;
 
-
-}
+    @Override
+    public boolean decrease(Integer maskId, Integer remain) {
+        UpdateWrapper<TMask> update = new UpdateWrapper<TMask>();
+        update.eq("id", maskId)
+                .set("mask_stock", remain);
+        return this.update(update);
+    }
+}

+ 12 - 1
src/main/java/com/sw/service/impl/TOrderServiceImpl.java

@@ -7,6 +7,7 @@ import com.sw.service.TOrderService;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.util.Date;
 
 /**
  * (TOrder)表服务实现类
@@ -20,4 +21,14 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderDao, TOrder> implements
     private TOrderDao tOrderDao;
 
 
-}
+    @Override
+    public int creatOrder(Integer uid, Integer maskId) {
+        TOrder order = new TOrder();
+        order.setCreateTime(new Date());
+        order.setMid(maskId);
+        order.setUid(uid);
+        order.setState("1");
+
+       return tOrderDao.insert(order);
+    }
+}

+ 10 - 0
src/main/java/com/sw/vo/SnatchUser.java

@@ -0,0 +1,10 @@
+package com.sw.vo;
+
+/**
+ * @Description TODO
+ * @Author DingLan
+ * @Date 2022/2/18 13:17
+ **/
+public class SnatchUser {
+    private String uid;
+}

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

@@ -17,12 +17,15 @@ spring:
     port: 6379
     # Redis服务器连接密码(默认为空)
     password: sunwin
+  jms:
+    # 配置消息的类型 默认false,如果是true则表示为topic消息,如果为false表示Queue消息,一个服务里 只能用一种???
+    pub-sub-domain: false
   activemq:
     user: admin    # 连接用户名
     password: admin   # 连接密码
     broker-url: tcp://192.168.20.76:28085 # 消息组件的连接主机信息
-    packages:
-      trust-all: true
+queuename: mask
+
 mybatis-plus:
   # 指向实体类包路径
   type-aliases-package: com.sw.domain

+ 29 - 0
src/test/java/com/sw/ActivemqTest.java

@@ -0,0 +1,29 @@
+package com.sw;
+
+import com.sw.activemq.producer.IMessageProducerService;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+
+import javax.annotation.Resource;
+
+/**
+ * @Description TODO
+ * @Author DingLan
+ * @Date 2022/2/18 11:34
+ **/
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest
+@WebAppConfiguration
+public class ActivemqTest {
+    @Resource
+    private IMessageProducerService service;
+
+    @Test
+    public void  send(){
+        service.sendMessage("mask的第一条mq消息");
+    }
+}

+ 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);
         }
     }

+ 19 - 3
src/test/java/com/sw/RedisTest.java

@@ -28,9 +28,25 @@ public class RedisTest {
     public void addSetTest(){
         List<Integer> uids = new ArrayList<>();
         for (int i = 0;i < 1000; i++){
-            uids.add(i + 1);
+            uids.add( i + 1);
         }
-        redisTemplate.opsForSet().add("userUid", uids.toArray());
-        Assert.assertEquals(1000l, redisTemplate.opsForSet().size("userUid").longValue());
+        redisTemplate.opsForSet().add("userUids", uids.toArray());
+        Assert.assertEquals(1000l, redisTemplate.opsForSet().size("userUids").longValue());
+    }
+
+    @Test
+    @Order(1)
+    public void clear(){
+        Long count = redisTemplate.opsForSet().size("userUids");
+        System.out.println(count);
+        redisTemplate.opsForSet().pop("userUids", count);
+
+    }
+
+    @Test
+    public void  isMember(){
+//        System.out.println(redisTemplate.opsForSet().isMember("userUid", "18"));
+//        System.out.println(redisTemplate.opsForSet().isMember("userUid", 18));
+        System.out.println(redisTemplate.opsForSet().isMember("successUserUids", 18));
     }
 }