作者 何书鹏
1 个管道 的构建 通过 耗费 1 秒

异步回调更新商品销量

@@ -5,6 +5,7 @@ namespace app\api\controller; @@ -5,6 +5,7 @@ namespace app\api\controller;
5 use app\common\controller\Api; 5 use app\common\controller\Api;
6 use app\common\model\Goods; 6 use app\common\model\Goods;
7 use app\common\model\GoodsStyle; 7 use app\common\model\GoodsStyle;
  8 +use app\common\model\GoodsSpec;
8 use app\common\controller\Wechat; 9 use app\common\controller\Wechat;
9 use think\Db; 10 use think\Db;
10 11
@@ -371,17 +372,29 @@ class Cart extends Api @@ -371,17 +372,29 @@ class Cart extends Api
371 $order->pay_time = time(); // 更新支付时间为当前时间 372 $order->pay_time = time(); // 更新支付时间为当前时间
372 $order->pay_status = '1'; 373 $order->pay_status = '1';
373 $order->save(); 374 $order->save();
374 - // 非定制商品减库存  
375 - $deductStockData = []; 375 + // 整理批量更新商品销量
  376 + $goodsSave = [];
  377 + // 批量更新商品规格:sku销量、库存
  378 + $goodsSpecSave = [];
376 foreach($order['goods'] as $v){ 379 foreach($order['goods'] as $v){
377 - if($v['goods']['ismake'] == '0'){  
378 - $deductStockData[] = [ 380 + $goodsSave[] = [
  381 + 'id' => $v['goods_id'],
  382 + 'sale_num' => ['inc', $v['goods_num']]
  383 + ];
  384 + $specData = [
379 'id' => $v['spec']['id'], 385 'id' => $v['spec']['id'],
380 - 'stock_num' => ['dec', $v['goods_num']] 386 + 'sale_num' => ['inc', $v['goods_num']]
381 ]; 387 ];
  388 + // 非定制商品减库存
  389 + if($v['goods']['ismake'] == '0'){
  390 + $specData['stock_num'] = ['dec', $v['goods_num']];
382 } 391 }
  392 + $goodsSpecSave[] = $specData;
383 } 393 }
384 - !empty($deductStockData) && (new \app\common\model\GoodsSpec)->isUpdate()->saveAll($deductStockData); 394 + // 更新商品总销量
  395 + (new Goods)->allowField(true)->isUpdate()->saveAll($goodsSave);
  396 + // 更新商品规格库存
  397 + (new GoodsSpec)->allowField(true)->isUpdate()->saveAll($goodsSpecSave);
385 return true; 398 return true;
386 } 399 }
387 } 400 }