fix(StockController.class): 锁库存异常自定义响应

This commit is contained in:
有来技术 2021-10-20 00:28:07 +08:00
parent e4f81463d8
commit 831b3e45f0
4 changed files with 15 additions and 13 deletions

View File

@ -189,7 +189,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
System.out.println(goodsResult);
// 锁定库存
Result<Boolean> lockResult = stockFeignClient.lockStock(skuLockList);
Result lockResult = stockFeignClient.lockStock(skuLockList);
Assert.isTrue(Result.isSuccess(lockResult), "锁定商品库存失败:{}", lockResult.getMsg());
// 创建订单(状态待支付)

View File

@ -38,9 +38,8 @@ public class StockController {
@ApiOperation(value = "锁定库存")
@PutMapping("/_lock")
public Result<Boolean> lockStock(@RequestBody List<LockStockDTO> list) {
boolean result = iPmsSkuService.lockStock(list);
return Result.judge(result);
public Result lockStock(@RequestBody List<LockStockDTO> list) {
return iPmsSkuService.lockStock(list);
}

View File

@ -1,6 +1,7 @@
package com.youlai.mall.pms.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.youlai.common.result.Result;
import com.youlai.mall.pms.pojo.entity.PmsSku;
import com.youlai.mall.pms.pojo.dto.app.SkuDTO;
import com.youlai.mall.pms.pojo.dto.app.LockStockDTO;
@ -12,12 +13,12 @@ public interface IPmsSkuService extends IService<PmsSku> {
/**
* 锁定库存
*/
boolean lockStock(List<LockStockDTO> list);
Result lockStock(List<LockStockDTO> list);
/**
* 锁定库存
*/
boolean lockStockTcc(List<LockStockDTO> list);
Boolean lockStockTcc(List<LockStockDTO> list);
/**
* 解锁库存

View File

@ -7,6 +7,7 @@ import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.common.result.Result;
import com.youlai.common.web.exception.BizException;
import com.youlai.mall.pms.mapper.PmsSkuMapper;
import com.youlai.mall.pms.pojo.entity.PmsSku;
@ -41,22 +42,23 @@ public class PmsSkuServiceImpl extends ServiceImpl<PmsSkuMapper, PmsSku> impleme
@Override
@GlobalTransactional
public boolean lockStockTcc(List<LockStockDTO> skuLockList) {
public Boolean lockStockTcc(List<LockStockDTO> skuLockList) {
seataTccSkuService.prepareSkuLockList(null,skuLockList);
seataTccSkuService.prepareSkuLockList(null, skuLockList);
String orderToken = skuLockList.get(0).getOrderToken();
redisTemplate.opsForValue().set(LOCKED_STOCK_PREFIX + orderToken, JSONUtil.toJsonStr(skuLockList));
return true;
}
/**
* 创建订单时锁定库存
*/
@Override
public boolean lockStock(List<LockStockDTO> skuLockList) {
public Result lockStock(List<LockStockDTO> skuLockList) {
log.info("=======================创建订单,开始锁定商品库存=======================");
log.info("锁定商品信息:{}", skuLockList.toString());
if (CollectionUtil.isEmpty(skuLockList)) {
throw new BizException("锁定的商品列表为空");
return Result.failed("锁定的商品列表为空");
}
//prepareSkuLockList(null, skuLockList);
// 锁定商品
@ -87,14 +89,14 @@ public class PmsSkuServiceImpl extends ServiceImpl<PmsSkuMapper, PmsSku> impleme
.setSql("locked_stock = locked_stock - " + item.getCount()))
);
// 提示订单哪些商品库存不足
List<Long> ids = unlockSkuList.stream().map(LockStockDTO::getSkuId).collect(Collectors.toList());
throw new BizException("商品" + ids.toString() + "库存不足");
String ids = unlockSkuList.stream().map(sku -> sku.getSkuId().toString()).collect(Collectors.joining(","));
return Result.failed("商品" + ids + "库存不足");
}
// 将锁定的商品保存至Redis中
String orderToken = skuLockList.get(0).getOrderToken();
redisTemplate.opsForValue().set(LOCKED_STOCK_PREFIX + orderToken, JSONUtil.toJsonStr(skuLockList));
return true;
return Result.success();
}