From a4f08f38caa261ab103f71c5fde9400381143fbb Mon Sep 17 00:00:00 2001
From: gongzuming <gongzuming>
Date: 星期二, 10 九月 2024 14:28:30 +0800
Subject: [PATCH] 优化兑换商品
---
src/main/java/com/mzl/flower/service/point/PointGoodsService.java | 110 +++++++++++++++++++++++++++++++-----------------------
1 files changed, 63 insertions(+), 47 deletions(-)
diff --git a/src/main/java/com/mzl/flower/service/point/PointGoodsService.java b/src/main/java/com/mzl/flower/service/point/PointGoodsService.java
index 4c2ab59..91da73f 100644
--- a/src/main/java/com/mzl/flower/service/point/PointGoodsService.java
+++ b/src/main/java/com/mzl/flower/service/point/PointGoodsService.java
@@ -18,6 +18,7 @@
import com.mzl.flower.mapper.point.PointGoodsMapper;
import com.mzl.flower.mapper.point.PointGoodsRecordMapper;
import com.mzl.flower.service.BaseService;
+import com.mzl.flower.service.payment.RedisLockService;
import com.mzl.flower.utils.UUIDGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -43,6 +44,9 @@
@Autowired
private CustomerPointDetailMapper customerPointDetailMapper;
+
+ @Autowired
+ private RedisLockService lockService;
public Long addPointGoods(PointGoodsCreateDTO dto){
PointGoods p = new PointGoods();
@@ -117,57 +121,69 @@
}
}
- public synchronized void exchangeGoods(ExchangeGoodsDTO dto) {
- PointGoods p = pointGoodsMapper.selectById(dto.getGoodsId());
- if(p == null){
- throw new ValidationException("商品未找到");
+ public void exchangeGoods(ExchangeGoodsDTO dto) {
+ String key="EXCHANGE_GOODS:"+dto.getGoodsId()+":"+SecurityUtils.getUserId();
+ boolean lock = lockService.getObjectLock(key, "");
+ if(!lock){
+ throw new ValidationException("系统操作频繁,请稍后重试");
}
- if(!Constants.POINT_GOODS_STATUS.A.name().equals(p.getStatus())){
- throw new ValidationException("商品未上架");
- }
- if(p.getStock()< dto.getNum()){
- throw new ValidationException("商品库存不足");
- }
- CustomerPoint cp = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>()
- .eq(CustomerPoint::getUserId, SecurityUtils.getUserId()));
- if(cp == null || (cp.getTotalPoint()-cp.getUsedPoint()-cp.getExpiredPoint()) < p.getPoint() * dto.getNum()){
- throw new ValidationException("积分不足");
- }
- //记录兑换记录
- PointGoodsRecord record = new PointGoodsRecord();
- record.setUserId(SecurityUtils.getUserId());
- record.setCustomerId(getCustomerByUserId(SecurityUtils.getUserId()).getId());
- record.setGoodsId(dto.getGoodsId());
- record.setNum(dto.getNum());
- record.setPoint(p.getPoint());
- record.setName(p.getName());
- record.setPictures(p.getPictures());
- record.setDescription(p.getDescription());
- record.setTotalPoint(p.getPoint() * dto.getNum());
- record.setCover(p.getCover());
- record.setRedeemCode(UUIDGenerator.getUUID());
- record.setStatus(Constants.POINT_GOODS_RECORD_STATUS.A.name());//未使用
- pointGoodsRecordMapper.insert(record);
+ try {
+ PointGoods p = pointGoodsMapper.selectById(dto.getGoodsId());
+ if(p == null){
+ throw new ValidationException("商品未找到");
+ }
+ if(!Constants.POINT_GOODS_STATUS.A.name().equals(p.getStatus())){
+ throw new ValidationException("商品未上架");
+ }
+ if(p.getStock()< dto.getNum()){
+ throw new ValidationException("商品库存不足");
+ }
+ CustomerPoint cp = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>()
+ .eq(CustomerPoint::getUserId, SecurityUtils.getUserId()));
+ if(cp == null || (cp.getTotalPoint()-cp.getUsedPoint()-cp.getExpiredPoint()) < p.getPoint() * dto.getNum()){
+ throw new ValidationException("积分不足");
+ }
+ //记录兑换记录
+ PointGoodsRecord record = new PointGoodsRecord();
+ record.setUserId(SecurityUtils.getUserId());
+ record.setCustomerId(getCustomerByUserId(SecurityUtils.getUserId()).getId());
+ record.setGoodsId(dto.getGoodsId());
+ record.setNum(dto.getNum());
+ record.setPoint(p.getPoint());
+ record.setName(p.getName());
+ record.setPictures(p.getPictures());
+ record.setDescription(p.getDescription());
+ record.setTotalPoint(p.getPoint() * dto.getNum());
+ record.setCover(p.getCover());
+ record.setRedeemCode(UUIDGenerator.getUUID());
+ record.setStatus(Constants.POINT_GOODS_RECORD_STATUS.A.name());//未使用
+ pointGoodsRecordMapper.insert(record);
- //更新积分汇总
- cp.setUsedPoint(cp.getUsedPoint()+record.getTotalPoint());
- customerPointMapper.updateById(cp);
+ //更新积分汇总
+ cp.setUsedPoint(cp.getUsedPoint()+record.getTotalPoint());
+ customerPointMapper.updateById(cp);
- //记录积分明细
- CustomerPointDetail detail = new CustomerPointDetail();
- detail.setUserId(SecurityUtils.getUserId());
- detail.setCustomerId(record.getCustomerId());
- detail.setPoint(record.getTotalPoint());
- detail.setChangeType(Constants.POINT_CHANGE_TYPE.reduce.name());
- detail.setType(Constants.POINT_TYPE.exchange.name());
- detail.setRecordDate(LocalDate.now());
- detail.setRemarks(record.getName());
- detail.create(SecurityUtils.getUserId());
- customerPointDetailMapper.insert(detail);
+ //记录积分明细
+ CustomerPointDetail detail = new CustomerPointDetail();
+ detail.setUserId(SecurityUtils.getUserId());
+ detail.setCustomerId(record.getCustomerId());
+ detail.setPoint(record.getTotalPoint());
+ detail.setChangeType(Constants.POINT_CHANGE_TYPE.reduce.name());
+ detail.setType(Constants.POINT_TYPE.exchange.name());
+ detail.setRecordDate(LocalDate.now());
+ detail.setRemarks(record.getName());
+ detail.create(SecurityUtils.getUserId());
+ customerPointDetailMapper.insert(detail);
- //更新库存
- p.setStock(p.getStock()- dto.getNum());
- pointGoodsMapper.updateById(p);
+ //更新库存
+ p.setStock(p.getStock()- dto.getNum());
+ pointGoodsMapper.updateById(p);
+ }catch (Exception e){
+ throw new ValidationException("兑换失败");
+ }finally {
+ lockService.releaseObjectLock(key,"");
+ }
+
}
public Page<PointGoodsRecordDTO> myExchangeGoods(QueryExchangeGoodsDTO dto, Page page) {
--
Gitblit v1.9.3