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