|
|
@ -7,6 +7,8 @@ import com.hfkj.entity.GoodsMsgExample; |
|
|
|
import com.hfkj.service.goods.GoodsMsgService; |
|
|
|
import com.hfkj.service.goods.GoodsMsgService; |
|
|
|
import org.apache.commons.collections4.MapUtils; |
|
|
|
import org.apache.commons.collections4.MapUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import javax.annotation.Resource; |
|
|
@ -21,6 +23,9 @@ public class GoodsMsgServiceImpl implements GoodsMsgService { |
|
|
|
@Resource |
|
|
|
@Resource |
|
|
|
private GoodsMsgMapper goodsMsgMapper; |
|
|
|
private GoodsMsgMapper goodsMsgMapper; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
|
|
private RedisTemplate redisTemplate; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void create(GoodsMsg goodsMsg) { |
|
|
|
public void create(GoodsMsg goodsMsg) { |
|
|
|
goodsMsgMapper.insert(goodsMsg); |
|
|
|
goodsMsgMapper.insert(goodsMsg); |
|
|
@ -96,12 +101,27 @@ public class GoodsMsgServiceImpl implements GoodsMsgService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void addSaleNum(Long goodsId, Integer num) { |
|
|
|
public void addSaleNum(Long goodsId, Integer num) throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String key = "GOODS" + goodsId; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 分布式锁占坑
|
|
|
|
|
|
|
|
Boolean lock = redisTemplate.opsForValue().setIfAbsent(key, goodsId); |
|
|
|
|
|
|
|
|
|
|
|
GoodsMsg goodsMsg = queryDetail(goodsId); |
|
|
|
GoodsMsg goodsMsg = queryDetail(goodsId); |
|
|
|
|
|
|
|
|
|
|
|
if (goodsMsg != null) { |
|
|
|
if (goodsMsg != null) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(Boolean.TRUE.equals(lock)) { |
|
|
|
goodsMsg.setSaleNum(goodsMsg.getSaleNum() + num); |
|
|
|
goodsMsg.setSaleNum(goodsMsg.getSaleNum() + num); |
|
|
|
update(goodsMsg); |
|
|
|
update(goodsMsg); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// 加锁失败,重试
|
|
|
|
|
|
|
|
Thread.sleep(100); |
|
|
|
|
|
|
|
addSaleNum(goodsId , num); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|