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