feat: 实现订单创建功能,包括更新商品库存和销量

This commit is contained in:
mkm 2024-07-30 14:21:57 +08:00
parent d9bb85651e
commit 58cf679569

View File

@ -330,19 +330,30 @@ class OrderLogic extends BaseLogic
try {
$order = StoreOrder::create($_order);
$goods_list = $orderInfo['cart_list'];
$updateData = [];
$updateDataTwo = [];
foreach ($goods_list as $k => $v) {
$goods_list[$k]['oid'] = $order->id;
$goods_list[$k]['uid'] = $uid;
$goods_list[$k]['cart_id'] = implode(',', $cartId);
$goods_list[$k]['delivery_id'] = $params['store_id']; //商家id
// $StoreBranchProduct = StoreBranchProduct::where('id', $v['branch_product_id'])->withTrashed()->find();
// if ($StoreBranchProduct['stock'] - $v['cart_num'] <= 0) {
// Db::name('store_product_cate')->where(['cate_id' => $StoreBranchProduct['cate_id'], 'store_id' => $params['store_id']])->update(['count' => 0]);
// }
$updateData[] = [
'id' => $v['branch_product_id'],
'sales' => ['inc', $v['cart_num']],
'stock' => ['dec', $v['cart_num']],
];
$updateDataTwo[] = [
'id' => $v['product_id'],
'sales' => ['inc', $v['cart_num']]
];
}
(new StoreOrderCartInfo())->saveAll($goods_list);
$where = ['is_pay' => 0];
Cart::whereIn('id', $cartId)->where($where)->update(['is_pay' => 1]);
(new StoreBranchProduct())->saveAll($updateData);
(new StoreProduct())->saveAll($updateDataTwo);
Db::commit();
return $order;
} catch (\Exception $e) {