|
@@ -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 "抢购失败 稍后重试";
|
|
|
|
+ }
|
|
|
|
+}
|