From b61182b5c602d0bb4af7c68cb51dbfa1eb5a755f Mon Sep 17 00:00:00 2001 From: gongzuming <gongzuming> Date: 星期四, 19 九月 2024 21:53:59 +0800 Subject: [PATCH] 优化 --- src/main/java/com/mzl/flower/service/point/PointGoodsService.java | 23 +++++++++++++++++------ 1 files changed, 17 insertions(+), 6 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 91da73f..e56ab9f 100644 --- a/src/main/java/com/mzl/flower/service/point/PointGoodsService.java +++ b/src/main/java/com/mzl/flower/service/point/PointGoodsService.java @@ -19,6 +19,7 @@ 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.DateUtils; import com.mzl.flower.utils.UUIDGenerator; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -26,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.List; @Service @@ -114,6 +116,12 @@ continue; } + if(Constants.POINT_GOODS_STATUS.A.name().equals(status)){ + if(p.getStock() == null || p.getStock() <= 0){ + throw new ValidationException("商品" + p.getName() + "库存为0不能上架"); + } + } + p.setStatus(status); p.update(SecurityUtils.getUserId()); pointGoodsMapper.updateById(p); @@ -135,12 +143,12 @@ if(!Constants.POINT_GOODS_STATUS.A.name().equals(p.getStatus())){ throw new ValidationException("商品未上架"); } - if(p.getStock()< dto.getNum()){ + if(p.getStock() <=0 || 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()){ + if(cp == null || (cp.getTotalPoint()-cp.getUsedPoint()-cp.getExpiredPoint()-cp.getDeductionPoint()) < p.getPoint() * dto.getNum()){ throw new ValidationException("积分不足"); } //记录兑换记录 @@ -151,12 +159,16 @@ record.setNum(dto.getNum()); record.setPoint(p.getPoint()); record.setName(p.getName()); - record.setPictures(p.getPictures()); + record.setPictures(p.getCover()); 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());//未使用 + LocalDate expireDate = LocalDate.now().plusYears(1); + String expireDateStr = DateUtils.toString(expireDate,"yyyy-MM-dd"); + record.setExpireTime(DateUtils.dateToLocalDateTime(expireDateStr,false)); + record.create(SecurityUtils.getUserId()); pointGoodsRecordMapper.insert(record); //更新积分汇总 @@ -178,8 +190,6 @@ //更新库存 p.setStock(p.getStock()- dto.getNum()); pointGoodsMapper.updateById(p); - }catch (Exception e){ - throw new ValidationException("兑换失败"); }finally { lockService.releaseObjectLock(key,""); } @@ -208,6 +218,7 @@ record.setOrderId(orderId); pointGoodsRecordMapper.updateById(record); } + public void revertExchangeGoods(Long recordId) { PointGoodsRecord record = pointGoodsRecordMapper.selectById(recordId); if(record == null){ @@ -216,7 +227,7 @@ if(!Constants.POINT_GOODS_RECORD_STATUS.U.name().equals(record.getStatus())){ throw new ValidationException("兑换券未使用或过期"); } - if(!SecurityUtils.getUserId().equals(record.getUserId())){ + if(SecurityUtils.getUserId() != null && !SecurityUtils.getUserId().equals(record.getUserId())){ throw new ValidationException("兑换券不属于当前用户"); } record.setStatus(Constants.POINT_GOODS_RECORD_STATUS.A.name()); -- Gitblit v1.9.3