|
@@ -8,6 +8,7 @@ import com.sw.dao.TOrderDao;
|
|
import com.sw.domain.TMask;
|
|
import com.sw.domain.TMask;
|
|
import com.sw.domain.TOrder;
|
|
import com.sw.domain.TOrder;
|
|
import com.sw.service.TMaskService;
|
|
import com.sw.service.TMaskService;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@@ -22,6 +23,7 @@ import java.util.Date;
|
|
* @author yegang
|
|
* @author yegang
|
|
* @since 2022-02-18 10:27:46
|
|
* @since 2022-02-18 10:27:46
|
|
*/
|
|
*/
|
|
|
|
+@Slf4j
|
|
@Service("tMaskService")
|
|
@Service("tMaskService")
|
|
public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TMaskService {
|
|
public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TMaskService {
|
|
@Resource
|
|
@Resource
|
|
@@ -41,15 +43,16 @@ public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TM
|
|
* @date: 2022/2/18
|
|
* @date: 2022/2/18
|
|
*/
|
|
*/
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
- public synchronized String spike(Integer mid, Integer uid) {
|
|
|
|
|
|
+ public String spike(Integer mid, Integer uid) {
|
|
//1.判断库存
|
|
//1.判断库存
|
|
TMask tMask = tMaskDao.selectById(mid);
|
|
TMask tMask = tMaskDao.selectById(mid);
|
|
if(tMask.getMaskStock()<5){
|
|
if(tMask.getMaskStock()<5){
|
|
return "库存不够,抢购失败";
|
|
return "库存不够,抢购失败";
|
|
}
|
|
}
|
|
//2.减库存
|
|
//2.减库存
|
|
- boolean b = tMaskDao.decreaseStock(5, tMask.getVersion(), 2);
|
|
|
|
|
|
+ boolean b = tMaskDao.decreaseStock(5, tMask.getVersion(), mid);
|
|
if(!b){
|
|
if(!b){
|
|
|
|
+// log.error("减库存成功");
|
|
return "抢购失败,请重试";
|
|
return "抢购失败,请重试";
|
|
}
|
|
}
|
|
//3.下订单
|
|
//3.下订单
|
|
@@ -58,9 +61,11 @@ public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TM
|
|
tOrder.setState("1");
|
|
tOrder.setState("1");
|
|
tOrder.setMid(mid);
|
|
tOrder.setMid(mid);
|
|
tOrder.setUid(uid);
|
|
tOrder.setUid(uid);
|
|
|
|
+// log.error("insert前");
|
|
int insert = tOrderDao.insert(tOrder);
|
|
int insert = tOrderDao.insert(tOrder);
|
|
|
|
+// log.error("insert后");
|
|
//4.设置成功缓存
|
|
//4.设置成功缓存
|
|
- redisTemplate.opsForSet().add("sucess",uid);
|
|
|
|
|
|
+ redisTemplate.opsForSet().add("sucess-" + mid,uid);
|
|
return "抢购成功,订单号为"+tOrder.getId();
|
|
return "抢购成功,订单号为"+tOrder.getId();
|
|
}
|
|
}
|
|
@Override
|
|
@Override
|
|
@@ -70,4 +75,4 @@ public class TMaskServiceImpl extends ServiceImpl<TMaskDao, TMask> implements TM
|
|
.set("mask_stock", remain);
|
|
.set("mask_stock", remain);
|
|
return this.update(update);
|
|
return this.update(update);
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|