|
|
@ -1,5 +1,8 @@ |
|
|
|
package com.hfkj.service.goods.impl; |
|
|
|
package com.hfkj.service.goods.impl; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.hfkj.common.exception.ErrorCode; |
|
|
|
|
|
|
|
import com.hfkj.common.exception.ErrorHelp; |
|
|
|
|
|
|
|
import com.hfkj.common.exception.SysCode; |
|
|
|
import com.hfkj.dao.GoodsMsgMapper; |
|
|
|
import com.hfkj.dao.GoodsMsgMapper; |
|
|
|
import com.hfkj.entity.GoodsBrand; |
|
|
|
import com.hfkj.entity.GoodsBrand; |
|
|
|
import com.hfkj.entity.GoodsMsg; |
|
|
|
import com.hfkj.entity.GoodsMsg; |
|
|
@ -101,33 +104,41 @@ public class GoodsMsgServiceImpl implements GoodsMsgService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void editSaleNum(Long goodsId, Integer num , Integer type) throws Exception { |
|
|
|
public void editSaleNum(Long goodsId, Integer num , Integer type) { |
|
|
|
|
|
|
|
|
|
|
|
String key = "GOODS" + goodsId; |
|
|
|
String key = "GOODS" + goodsId; |
|
|
|
|
|
|
|
|
|
|
|
// 分布式锁占坑
|
|
|
|
try { |
|
|
|
Boolean lock = redisTemplate.opsForValue().setIfAbsent(key, 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)) { |
|
|
|
if(Boolean.TRUE.equals(lock)) { |
|
|
|
|
|
|
|
|
|
|
|
if (type == 1) { |
|
|
|
if (type == 1) { |
|
|
|
goodsMsg.setSaleNum(goodsMsg.getSaleNum() + num); |
|
|
|
goodsMsg.setSaleNum(goodsMsg.getSaleNum() + num); |
|
|
|
} else if (type == 2){ |
|
|
|
} else if (type == 2){ |
|
|
|
goodsMsg.setSaleNum(goodsMsg.getSaleNum() - num); |
|
|
|
goodsMsg.setSaleNum(goodsMsg.getSaleNum() - num); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
update(goodsMsg); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// 加锁失败,重试
|
|
|
|
|
|
|
|
Thread.sleep(100); |
|
|
|
|
|
|
|
editSaleNum(goodsId , num , type); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
update(goodsMsg); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// 加锁失败,重试
|
|
|
|
|
|
|
|
Thread.sleep(100); |
|
|
|
|
|
|
|
editSaleNum(goodsId , num , type); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
// 删除key,释放锁
|
|
|
|
|
|
|
|
redisTemplate.delete(key); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|