error('此接口仅在开发环境下可用'); // } $orderId = $this->request->param('order_id'); if (empty($orderId)) { $this->error('请提供订单ID'); } // try { // 查询订单信息 $order = OrderModel::with('items') ->where('id', $orderId) ->find(); if (!$order) { $this->error('订单不存在'); } // 查询用户信息 $user = UserModel::where('id', $order->user_id)->find(); if (!$user) { $this->error('用户不存在'); } // 手动触发支付后事件 $eventData = [ 'order' => $order, 'user' => $user ]; Hook::listen('order_paid_after', $eventData); $this->success('支付后事件测试完成',); // } catch (\Exception $e) { // $this->error('测试失败: ' . $e->getMessage()); // } } /** * 获取分销统计数据 */ private function getCommissionStats($order, $user) { // 查询分销订单 $commissionOrders = Db::name('commission_order') ->where('order_id', $order->id) ->select(); // 查询佣金记录 $rewards = Db::name('commission_reward') ->alias('r') ->leftJoin('commission_order o', 'r.commission_order_id = o.id') ->where('o.order_id', $order->id) ->field('r.*') ->select(); // 查询用户分销商信息 $agent = Db::name('shop_commission_agent') ->where('user_id', $user->id) ->find(); // 查询用户的上级关系 $parentInfo = null; if ($user->parent_user_id > 0) { $parentUser = UserModel::where('id', $user->parent_user_id)->find(); if ($parentUser) { $parentAgent = Db::name('shop_commission_agent') ->where('user_id', $parentUser->id) ->find(); $parentInfo = [ 'parent_user_id' => $parentUser->id, 'parent_nickname' => $parentUser->nickname, 'parent_is_agent' => !empty($parentAgent), 'bind_time' => $user->bind_time ? date('Y-m-d H:i:s', $user->bind_time) : null ]; } } return [ 'user_commission' => $user->commission, 'agent_info' => $agent ? [ 'agent_id' => $agent['id'], 'agent_type' => $agent['agent_type'], 'total_income' => $agent['total_income'], 'withdrawn_amount' => $agent['withdrawn_amount'], 'child_user_count_all' => $agent['child_user_count_all'], 'child_order_money_all' => $agent['child_order_money_all'], ] : null, 'parent_info' => $parentInfo, 'commission_orders_count' => count($commissionOrders), 'commission_orders' => $commissionOrders, 'rewards_count' => count($rewards), 'rewards' => $rewards, 'total_reward_amount' => array_sum(array_column($rewards, 'commission')) ]; } /** * 格式化订单商品 */ private function formatOrderItems($items) { $result = []; foreach ($items as $item) { $result[] = [ 'item_id' => $item->id, 'goods_name' => $item->goods_name, 'price' => $item->price, 'quantity' => $item->quantity, 'total_price' => $item->total_price, 'is_commission' => isset($item->ext['is_commission']) ? $item->ext['is_commission'] : true, 'commission_rate' => $item->ext['commission_rate'] ?? 0, ]; } return $result; } /** * 比较前后数据变化 */ private function compareStats($before, $after) { $changes = []; // 用户佣金变化 if ($before['user_commission'] != $after['user_commission']) { $changes['user_commission'] = [ 'before' => $before['user_commission'], 'after' => $after['user_commission'], 'change' => $after['user_commission'] - $before['user_commission'] ]; } // 代理商信息变化 if ($before['agent_info'] && $after['agent_info']) { $beforeAgent = $before['agent_info']; $afterAgent = $after['agent_info']; if ($beforeAgent['total_income'] != $afterAgent['total_income']) { $changes['agent_total_income'] = [ 'before' => $beforeAgent['total_income'], 'after' => $afterAgent['total_income'], 'change' => $afterAgent['total_income'] - $beforeAgent['total_income'] ]; } if ($beforeAgent['child_order_money_all'] != $afterAgent['child_order_money_all']) { $changes['agent_child_order_money'] = [ 'before' => $beforeAgent['child_order_money_all'], 'after' => $afterAgent['child_order_money_all'], 'change' => $afterAgent['child_order_money_all'] - $beforeAgent['child_order_money_all'] ]; } } // 分销订单数量变化 if ($before['commission_orders_count'] != $after['commission_orders_count']) { $changes['commission_orders_count'] = [ 'before' => $before['commission_orders_count'], 'after' => $after['commission_orders_count'], 'change' => $after['commission_orders_count'] - $before['commission_orders_count'] ]; } // 佣金记录数量变化 if ($before['rewards_count'] != $after['rewards_count']) { $changes['rewards_count'] = [ 'before' => $before['rewards_count'], 'after' => $after['rewards_count'], 'change' => $after['rewards_count'] - $before['rewards_count'] ]; } // 总佣金金额变化 if ($before['total_reward_amount'] != $after['total_reward_amount']) { $changes['total_reward_amount'] = [ 'before' => $before['total_reward_amount'], 'after' => $after['total_reward_amount'], 'change' => $after['total_reward_amount'] - $before['total_reward_amount'] ]; } return $changes; } /** * 获取最近的分销日志 */ private function getRecentCommissionLogs($userId) { return Db::name('commission_log') ->where('user_id', $userId) ->order('createtime desc') ->limit(5) ->select(); } /** * 查询订单基本信息(不触发事件) */ public function getOrderInfo() { $orderId = $this->request->param('order_id'); if (empty($orderId)) { $this->error('请提供订单ID'); } // 查询订单信息 $order = OrderModel::with(['items', 'user']) ->where('id', $orderId) ->find(); if (!$order) { $this->error('订单不存在'); } // 查询用户信息 $user = UserModel::where('id', $order->user_id)->find(); // 查询当前分销数据 $currentStats = $this->getCommissionStats($order, $user); $result = [ 'order_info' => [ 'id' => $order->id, 'order_sn' => $order->order_sn, 'user_id' => $order->user_id, 'status' => $order->status, 'pay_status' => $order->pay_status, 'total_price' => $order->total_price, 'pay_time' => $order->pay_time ? date('Y-m-d H:i:s', $order->pay_time) : null, 'createtime' => date('Y-m-d H:i:s', $order->createtime), ], 'user_info' => [ 'id' => $user->id, 'username' => $user->username, 'nickname' => $user->nickname, 'mobile' => $user->mobile, 'parent_user_id' => $user->parent_user_id, 'bind_time' => $user->bind_time ? date('Y-m-d H:i:s', $user->bind_time) : null, 'commission' => $user->commission, ], 'order_items' => $this->formatOrderItems($order->items), 'current_commission_stats' => $currentStats ]; $this->success('订单信息查询成功', $result); } /** * 批量测试多个订单 */ public function batchTestOrders() { // 检查是否为开发环境 if (config('app_debug') !== true) { $this->error('此接口仅在开发环境下可用'); } $orderIds = $this->request->param('order_ids'); if (empty($orderIds) || !is_array($orderIds)) { $this->error('请提供订单ID数组'); } $results = []; foreach ($orderIds as $orderId) { try { // 查询订单信息 $order = OrderModel::with(['items', 'user'])->where('id', $orderId)->find(); if (!$order) { $results[$orderId] = ['error' => '订单不存在']; continue; } $user = UserModel::where('id', $order->user_id)->find(); if (!$user) { $results[$orderId] = ['error' => '用户不存在']; continue; } // 记录触发前状态 $beforeStats = $this->getCommissionStats($order, $user); // 触发事件 Hook::listen('order_paid_after', [ 'order' => $order, 'user' => $user ]); // 记录触发后状态 $afterStats = $this->getCommissionStats($order, $user); $results[$orderId] = [ 'success' => true, 'order_sn' => $order->order_sn, 'user_nickname' => $user->nickname, 'changes' => $this->compareStats($beforeStats, $afterStats) ]; } catch (\Exception $e) { $results[$orderId] = ['error' => $e->getMessage()]; } } $this->success('批量测试完成', [ 'total_orders' => count($orderIds), 'results' => $results ]); } }