where("id", $order["id"])->find(); $items = $order ? $order["item"] : []; // 1. 订单提交后只创建唯一打印任务;实际云打印由独立队列执行。 $this->queuePrintOrder($order); // 2. 门店管理员通知 $storeIds = []; foreach ($items as $item) { if (in_array($item["dispatch_type"], ["store", "selfetch"]) && $item["store_id"]) { $storeIds[] = $item["store_id"]; } } $data = []; if ($storeIds) { $data = []; // 存在门店,查询门店管理员 $stores = Store::with(["userStore.user"])->where("id", "in", $storeIds)->select(); foreach ($stores as $key => $store) { $userStoreList = $store["user_store"] ? : []; unset($store["user_store"]); // 当前门店所有用户管理员 $userList = []; foreach ($userStoreList as $user) { if ($user["user"]) { $userList[] = $user["user"]; } } // 有用户才能发送消息 if ($userList) { $data[] = [ "store" => $store, "userList" => $userList ]; } } } // 存在要通知的门店管理员 if ($data) { // 按门店为单位发送通知 foreach ($data as $key => $sendData) { \addons\shopro\library\notify\Notify::send( $sendData["userList"], new \addons\shopro\notifications\store\Order([ "store" => $sendData["store"], "order" => $order, "event" => "store_order_new" ]) ); } } } protected function queuePrintOrder($order) { if (!$order) { return; } try { $task = \think\Db::name('shopro_order_print_task')->where('order_id', $order['id'])->find(); if (!$task) { try { \think\Db::name('shopro_order_print_task')->insert([ 'order_id' => $order['id'], 'order_sn' => $order['order_sn'], 'status' => 0, 'attempts' => 0, 'created_at' => time(), 'updated_at' => time(), ]); $task = true; } catch (\Exception $e) { // 唯一索引冲突表示另一个事件已创建任务,禁止重复入队。 $task = \think\Db::name('shopro_order_print_task')->where('order_id', $order['id'])->find(); } } if ($task && (!is_array($task) || (int)$task['status'] !== 1)) { \think\Queue::push('\\addons\\shopro\\job\\OrderPrint@handle', ['order_id' => $order['id']], 'shopro-high'); } } catch (\Exception $e) { \think\Log::record('创建小票打印任务失败,订单ID ' . $order['id'] . ': ' . $e->getMessage(), 'error'); } } }