From 2ee5067c3a0e160490f5cdefbb0b10600511336b Mon Sep 17 00:00:00 2001 From: Cui Zhi Feng <7426394+wuxixiaocui@user.noreply.gitee.com> Date: 星期二, 03 九月 2024 09:53:01 +0800 Subject: [PATCH] 添加购物车 重复bug --- src/main/java/com/mzl/flower/service/payment/OrderService.java | 137 ++++++++++++++++++++++++++++----------------- 1 files changed, 85 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/mzl/flower/service/payment/OrderService.java b/src/main/java/com/mzl/flower/service/payment/OrderService.java index 44149e3..dcbfd74 100644 --- a/src/main/java/com/mzl/flower/service/payment/OrderService.java +++ b/src/main/java/com/mzl/flower/service/payment/OrderService.java @@ -131,47 +131,71 @@ @Autowired private OrderItemCheckMapper orderItemCheckMapper; + @Autowired + private RedisLockService lockService; + public void changeFlower2Cart(CartSaveDTO dto) { String userId = SecurityUtils.getUserId(); - Cart c = cartMapper.selectOne(new QueryWrapper<Cart>() - .eq("create_by", userId).eq("flower_id", dto.getId())); - if (c == null) { - if (dto.getNum() <= 0) { - throw new ValidationException("数量不能小于等于0,请刷新页面重试"); - } - c = new Cart(); - c.setFlowerId(dto.getId()); - c.setNum(dto.getNum()); - c.create(userId); + String key = userId + "_" + dto.getId(); + boolean lock = lockService.getObjectLock(RedisLockService.LOCK_KEY_CART_, key); + if(!lock){ + return; + } - cartMapper.insert(c); - } else { - c.setNum(c.getNum() + dto.getNum()); - c.update(userId); - if (c.getNum() <= 0) { - cartMapper.deleteById(c.getId()); + try { + Cart c = cartMapper.selectOne(new QueryWrapper<Cart>() + .eq("create_by", userId).eq("flower_id", dto.getId())); + if (c == null) { + if (dto.getNum() <= 0) { + throw new ValidationException("数量不能小于等于0,请刷新页面重试"); + } + c = new Cart(); + c.setFlowerId(dto.getId()); + c.setNum(dto.getNum()); + c.create(userId); + + cartMapper.insert(c); } else { - cartMapper.updateById(c); + c.setNum(c.getNum() + dto.getNum()); + c.update(userId); + if (c.getNum() <= 0) { + cartMapper.deleteById(c.getId()); + } else { + cartMapper.updateById(c); + } } + } finally { + lockService.releaseObjectLock(RedisLockService.LOCK_KEY_CART_, key); } } public void saveFlower2Cart(CartSaveDTO dto) { String userId = SecurityUtils.getUserId(); - Cart c = cartMapper.selectOne(new QueryWrapper<Cart>() - .eq("create_by", userId).eq("flower_id", dto.getId())); - if (c == null) { - c = new Cart(); - c.setFlowerId(dto.getId()); - c.setNum(dto.getNum()); - c.create(userId); - cartMapper.insert(c); - } else { - c.setNum(dto.getNum()); - c.update(userId); + String key = userId + "_" + dto.getId(); + boolean lock = lockService.getObjectLock(RedisLockService.LOCK_KEY_CART_, key); + if(!lock){ + return; + } - cartMapper.updateById(c); + try { + Cart c = cartMapper.selectOne(new QueryWrapper<Cart>() + .eq("create_by", userId).eq("flower_id", dto.getId())); + if (c == null) { + c = new Cart(); + c.setFlowerId(dto.getId()); + c.setNum(dto.getNum()); + c.create(userId); + + cartMapper.insert(c); + } else { + c.setNum(dto.getNum()); + c.update(userId); + + cartMapper.updateById(c); + } + } finally { + lockService.releaseObjectLock(RedisLockService.LOCK_KEY_CART_, key); } } @@ -519,7 +543,27 @@ * 合伙人订单定时发货 */ public void setPartnerOrderSend() { - orderMapper.setPartnerOrderReceive(); + List<Order> orders = orderMapper.getPartnerOrderReceive(); + if(orders == null || orders.size() == 0){ + return; + } + for (Order o : orders) { + //set status_backend = 'RECEIVE', status = 'RECEIVE' + o.setStatusBackend("RECEIVE"); + o.setStatus("RECEIVE"); + //发送微信发货请求 + JSONObject jsonObject = sendWxDeliveryGood(o); + if (jsonObject != null && (int) jsonObject.get("errcode") == 0) { + o.setWxDeliveryGood(true); + o.setWxDeliveryMsg(GsonUtil.toJson(jsonObject)); + } else { + o.setWxDeliveryGood(false); + o.setWxDeliveryMsg(GsonUtil.toJson(jsonObject)); + } + + orderMapper.updateById(o); + } + //orderMapper.setPartnerOrderReceive(); } /** @@ -539,6 +583,17 @@ o.setStatus(Constants.ORDER_STATUS.RECEIVE.name()); o.setStatusBackend(Constants.ORDER_STATUS_BACKEND.RECEIVE.name()); o.update(SecurityUtils.getUserId()); + + //发送微信发货请求 + JSONObject jsonObject = sendWxDeliveryGood(o); + if (jsonObject != null && (int) jsonObject.get("errcode") == 0) { + o.setWxDeliveryGood(true); + o.setWxDeliveryMsg(GsonUtil.toJson(jsonObject)); + } else { + o.setWxDeliveryGood(false); + o.setWxDeliveryMsg(GsonUtil.toJson(jsonObject)); + } + orderMapper.updateById(o); } @@ -787,17 +842,6 @@ o.setStatusBackend(Constants.ORDER_STATUS_BACKEND.EVALUATE.name()); o.setReceiveTime(LocalDateTime.now()); o.update("sys"); - - - //发送微信发货请求 - JSONObject jsonObject = sendWxDeliveryGood(o); - if (jsonObject != null && (int) jsonObject.get("errcode") == 0) { - o.setWxDeliveryGood(true); - o.setWxDeliveryMsg(GsonUtil.toJson(jsonObject)); - } else { - o.setWxDeliveryGood(false); - o.setWxDeliveryMsg(GsonUtil.toJson(jsonObject)); - } orderMapper.updateById(o); } @@ -860,17 +904,6 @@ o.setStatusBackend(Constants.ORDER_STATUS_BACKEND.EVALUATE.name()); o.setReceiveTime(LocalDateTime.now()); o.update(userId); - - //发送微信发货请求 - JSONObject jsonObject = sendWxDeliveryGood(o); - if (jsonObject != null && (int) jsonObject.get("errcode") == 0) { - o.setWxDeliveryGood(true); - o.setWxDeliveryMsg(GsonUtil.toJson(jsonObject)); - } else { - o.setWxDeliveryGood(false); - o.setWxDeliveryMsg(GsonUtil.toJson(jsonObject)); - } - orderMapper.updateById(o); } -- Gitblit v1.9.3