From 493e8712be5ee3cbce3b8e21f0a68274c0d9dec5 Mon Sep 17 00:00:00 2001 From: wangxiaowei Date: Fri, 1 Nov 2024 14:07:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B8=85=E9=99=A4=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E8=B4=AD=E7=89=A9=E8=BD=A6=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/logic/LocalStoreGoodsCartLogic.php | 66 ++++++++++++++++------ 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/app/api/logic/LocalStoreGoodsCartLogic.php b/app/api/logic/LocalStoreGoodsCartLogic.php index c4f04200..f0d1f90d 100644 --- a/app/api/logic/LocalStoreGoodsCartLogic.php +++ b/app/api/logic/LocalStoreGoodsCartLogic.php @@ -147,24 +147,54 @@ class LocalStoreGoodsCartLogic extends Logic public static function clearCart($post, $user_id) { - if ($post['cart_id'] > 0) { - // 清空购物车单独的商品 - LocalStoreGoodsCart::where([ - 'id' => $post['cart_id'], - 'user_id' => $user_id, - 'lstore_id' => $post['lstore_id'], - ])->update([ - 'is_delete' => 1, - 'delete_time' => time() - ]); - } else { - LocalStoreGoodsCart::where([ - 'user_id' => $user_id, - 'lstore_id' => $post['lstore_id'], - ])->update([ - 'is_delete' => 1, - 'delete_time' => time() - ]); + Db::startTrans(); + try { + if (isset($post['cart_id']) > 0) { + // 清空购物车单独的商品 + LocalStoreGoodsCart::where([ + 'id' => $post['cart_id'], + 'user_id' => $user_id, + 'lstore_id' => $post['lstore_id'], + ])->update([ + 'is_delete' => 1, + 'delete_time' => time() + ]); + } else { + // 清空购物车 + LocalStoreGoodsCart::where([ + 'user_id' => $user_id, + 'lstore_id' => $post['lstore_id'], + ])->update([ + 'is_delete' => 1, + 'delete_time' => time() + ]); + } + + $result = LocalStoreGoodsCart::alias('c') + ->join('local_store_goods g', 'c.lsg_id = g.id') + ->where(['c.user_id' => $user_id, 'c.lstore_id' => $post['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0]) + ->field('c.id, c.item_id, c.goods_num, g.id as lsg_id, g.title, g.price') + ->append(['pic', 'item']) + ->select() + ->toArray(); + + $price = $goods_num = 0; + foreach ($result as &$v) { + $price += ($v['goods_num'] * $v['price']); + $goods_num += $v['goods_num']; + } + $price = number_format($price, 2, '.', ''); + Db::commit(); + + return [ + 'total_amount' => $price, + 'goods_num' => $goods_num + ]; + } catch (Exception $e) { + Db::rollback(); + dump($e->getMessage());die; + self::$error = $e->getMessage(); + return false; } } } \ No newline at end of file