123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\model\Order as OrderModel;
- use app\common\model\User as UserModel;
- use think\Hook;
- use think\Db;
- /**
- * 测试控制器
- * 仅用于开发和测试环境
- */
- class Test extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- /**
- * 测试支付后分销事件
- */
- public function testOrderPaidEvent()
- {
- // 检查是否为开发环境
- // if (config('app_debug') !== true) {
- // $this->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
- ]);
- }
- }
|