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