{"record":{"id":"47dfcc360ef6dcdc","repo":"paascloud/paascloud-master","slug":"mdc10021016","errorCode":"MDC10021016","errorMessage":"MDC10021016","messagePattern":"MDC10021016","errorType":"error_code","errorClass":"OmcBizException","httpStatus":null,"severity":"error","filePath":"paascloud-provider/paascloud-provider-omc/src/main/java/com/paascloud/provider/service/impl/OmcCartServiceImpl.java","lineNumber":325,"sourceCode":"\tpublic List<OmcOrderDetail> getCartOrderItem(Long userId, List<OmcCart> cartList) {\n\t\tList<OmcOrderDetail> orderItemList = Lists.newArrayList();\n\t\tif (CollectionUtils.isEmpty(cartList)) {\n\t\t\tthrow new OmcBizException(ErrorCodeEnum.OMC10031001, userId);\n\t\t}\n\n\t\t//校验购物车的数据,包括产品的状态和数量\n\t\tfor (OmcCart cartItem : cartList) {\n\t\t\tOmcOrderDetail orderDetail = new OmcOrderDetail();\n\t\t\tProductDto product = mdcProductService.selectById(cartItem.getProductId());\n\t\t\tif (MdcApiConstant.ProductStatusEnum.ON_SALE.getCode() != product.getStatus()) {\n\t\t\t\tlogger.error(\"商品不是在线售卖状态, productId={}\", product.getId());\n\t\t\t\tthrow new OmcBizException(ErrorCodeEnum.MDC10021015, product.getId());\n\t\t\t}\n\n\t\t\t//校验库存\n\t\t\tif (cartItem.getQuantity() > product.getStock()) {\n\t\t\t\tlogger.error(\"商品库存不足, productId={}\", product.getId());\n\t\t\t\tthrow new OmcBizException(ErrorCodeEnum.MDC10021016, product.getId());\n\t\t\t}\n\n\t\t\torderDetail.setUserId(userId);\n\t\t\torderDetail.setProductId(product.getId());\n\t\t\torderDetail.setProductName(product.getName());\n\t\t\torderDetail.setProductImage(product.getMainImage());\n\t\t\torderDetail.setCurrentUnitPrice(product.getPrice());\n\t\t\torderDetail.setQuantity(cartItem.getQuantity());\n\t\t\torderDetail.setTotalPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(), cartItem.getQuantity()));\n\t\t\torderItemList.add(orderDetail);\n\t\t}\n\t\treturn orderItemList;\n\t}\n\n\tprivate OrderItemVo assembleOrderItemVo(OmcOrderDetail orderItem) {\n\t\tOrderItemVo orderItemVo = new OrderItemVo();\n\t\torderItemVo.setOrderNo(orderItem.getOrderNo());\n\t\torderItemVo.setProductId(orderItem.getProductId());","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/paascloud/paascloud-master/blob/781281a9503332ed3cef44ea618349d14230a127/paascloud-provider/paascloud-provider-omc/src/main/java/com/paascloud/provider/service/impl/OmcCartServiceImpl.java#L307-L343","documentation":"MDC10021016 means '商品库存不足' (insufficient stock for product, productId=%s). getCartOrderItem compares the requested cart quantity against the live product stock and throws when cartItem.getQuantity() > product.getStock(), preventing overselling.","triggerScenarios":"Requesting more units of a product than currently in stock — concurrent purchases drained stock after the item was added to the cart, or stock was reduced by admin/inventory adjustments.","commonSituations":"Flash-sale or high-traffic scenarios where several buyers race for limited stock; stock decremented in MDC without updating carts; user edited quantity in a stale cart page; inventory sync issues between services.","solutions":["Reduce the cart quantity to at most product.getStock() and retry checkout.","Restock the product in MDC if the requested quantity is legitimate.","Catch OmcBizException code 10021016 and tell the user how many units are actually available.","Add optimistic stock reservation/decrement at add-to-cart or checkout time to fail earlier and avoid races."],"exampleFix":"// before\nif (cartItem.getQuantity() > product.getStock()) {\n    throw new OmcBizException(ErrorCodeEnum.MDC10021016, product.getId());\n}\n// after (caller-side clamp)\nint sellable = Math.min(cartItem.getQuantity(), product.getStock());\nif (sellable <= 0) {\n    omcCartService.deleteByUserIdProductId(userId, cartItem.getProductId());\n    continue;\n}\ncartItem.setQuantity(sellable);","handlingStrategy":"validation","validationCode":"// Java, pre-check stock before checkout\nfor (OmcCart item : cartList) {\n    ProductDto p = mdcProductService.selectById(item.getProductId());\n    if (p == null || item.getQuantity() > p.getStock()) {\n        throw new OmcBizException(ErrorCodeEnum.MDC10021016, item.getProductId());\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    orderService.createOrderDoc(loginAuthDto, shippingId);\n} catch (OmcBizException e) {\n    if (e.getCode() == ErrorCodeEnum.MDC10021016.getCode()) {\n        return ResultHelper.fail(e.getCode(), \"Insufficient stock, please adjust quantities\");\n    }\n    throw e;\n}","preventionTips":["Clamp cart quantities to available stock when the user updates the cart.","Re-check stock immediately before payment/order creation to close race windows.","Use atomic stock decrement (conditional UPDATE ... WHERE stock >= ?) at order time.","Surface per-item availability on the cart page so users self-correct."],"tags":["stock","inventory","cart","oversell-prevention"],"backgroundTag":"value-out-of-range","analyzedSha":"781281a9503332ed3cef44ea618349d14230a127","analyzedAt":"2026-09-10T10:59:02.070Z","contentChangedAt":"2026-09-10T10:59:02.070Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}