修订下单锁问题;调整商品查询排序为更新时间

This commit is contained in:
DB 2023-11-02 18:31:50 +08:00
parent 63bacf0cd8
commit 5984409601
3 changed files with 35 additions and 17 deletions

View File

@ -1,22 +1,24 @@
package com.cpop.mall.business.controller.mini;
import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.R;
import com.cpop.core.utils.SpringUtils;
import com.cpop.mall.business.bo.ProductPageBo;
import com.cpop.mall.business.service.ProductService;
import com.cpop.mall.business.vo.MiniProductPageVo;
import com.cpop.mall.business.vo.ProductEvaluateVo;
import com.cpop.mall.business.vo.ProductPageVo;
import com.cpop.mall.business.vo.StoreListVo;
import com.cpop.system.business.entity.Store;
import com.cpop.system.business.service.StoreService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
/**
@ -74,4 +76,19 @@ public class MiniProductController {
return R.ok(page);
}
/**
* @descriptions 根据校区ids获取校区信息
* @author DB
* @date 2023/11/02 17:11
* @param storeIds 校区id列表
* @return: com.cpop.core.base.R<java.util.List<com.cpop.mall.business.vo.StoreListVo>>
*/
@PostMapping("/getStoreByIds")
@ApiOperation("根据校区ids获取校区信息")
public R<List<StoreListVo>> getMiniProductPage(@RequestParam("storeIds") String storeIds) {
List<Store> stores = SpringUtils.getBean(StoreService.class).listByIds(Arrays.asList(storeIds.split(",")));
List<StoreListVo> list = BeanUtils.mapToList(stores, StoreListVo.class);
return R.ok(list);
}
}

View File

@ -144,8 +144,9 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
Lock userIdLock = redisService.distributedLock(MallRedisConstant.IDEMPOTENT_LOCK_USER_PAY + loginUserInfo.getString("userId"));
if (userIdLock.tryLock()) {
//检查库存
Map<String, Integer> recordNumIsEnough = recordNumIsEnough(bo.getPlaceOrderDetailList());
Map<String, Integer> recordNumIsEnough = null;
try {
recordNumIsEnough= recordNumIsEnough(bo.getPlaceOrderDetailList());
Order order = BeanUtils.mapToClass(bo, Order.class);
List<OrderDetail> orderDetails = BeanUtils.mapToList(bo.getPlaceOrderDetailList(), OrderDetail.class);
//规格记录ids
@ -202,17 +203,19 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}
} catch (Exception e) {
//回滚库存
recordNumIsEnough.forEach((key, value) -> {
redisService.longIncrement(MallRedisConstant.STOCK_RECORD_NUM + key, value.longValue());
});
if (recordNumIsEnough != null){
recordNumIsEnough.forEach((key, value) -> {
redisService.longIncrement(MallRedisConstant.STOCK_RECORD_NUM + key, value.longValue());
});
}
throw new ServiceException(e.getMessage());
}finally {
} finally {
//释放锁
userIdLock.unlock();
}
}else {
//获取锁失败直接返回空
return null;
throw new ServiceException("请勿重复下单");
}
}
@ -232,7 +235,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
productRecordService.updateBatch(productRecords);
//TODO: 通知到云
//修改订单状态
return this.updateChain().set(ORDER.ORDER_STATUS, 3).where(ORDER.ID.eq(orderId)).update();
return this.updateChain().set(ORDER.ORDER_STATUS, 1).where(ORDER.ID.eq(orderId)).update();
}
@ -243,7 +246,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
* @param list 订单详情
* @return: Map<String, Integer> 预库存记录
*/
private Map<String, Integer> recordNumIsEnough(List<PlaceOrderBo.PlaceOrderDetail> list) {
private Map<String, Integer> recordNumIsEnough(List<PlaceOrderBo.PlaceOrderDetail> list) throws ServiceException {
RedisService redisService = SpringUtils.getBean(RedisService.class);
Map<String, Integer> stockNumMap = new HashMap<>();
//遍历库存

View File

@ -92,9 +92,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
.and(PRODUCT.IS_UP.eq(bo.getIsUp()))
.and(PRODUCT.STORE_IDS.like(bo.getAuthorizedStoreId()))
//置顶
.orderBy(PRODUCT.IS_TOP.desc())
.orderBy(PRODUCT.CREATE_TIME.desc())
.orderBy(pageDomain.getOrderByColumn()),
.orderBy(PRODUCT.IS_TOP.desc(),PRODUCT.UPDATE_TIME.desc()),
ProductPageVo.class,
//子查询
item -> item.field(ProductPageVo::getTradeInfo)
@ -133,7 +131,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
//价格排序
if (null != bo.getPriceOrder()){
if (bo.getPriceOrder()) {
queryWrapper.orderBy(PRODUCT.MAX_PRICE.desc());
queryWrapper.orderBy(PRODUCT.MIN_PRICE.desc());
} else {
queryWrapper.orderBy(PRODUCT.MIN_PRICE.asc());
}
@ -149,7 +147,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
.and(PRODUCT.PRODUCT_NAME.like(bo.getProductName()))
.and(PRODUCT.BUY_RESTRICT.eq(bo.getBuyRestrict()))
.and(PRODUCT.PAY_TYPE.eq(bo.getPayType()))
.orderBy(pageDomain.getOrderByColumn()),
.orderBy(PRODUCT.IS_TOP.desc(), PRODUCT.UPDATE_TIME.desc()),
MiniProductPageVo.class);
}