@@ -189,35 +189,67 @@ protected function actionDeduct($items = '')
189
189
$ product_name = Yii::$ service ->store ->getStoreAttrVal ($ item ['product_name ' ], 'name ' );
190
190
$ custom_option_sku = $ item ['custom_option_sku ' ];
191
191
if ($ product_id && $ sale_qty ) {
192
+ $ sale_qty = 0 - $ sale_qty ;
192
193
if (!$ custom_option_sku ){
193
194
// 应对高并发库存超卖的控制,更新后在查询产品的库存,如果库存小于则回滚。
194
- $ sql = 'update ' .$ this ->_flatQtyModel ->tableName ().' set qty = qty - :sale_qty where product_id = :product_id ' ;
195
- $ data = [
196
- 'sale_qty ' => $ sale_qty ,
197
- 'product_id ' => $ product_id ,
198
- ];
199
- $ result = $ this ->_flatQtyModel ->getDb ()->createCommand ($ sql ,$ data )->execute ();
200
- $ productFlatQty = $ this ->_flatQtyModel ->find ()->where ([
201
- 'product_id ' => $ product_id
202
- ])->one ();
203
- if ($ productFlatQty ['qty ' ] < 0 ){
195
+
196
+ /** 废弃,采用下面更好的方式来处理库存
197
+ * $sql = 'update '.$this->_flatQtyModel->tableName().' set qty = qty - :sale_qty where product_id = :product_id';
198
+ * $data = [
199
+ * 'sale_qty' => $sale_qty,
200
+ * 'product_id'=> $product_id,
201
+ * ];
202
+ * $result = $this->_flatQtyModel->getDb()->createCommand($sql,$data)->execute();
203
+ * $productFlatQty = $this->_flatQtyModel->find()->where([
204
+ * 'product_id' => $product_id
205
+ * ])->one();
206
+ * if($productFlatQty['qty'] < 0){
207
+ * Yii::$service->helper->errors->add('product: [ {product_name} ] is stock out',['product_name' => $product_name]);
208
+ * return false;
209
+ * }
210
+ *
211
+ **/
212
+ $ updateColumns = $ this ->_flatQtyModel ::updateAllCounters (
213
+ ['qty ' => $ sale_qty ],
214
+ ['and ' , ['product_id ' => $ product_id ], ['>= ' , 'qty ' , $ sale_qty ]]
215
+ );
216
+ if (empty ($ updateColumns )) {// 上面更新sql返回的更新行数如果为0,则说明更新失败,产品不存在,或者产品库存不够
204
217
Yii::$ service ->helper ->errors ->add ('product: [ {product_name} ] is stock out ' ,['product_name ' => $ product_name ]);
205
218
return false ;
206
219
}
207
220
}else {
208
221
// 对于custom option(淘宝模式)的库存扣除
209
- $ sql = 'update ' .$ this ->_COQtyModel ->tableName ().' set qty = qty - :sale_qty where product_id = :product_id and custom_option_sku = :custom_option_sku ' ;
210
- $ data = [
211
- 'sale_qty ' => $ sale_qty ,
212
- 'product_id ' => $ product_id ,
213
- 'custom_option_sku ' => $ custom_option_sku
214
- ];
215
- $ result = $ this ->_COQtyModel ->getDb ()->createCommand ($ sql ,$ data )->execute ();
216
- $ productCustomOptionQty = $ this ->_COQtyModel ->find ()->where ([
217
- 'product_id ' => $ product_id ,
218
- 'custom_option_sku ' => $ custom_option_sku ,
219
- ])->one ();
220
- if ($ productCustomOptionQty ['qty ' ] < 0 ){
222
+
223
+ /** 废弃,采用下面更好的方式来处理库存
224
+ * $sql = 'update '.$this->_COQtyModel->tableName().' set qty = qty - :sale_qty where product_id = :product_id and custom_option_sku = :custom_option_sku';
225
+ * $data = [
226
+ * 'sale_qty' => $sale_qty,
227
+ * 'product_id'=> $product_id,
228
+ * 'custom_option_sku' => $custom_option_sku
229
+ * ];
230
+ * $result = $this->_COQtyModel->getDb()->createCommand($sql,$data)->execute();
231
+ * $productCustomOptionQty = $this->_COQtyModel->find()->where([
232
+ * 'product_id' => $product_id,
233
+ * 'custom_option_sku' => $custom_option_sku,
234
+ * ])->one();
235
+ * if($productCustomOptionQty['qty'] < 0){
236
+ * Yii::$service->helper->errors->add('product: [ {product_name} ] is stock out' ,['product_name' => $product_name]);
237
+ * return false;
238
+ * }
239
+ **/
240
+ $ updateColumns = $ this ->_COQtyModel ::updateAllCounters (
241
+ ['qty ' => $ sale_qty ]
242
+ ,
243
+ [
244
+ 'and ' ,
245
+ [
246
+ 'custom_option_sku ' => $ custom_option_sku ,
247
+ 'product_id ' => $ product_id
248
+ ],
249
+ ['>= ' ,'qty ' ,$ sale_qty ]
250
+ ]
251
+ );
252
+ if (empty ($ updateColumns )) {// 上面更新sql返回的更新行数如果为0,则说明更新失败,产品不存在,或者产品库存不够
221
253
Yii::$ service ->helper ->errors ->add ('product: [ {product_name} ] is stock out ' ,['product_name ' => $ product_name ]);
222
254
return false ;
223
255
}
@@ -381,7 +413,7 @@ protected function actionCheckItemsQty()
381
413
* ],
382
414
* ]
383
415
* @return bool
384
- * 返还产品库存。
416
+ * 返还产品库存。如果在返还过程中产品不存在,也不会返回false
385
417
*/
386
418
protected function actionReturnQty ($ product_items )
387
419
{
@@ -396,23 +428,29 @@ protected function actionReturnQty($product_items)
396
428
$ custom_option_sku = $ item ['custom_option_sku ' ];
397
429
if ($ product_id && $ sale_qty ) {
398
430
if (!$ custom_option_sku ){
399
- $ sql = 'update ' .$ this ->_flatQtyModel ->tableName ().' set qty = qty + :sale_qty where product_id = :product_id ' ;
400
- $ data = [
401
- 'sale_qty ' => $ sale_qty ,
402
- 'product_id ' => $ product_id ,
403
- ];
404
- $ result = $ this ->_flatQtyModel ->getDb ()->createCommand ($ sql ,$ data )->execute ();
405
-
431
+ /** 废弃,采用下面更好的方式更新产品库存
432
+ * $sql = 'update '.$this->_flatQtyModel->tableName().' set qty = qty + :sale_qty where product_id = :product_id';
433
+ * $data = [
434
+ * 'sale_qty' => $sale_qty,
435
+ * 'product_id'=> $product_id,
436
+ * ];
437
+ * $result = $this->_flatQtyModel->getDb()->createCommand($sql,$data)->execute();
438
+ **/
439
+ $ updateColumns = $ this ->_flatQtyModel ::updateAllCounters (['qty ' => $ sale_qty ], ['product_id ' => $ product_id ]);
440
+
406
441
}else {
407
442
// 对于custom option(淘宝模式)的库存扣除
408
- $ sql = 'update ' .$ this ->_COQtyModel ->tableName ().' set qty = qty + :sale_qty where product_id = :product_id and custom_option_sku = :custom_option_sku ' ;
409
- $ data = [
410
- 'sale_qty ' => $ sale_qty ,
411
- 'product_id ' => $ product_id ,
412
- 'custom_option_sku ' => $ custom_option_sku
413
- ];
414
- $ result = $ this ->_COQtyModel ->getDb ()->createCommand ($ sql ,$ data )->execute ();
415
443
444
+ /** 废弃,采用下面更好的方式更新产品库存
445
+ * $sql = 'update '.$this->_COQtyModel->tableName().' set qty = qty + :sale_qty where product_id = :product_id and custom_option_sku = :custom_option_sku';
446
+ * $data = [
447
+ * 'sale_qty' => $sale_qty,
448
+ * 'product_id'=> $product_id,
449
+ * 'custom_option_sku' => $custom_option_sku
450
+ * ];
451
+ * $result = $this->_COQtyModel->getDb()->createCommand($sql,$data)->execute();
452
+ **/
453
+ $ updateColumns = $ this ->_COQtyModel ::updateAllCounters (['qty ' => $ sale_qty ], ['product_id ' => $ product_id ,'custom_option_sku ' => $ custom_option_sku ]);
416
454
}
417
455
}
418
456
}
0 commit comments