$good) { $item_value = $good['item_value']; //扣除库存,扣除规格库存,增加商品销量 Db::name('goods') ->where('id', $good['goods_id']) ->update([ 'order_num' => Db::raw("order_num+" . $good['goods_num']), 'stock' => Db::raw('stock-' . $good['goods_num']) ]); //扣除规格表库存 Db::name('goods_item') ->where(array('goods_id'=>$good['goods_id'],'spec_value'=>$good['item_value'])) ->setDec('stock', $good['goods_num']); $goods_ids[] = $good['goods_id']; } //下架商品总库存为0的商品 if (!empty($goods_ids)){ self::outGoods($goods_ids); } } /** * Notes: 下单后下架商品总库存为0的商品 * @param $goods_id */ public static function outGoods($goods_ids) { try{ $goods = Db::name('goods') ->field('id, stock') ->where('id', 'in', $goods_ids) ->select(); if (empty($goods)){ return true; } $need_handle_ids = []; foreach ($goods as $good) { if ($good['stock'] <= 0) { $need_handle_ids[] = $good['id']; } } if (empty($need_handle_ids)){ return true; } //下架订单商品中 商品总库存已为0的商品 Db::name('goods')->where('id', 'in', $need_handle_ids)->update(['status' => 0]); } catch (\Exception $e) {} } }