From 82c1eb1ce61ef0d5166c1577e8cccaa6f96eb291 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Mon, 3 Feb 2025 14:22:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(admin):=20=E6=B7=BB=E5=8A=A0=20demo4=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=B9=B6=E5=AE=9E=E7=8E=B0=E4=BB=B7=E6=A0=BC?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 IndexController 中添加 demo4 方法,用于处理价格同步请求 - 在 DemoLogic 中实现 syncPrice 方法,完成价格同步逻辑 - 支持根据是否为 VIP 用户更新商品价格 - 更新订单总价和商品小计价格 - 如果存在出库单,同步更新仓库商品价格 --- app/admin/controller/IndexController.php | 8 ++++ app/api/logic/DemoLogic.php | 47 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/app/admin/controller/IndexController.php b/app/admin/controller/IndexController.php index a0182cb1e..57a6d1c76 100644 --- a/app/admin/controller/IndexController.php +++ b/app/admin/controller/IndexController.php @@ -37,4 +37,12 @@ class IndexController extends BaseAdminController $res=DemoLogic::test3($id,$warehouse_id); return $this->success('成功'); } + + public function demo4() + { + $params=$this->request->get(); + $is_vip=$this->request->get('is_vip',0); + $res=DemoLogic::syncPrice($params,$is_vip); + return $this->success('成功'); + } } \ No newline at end of file diff --git a/app/api/logic/DemoLogic.php b/app/api/logic/DemoLogic.php index 20886ce22..30a6f0928 100644 --- a/app/api/logic/DemoLogic.php +++ b/app/api/logic/DemoLogic.php @@ -3,6 +3,8 @@ namespace app\api\logic; use app\common\logic\BaseLogic; +use app\common\model\beforehand_order\BeforehandOrder; +use app\common\model\beforehand_order_cart_info\BeforehandOrderCartInfo; use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_product\StoreProduct; use app\common\model\store_product_group_price\StoreProductGroupPrice; @@ -111,4 +113,49 @@ class DemoLogic extends BaseLogic } } } + + public static function syncPrice($params,$is_vip=0) + { + $outbound_id=BeforehandOrder::where('id', $params['id'])->value('outbound_id'); + + $cartInfo = BeforehandOrderCartInfo::where('bhoid', $params['id'])->select()->toArray(); + $productIds = array_column($cartInfo, 'product_id'); + $products = StoreProduct::whereIn('id', $productIds)->select()->toArray(); + $products = reset_index($products, 'id'); + $update = []; + foreach ($cartInfo as $v) { + $product = $products[$v['product_id']]; + if (empty($product)) { + continue; + } + if ($is_vip == 1) { + $price = $product['vip_price']; + } else { + $price = $product['price']; + } + $update[] = [ + 'id' => $v['id'], + 'price' => $price, + 'total_price' => $price * $v['cart_num'], + 'pay_price' => $price * $v['cart_num'], + ]; + } + (new BeforehandOrderCartInfo())->saveAll($update); + $totalPrice = array_sum(array_column($update, 'total_price')); + BeforehandOrder::where('id', $params['id'])->update(['total_price' => $totalPrice,'pay_price' => $totalPrice]); + if($outbound_id){ + WarehouseProduct::where('oid', $outbound_id)->select()->each(function ($item) use($is_vip) { + if($is_vip==0){ + $find = StoreProduct::where('id', $item['product_id'])->withTrashed()->field('purchase,vip_price,price')->find(); + $item->save(['price' => $find['price'], 'purchase' => $find['purchase'], 'vip_price' => $find['vip_price'],'total_price' => $find['price'] * $item['nums']]); + }else{ + $find = StoreProduct::where('id', $item['product_id'])->withTrashed()->field('purchase,vip_price,price')->find(); + $item->save(['price' => $find['vip_price'], 'purchase' => $find['purchase'], 'vip_price' => $find['vip_price'],'total_price' => $find['vip_price'] * $item['nums']]); + } + }); + $total_price=WarehouseProduct::where('oid', $outbound_id)->sum('total_price'); + BeforehandOrder::where('id', $params['id'])->update(['total_price' => $total_price]); + } + + } }