From c0ee92c93257e2248ab114acc597eeb2159b37c1 Mon Sep 17 00:00:00 2001 From: liu <1873441552@qq.com> Date: Tue, 11 Jun 2024 15:52:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=93=E5=AD=98=E5=87=8F=E5=B0=91=E5=92=8C?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=89=AB=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/logic/order/OrderLogic.php | 23 ++++++++++++++++++----- app/functions.php | 10 ++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/api/logic/order/OrderLogic.php b/app/api/logic/order/OrderLogic.php index 71d1f3081..bddc0dbbd 100644 --- a/app/api/logic/order/OrderLogic.php +++ b/app/api/logic/order/OrderLogic.php @@ -26,6 +26,7 @@ use app\common\model\user\UserShip; use support\exception\BusinessException; use support\Log; use taoser\exception\ValidateException; +use think\db\Raw; use think\Exception; use think\facade\Db; use Workerman\Events\Select; @@ -68,7 +69,7 @@ class OrderLogic extends BaseLogic /** 计算价格 */ $check = DictType::where('type', 'activities')->find(); foreach ($cart_select as $k => $v) { - $find = StoreBranchProduct::where(['product_id' => $v['product_id'], 'store_id' => $params['store_id']])->field('store_name,image,unit,price,cost,product_id')->find(); + $find = StoreBranchProduct::where(['product_id' => $v['product_id'], 'store_id' => $params['store_id']])->field('id branch_product_id,store_name,image,unit,price,cost,product_id')->find(); if (!$find) { continue; } @@ -104,6 +105,7 @@ class OrderLogic extends BaseLogic // } $cartInfo['vip_price'] = 0; //$cart_select[$k]['total'] - $vipPrice ?? 0; $cart_select[$k]['cart_info'] = json_encode($cartInfo); + $cart_select[$k]['branch_product_id'] = $find['branch_product_id']; //理论上每笔都是拆分了 $cart_select[$k]['name'] = $find['store_name']; $cart_select[$k]['imgs'] = $find['image']; @@ -158,9 +160,9 @@ class OrderLogic extends BaseLogic //成本价 收益 $order = [ 'create_time' => time(), - 'order_id' => getNewOrderId('PF'), + 'order_id' => $params['order_id']?:getNewOrderId('PF'), 'total_price' => self::$total_price, //总价 - 'cost' => self::$cost, //成本价 + 'cost' => self::$cost, //成本价1- 'profit' => self::$profit, //利润 'pay_price' => $pay_price, //后期可能有降价抵扣 'vip_price' => $vipPrice, @@ -193,8 +195,11 @@ class OrderLogic extends BaseLogic */ static public function createOrder($cartId, $addressId, $user = null, $params = []) { - $verify_code = verificationCode(); - $params['verify_code'] = $verify_code; + $order_id = getNewOrderId('PF'); + $code = rand(1,10).'-'.substr($order_id, -5); + $verify_code = createCode($code); + $params['order_id'] = $order_id; + $params['verify_code'] =$verify_code; $orderInfo = self::cartIdByOrderInfo($cartId, $addressId, $user, $params); if (!$orderInfo) { return false; @@ -227,13 +232,21 @@ class OrderLogic extends BaseLogic try { $order = StoreOrder::create($_order); $goods_list = $orderInfo['cart_list']; + $updateData = []; foreach ($goods_list as $k => $v) { $goods_list[$k]['oid'] = $order->id; $goods_list[$k]['uid'] = request()->userId; $goods_list[$k]['cart_id'] = implode(',', $cartId); $goods_list[$k]['delivery_id'] = $params['store_id']; //商家id + $stock = StoreBranchProduct::where('id',$v['branch_product_id'])->value('stock'); + $updateData[] = [ + 'id' => $v['branch_product_id'], + 'stock' => $stock-$v['cart_num'], + 'sales' => ['inc', $v['cart_num']] + ]; } (new StoreOrderCartInfo())->saveAll($goods_list); + (new StoreBranchProduct())->saveAll($updateData); $where = ['is_pay' => 0]; Cart::whereIn('id', $cartId)->where($where)->update(['is_pay' => 1]); Db::commit(); diff --git a/app/functions.php b/app/functions.php index 8ad59b471..2328a71b2 100644 --- a/app/functions.php +++ b/app/functions.php @@ -375,6 +375,16 @@ if (!function_exists('verificationCode')) { } } +if (!function_exists('createCode')) { + function createCode($code) { + $check = \app\common\model\store_order\StoreOrder::where('verify_code',$code)->count(); + if($check){ + $orderPickupCode = rand(1,10).'-'.str_pad(rand(0, 99999), 5, '0', STR_PAD_LEFT); + return createCode($orderPickupCode); + } + return $code; + } +} if (!function_exists('haversineDistance')) { function haversineDistance($latitude1, $longitude1, $latitude2, $longitude2) {