|
|
@@ -173,14 +173,10 @@ public class ProductService {
|
|
|
}
|
|
|
|
|
|
public boolean decreaseStock(Long productId, Integer quantity) {
|
|
|
- Product product = productMapper.selectById(productId);
|
|
|
- if (product == null || product.getStock() < quantity) {
|
|
|
+ if (quantity == null || quantity <= 0) {
|
|
|
return false;
|
|
|
}
|
|
|
- product.setStock(product.getStock() - quantity);
|
|
|
- product.setSalesCount((product.getSalesCount() != null ? product.getSalesCount() : 0) + quantity);
|
|
|
- product.setUpdatedAt(new Date());
|
|
|
- productMapper.updateById(product);
|
|
|
- return true;
|
|
|
+ int rows = productMapper.decreaseStockAtomic(productId, quantity);
|
|
|
+ return rows > 0;
|
|
|
}
|
|
|
}
|