feat(商城): 代码优化

master
wayn 1 year ago
parent 1ed26e59b0
commit cf2c30b78d

@ -28,7 +28,7 @@
<mysql.connector.java.version>8.0.30</mysql.connector.java.version>
<lettuce.version>6.2.3.RELEASE</lettuce.version>
<elasticsearch.version>7.14.0</elasticsearch.version>
<mybatis-plus.version>3.5.3.1</mybatis-plus.version>
<mybatis-plus.version>3.5.3.2</mybatis-plus.version>
<druid.version>1.2.16</druid.version>
<lombok.version>1.18.26</lombok.version>
<commons.io.version>2.11.0</commons.io.version>

@ -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);
}

@ -24,12 +24,6 @@
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>

@ -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";
/**
* stringbooleansuccess
*/
public final static String STRING_TRUE = "true";
/**
* stringbooleanerror
*/
public final static String STRING_FALSE = "false";
}

@ -20,11 +20,6 @@ public class OrderVO {
*/
private List<Long> cartIdArr;
/**
* id
*/
private Long userId;
/**
* id
*/

@ -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, "用户不存在"),

@ -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
*/

@ -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;
}
}

@ -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;
}

@ -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<CartMapper, Cart> implements ICartService {
private RedisLock redisLock;
private IGoodsService iGoodsService;
private IGoodsProductService iGoodsProductService;
@ -71,12 +75,16 @@ public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements IC
}
Long userId = MobileSecurityUtils.getLoginUser().getMember().getId();
GoodsProduct product = iGoodsProductService.getById(productId);
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) || product.getNumber() < number) {
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())) {
@ -102,6 +110,9 @@ public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements IC
return R.error();
}
}
} finally {
redisLock.unLock(RedisKeyEnum.CART_LOCK.getKey(userId));
}
return R.success();
}

@ -193,7 +193,12 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> 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<Long> cartIdArr = orderDTO.getCartIdArr();
@ -203,9 +208,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> 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<OrderMapper, Order> 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);
}
// 商品货品库存数量减少

Loading…
Cancel
Save