From cf2c30b78ddeb289ae41b6a714afe7df282e821f Mon Sep 17 00:00:00 2001
From: wayn <1669738430@qq.com>
Date: Thu, 10 Aug 2023 23:33:28 +0800
Subject: [PATCH] =?UTF-8?q?feat(=E5=95=86=E5=9F=8E):=20=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 2 +-
.../api/controller/shop/GoodsController.java | 5 +-
waynboot-common/pom.xml | 6 --
.../wayn/common/constant/SysConstants.java | 15 -----
.../wayn/common/core/domain/vo/OrderVO.java | 5 --
.../com/wayn/common/enums/ReturnCodeEnum.java | 2 +
.../data/elastic/constant/EsConstants.java | 4 --
.../data/redis/constant/RedisKeyEnum.java | 28 ++++++++
.../wayn/data/redis/manager/RedisLock.java | 6 +-
.../api/service/impl/CartServiceImpl.java | 67 +++++++++++--------
.../api/service/impl/OrderServiceImpl.java | 14 ++--
11 files changed, 85 insertions(+), 69 deletions(-)
create mode 100644 waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/constant/RedisKeyEnum.java
diff --git a/pom.xml b/pom.xml
index a848a1f..771b628 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
8.0.30
6.2.3.RELEASE
7.14.0
- 3.5.3.1
+ 3.5.3.2
1.2.16
1.18.26
2.11.0
diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java
index 3d217fe..9d82d0b 100644
--- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java
+++ b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java
@@ -12,6 +12,7 @@ import com.wayn.common.util.file.FileUtils;
import com.wayn.data.elastic.constant.EsConstants;
import com.wayn.data.elastic.manager.ElasticDocument;
import com.wayn.data.elastic.manager.ElasticEntity;
+import com.wayn.data.redis.constant.RedisKeyEnum;
import com.wayn.data.redis.manager.RedisCache;
import com.wayn.data.redis.manager.RedisLock;
import lombok.AllArgsConstructor;
@@ -73,7 +74,7 @@ public class GoodsController extends BaseController {
public R syncEs() {
boolean flag = false;
try {
- boolean lock = redisLock.lock(EsConstants.ES_GOODS_INDEX_KEY, 2);
+ boolean lock = redisLock.lock(RedisKeyEnum.ES_SYNC_CACHE.getKey(), 2);
if (!lock) {
throw new BusinessException("加锁失败");
}
@@ -105,7 +106,7 @@ public class GoodsController extends BaseController {
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
- redisLock.unLock(EsConstants.ES_GOODS_INDEX_KEY);
+ redisLock.unLock(RedisKeyEnum.ES_SYNC_CACHE.getKey());
}
return R.result(flag);
}
diff --git a/waynboot-common/pom.xml b/waynboot-common/pom.xml
index f7fb274..e0b4f8c 100644
--- a/waynboot-common/pom.xml
+++ b/waynboot-common/pom.xml
@@ -24,12 +24,6 @@
${spring-boot.version}
-
- org.springframework.boot
- spring-boot-starter-data-redis
- ${spring-boot.version}
-
-
org.springframework.boot
spring-boot-starter-security
diff --git a/waynboot-common/src/main/java/com/wayn/common/constant/SysConstants.java b/waynboot-common/src/main/java/com/wayn/common/constant/SysConstants.java
index 8283299..6d8ac7f 100644
--- a/waynboot-common/src/main/java/com/wayn/common/constant/SysConstants.java
+++ b/waynboot-common/src/main/java/com/wayn/common/constant/SysConstants.java
@@ -9,11 +9,6 @@ public class SysConstants {
public static final String DEFAULT_AVATAR = "/upload/54989c410a88af0f1c2a8e5ec87af675.webp";
- /**
- * 缓存前缀, 统一项目缓存前缀
- */
- public static final String CACHE_PREFIX = "waynboot-mall:";
-
/**
* 用户默认密码
*/
@@ -78,14 +73,4 @@ public class SysConstants {
* 校验返回结果码 0 不存在 1 已经存在
*/
public final static String NOT_UNIQUE = "1";
-
- /**
- * string类型boolean返回值,成功默认为success
- */
- public final static String STRING_TRUE = "true";
-
- /**
- * string类型boolean返回值,失败默认为error
- */
- public final static String STRING_FALSE = "false";
}
diff --git a/waynboot-common/src/main/java/com/wayn/common/core/domain/vo/OrderVO.java b/waynboot-common/src/main/java/com/wayn/common/core/domain/vo/OrderVO.java
index 3961da7..7cd731e 100644
--- a/waynboot-common/src/main/java/com/wayn/common/core/domain/vo/OrderVO.java
+++ b/waynboot-common/src/main/java/com/wayn/common/core/domain/vo/OrderVO.java
@@ -20,11 +20,6 @@ public class OrderVO {
*/
private List cartIdArr;
- /**
- * 用户id
- */
- private Long userId;
-
/**
* 地址id
*/
diff --git a/waynboot-common/src/main/java/com/wayn/common/enums/ReturnCodeEnum.java b/waynboot-common/src/main/java/com/wayn/common/enums/ReturnCodeEnum.java
index 4e63505..2a0eada 100644
--- a/waynboot-common/src/main/java/com/wayn/common/enums/ReturnCodeEnum.java
+++ b/waynboot-common/src/main/java/com/wayn/common/enums/ReturnCodeEnum.java
@@ -41,6 +41,8 @@ public enum ReturnCodeEnum {
ORDER_NOT_SUPPORT_PAYWAY_ERROR(5010, "不支持的支付类型"),
ORDER_HAS_CREATED_ERROR(5011, "订单已经创建过了 "),
ORDER_USER_NOT_SAME_ERROR(5012, "订单用户和当前登陆用户不一致"),
+ ORDER_ERROR_CART_EMPTY_ERROR(5013, "下单失败,当前购物车无数据"),
+ ORDER_ERROR_ADDRESS_ERROR(50143, "下单失败,当前地址错误"),
// 用户错误
USER_NOT_EXISTS_ERROR(5101, "用户不存在"),
diff --git a/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/constant/EsConstants.java b/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/constant/EsConstants.java
index a01327d..bb2beae 100644
--- a/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/constant/EsConstants.java
+++ b/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/constant/EsConstants.java
@@ -6,10 +6,6 @@ public class EsConstants {
* 商品的索引名称
*/
public static final String ES_GOODS_INDEX = "goods";
- /**
- * 商品索引创建缓存,创建成功后删除
- */
- public static final String ES_GOODS_INDEX_KEY = "es_goods";
/**
* es商品索引的创建语句所在文件位置
*/
diff --git a/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/constant/RedisKeyEnum.java b/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/constant/RedisKeyEnum.java
new file mode 100644
index 0000000..c1a9588
--- /dev/null
+++ b/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/constant/RedisKeyEnum.java
@@ -0,0 +1,28 @@
+package com.wayn.data.redis.constant;
+
+import lombok.Getter;
+
+/**
+ * @author: waynaqua
+ * @date: 2023/8/10 22:28
+ */
+@Getter
+public enum RedisKeyEnum {
+
+ ES_SYNC_CACHE(CacheConstants.CACHE_PREFIX + "es_sync_cache", 3600),
+ CART_LOCK(CacheConstants.CACHE_PREFIX + "cart_lock:", 3600),
+ ;
+
+ private String key;
+ private Integer expireSecond;
+
+ RedisKeyEnum(String key, Integer expireSecond) {
+ this.key = key;
+ this.expireSecond = expireSecond;
+ }
+
+ public String getKey(Object param) {
+ return this.getKey() + param;
+ }
+
+}
diff --git a/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/manager/RedisLock.java b/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/manager/RedisLock.java
index d6a329c..8e7f657 100644
--- a/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/manager/RedisLock.java
+++ b/waynboot-data/waynboot-data-redis/src/main/java/com/wayn/data/redis/manager/RedisLock.java
@@ -61,9 +61,9 @@ public class RedisLock {
public boolean lock(String key, Integer timeout) {
Integer timeoutTmp;
if (timeout == null) {
- timeoutTmp = DEFAULT_TIME_OUT;
+ timeoutTmp = DEFAULT_TIME_OUT * 1000;
} else {
- timeoutTmp = timeout;
+ timeoutTmp = timeout * 1000;
}
String nanoId;
if (stringThreadLocal.get() != null) {
@@ -84,7 +84,7 @@ public class RedisLock {
if (result != null && result == 2) {
ThreadUtil.shutdownAndAwaitTermination(scheduledExecutorService);
}
- }, 0, timeoutTmp / 3, TimeUnit.SECONDS);
+ }, timeoutTmp / 3, timeoutTmp / 3, TimeUnit.MILLISECONDS);
}
return flag;
}
diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/CartServiceImpl.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/CartServiceImpl.java
index 97136d6..1383bde 100644
--- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/CartServiceImpl.java
+++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/CartServiceImpl.java
@@ -15,6 +15,8 @@ import com.wayn.common.enums.ReturnCodeEnum;
import com.wayn.common.exception.BusinessException;
import com.wayn.common.util.R;
import com.wayn.common.util.bean.MyBeanUtil;
+import com.wayn.data.redis.constant.RedisKeyEnum;
+import com.wayn.data.redis.manager.RedisLock;
import com.wayn.mobile.api.domain.Cart;
import com.wayn.mobile.api.mapper.CartMapper;
import com.wayn.mobile.api.service.ICartService;
@@ -43,6 +45,8 @@ import java.util.stream.Collectors;
@AllArgsConstructor
public class CartServiceImpl extends ServiceImpl implements ICartService {
+ private RedisLock redisLock;
+
private IGoodsService iGoodsService;
private IGoodsProductService iGoodsProductService;
@@ -71,36 +75,43 @@ public class CartServiceImpl extends ServiceImpl implements IC
}
Long userId = MobileSecurityUtils.getLoginUser().getMember().getId();
GoodsProduct product = iGoodsProductService.getById(productId);
- Cart existsCart = checkExistsGoods(userId, goodsId, productId);
- if (Objects.isNull(existsCart)) {
- if (Objects.isNull(product) || product.getNumber() < number) {
- return R.error(ReturnCodeEnum.GOODS_STOCK_NOT_ENOUGH_ERROR);
- }
-
- cart.setGoodsSn(goods.getGoodsSn());
- cart.setGoodsName(goods.getName());
- if (StringUtils.isEmpty(product.getUrl())) {
- cart.setPicUrl(goods.getPicUrl());
+ boolean lock = redisLock.lock(RedisKeyEnum.CART_LOCK.getKey(userId), 2);
+ if (!lock) {
+ throw new BusinessException("加锁失败");
+ }
+ try {
+ Cart existsCart = checkExistsGoods(userId, goodsId, productId);
+ if (Objects.isNull(existsCart)) {
+ if (Objects.isNull(product) || number > product.getNumber()) {
+ return R.error(ReturnCodeEnum.GOODS_STOCK_NOT_ENOUGH_ERROR);
+ }
+ cart.setGoodsSn(goods.getGoodsSn());
+ cart.setGoodsName(goods.getName());
+ if (StringUtils.isEmpty(product.getUrl())) {
+ cart.setPicUrl(goods.getPicUrl());
+ } else {
+ cart.setPicUrl(product.getUrl());
+ }
+ cart.setPrice(product.getPrice());
+ cart.setSpecifications((product.getSpecifications()));
+ cart.setUserId(Math.toIntExact(userId));
+ cart.setChecked(true);
+ cart.setRemark(goods.getBrief());
+ cart.setCreateTime(LocalDateTime.now());
+ save(cart);
} else {
- cart.setPicUrl(product.getUrl());
- }
- cart.setPrice(product.getPrice());
- cart.setSpecifications((product.getSpecifications()));
- cart.setUserId(Math.toIntExact(userId));
- cart.setChecked(true);
- cart.setRemark(goods.getBrief());
- cart.setCreateTime(LocalDateTime.now());
- save(cart);
- } else {
- int num = existsCart.getNumber() + number;
- if (num > product.getNumber()) {
- return R.error(ReturnCodeEnum.GOODS_STOCK_NOT_ENOUGH_ERROR);
- }
- existsCart.setNumber(num);
- cart.setUpdateTime(LocalDateTime.now());
- if (!updateById(existsCart)) {
- return R.error();
+ int num = existsCart.getNumber() + number;
+ if (num > product.getNumber()) {
+ return R.error(ReturnCodeEnum.GOODS_STOCK_NOT_ENOUGH_ERROR);
+ }
+ existsCart.setNumber(num);
+ cart.setUpdateTime(LocalDateTime.now());
+ if (!updateById(existsCart)) {
+ return R.error();
+ }
}
+ } finally {
+ redisLock.unLock(RedisKeyEnum.CART_LOCK.getKey(userId));
}
return R.success();
}
diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/OrderServiceImpl.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/OrderServiceImpl.java
index aea21dc..6a2cc4c 100644
--- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/OrderServiceImpl.java
+++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/service/impl/OrderServiceImpl.java
@@ -193,7 +193,12 @@ public class OrderServiceImpl extends ServiceImpl implements
public R asyncSubmit(OrderVO orderVO) {
OrderDTO orderDTO = new OrderDTO();
MyBeanUtil.copyProperties(orderVO, orderDTO);
- Long userId = orderDTO.getUserId();
+ Long userId = MobileSecurityUtils.getUserId();
+ Long addressId = orderDTO.getAddressId();
+ Address address = iAddressService.getById(addressId);
+ if (!Objects.equals(address.getMemberId(), userId)) {
+ throw new BusinessException(ReturnCodeEnum.ORDER_ERROR_ADDRESS_ERROR);
+ }
// 获取用户订单商品,为空默认取购物车已选中商品
List cartIdArr = orderDTO.getCartIdArr();
@@ -203,9 +208,8 @@ public class OrderServiceImpl extends ServiceImpl implements
} else {
checkedGoodsList = iCartService.listByIds(cartIdArr);
}
-
if (CollectionUtils.isEmpty(checkedGoodsList)) {
- throw new BusinessException(ReturnCodeEnum.ORDER_SUBMIT_ERROR);
+ throw new BusinessException(ReturnCodeEnum.ORDER_ERROR_CART_EMPTY_ERROR);
}
// 商品费用
@@ -272,8 +276,8 @@ public class OrderServiceImpl extends ServiceImpl implements
checkedGoodsList = iCartService.listByIds(cartIdArr);
}
- if (checkedGoodsList.size() == 0) {
- throw new BusinessException(ReturnCodeEnum.ORDER_SUBMIT_ERROR);
+ if (checkedGoodsList.isEmpty()) {
+ throw new BusinessException(ReturnCodeEnum.ORDER_ERROR_CART_EMPTY_ERROR);
}
// 商品货品库存数量减少