Test.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Order as OrderModel;
  5. use app\common\model\User as UserModel;
  6. use think\Hook;
  7. use think\Db;
  8. /**
  9. * 测试控制器
  10. * 仅用于开发和测试环境
  11. */
  12. class Test extends Api
  13. {
  14. protected $noNeedLogin = [];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 测试支付后分销事件
  18. */
  19. public function testOrderPaidEvent()
  20. {
  21. // 检查是否为开发环境
  22. if (config('app_debug') !== true) {
  23. $this->error('此接口仅在开发环境下可用');
  24. }
  25. $orderId = $this->request->param('order_id');
  26. if (empty($orderId)) {
  27. $this->error('请提供订单ID');
  28. }
  29. try {
  30. // 查询订单信息
  31. $order = OrderModel::with(['items', 'user'])
  32. ->where('id', $orderId)
  33. ->find();
  34. if (!$order) {
  35. $this->error('订单不存在');
  36. }
  37. // 查询用户信息
  38. $user = UserModel::where('id', $order->user_id)->find();
  39. if (!$user) {
  40. $this->error('用户不存在');
  41. }
  42. // 记录触发前的状态
  43. $beforeStats = $this->getCommissionStats($order, $user);
  44. // 手动触发支付后事件
  45. $eventData = [
  46. 'order' => $order,
  47. 'user' => $user
  48. ];
  49. Hook::listen('order_paid_after', $eventData);
  50. // 记录触发后的状态
  51. $afterStats = $this->getCommissionStats($order, $user);
  52. // 构建测试结果
  53. $result = [
  54. 'test_info' => [
  55. 'order_id' => $order->id,
  56. 'order_sn' => $order->order_sn,
  57. 'user_id' => $user->id,
  58. 'user_nickname' => $user->nickname,
  59. 'event_triggered_time' => date('Y-m-d H:i:s'),
  60. 'order_status' => $order->status,
  61. 'pay_status' => $order->pay_status,
  62. 'total_amount' => $order->total_price,
  63. ],
  64. 'order_items' => $this->formatOrderItems($order->items),
  65. 'commission_before' => $beforeStats,
  66. 'commission_after' => $afterStats,
  67. 'changes' => $this->compareStats($beforeStats, $afterStats),
  68. 'event_logs' => $this->getRecentCommissionLogs($user->id)
  69. ];
  70. $this->success('支付后事件测试完成', $result);
  71. } catch (\Exception $e) {
  72. $this->error('测试失败: ' . $e->getMessage());
  73. }
  74. }
  75. /**
  76. * 获取分销统计数据
  77. */
  78. private function getCommissionStats($order, $user)
  79. {
  80. // 查询分销订单
  81. $commissionOrders = Db::name('commission_order')
  82. ->where('order_id', $order->id)
  83. ->select();
  84. // 查询佣金记录
  85. $rewards = Db::name('commission_reward')
  86. ->alias('r')
  87. ->leftJoin('commission_order o', 'r.commission_order_id = o.id')
  88. ->where('o.order_id', $order->id)
  89. ->field('r.*')
  90. ->select();
  91. // 查询用户分销商信息
  92. $agent = Db::name('shop_commission_agent')
  93. ->where('user_id', $user->id)
  94. ->find();
  95. // 查询用户的上级关系
  96. $parentInfo = null;
  97. if ($user->parent_user_id > 0) {
  98. $parentUser = UserModel::where('id', $user->parent_user_id)->find();
  99. if ($parentUser) {
  100. $parentAgent = Db::name('shop_commission_agent')
  101. ->where('user_id', $parentUser->id)
  102. ->find();
  103. $parentInfo = [
  104. 'parent_user_id' => $parentUser->id,
  105. 'parent_nickname' => $parentUser->nickname,
  106. 'parent_is_agent' => !empty($parentAgent),
  107. 'bind_time' => $user->bind_time ? date('Y-m-d H:i:s', $user->bind_time) : null
  108. ];
  109. }
  110. }
  111. return [
  112. 'user_commission' => $user->commission,
  113. 'agent_info' => $agent ? [
  114. 'agent_id' => $agent['id'],
  115. 'agent_type' => $agent['agent_type'],
  116. 'total_income' => $agent['total_income'],
  117. 'withdrawn_amount' => $agent['withdrawn_amount'],
  118. 'child_user_count_all' => $agent['child_user_count_all'],
  119. 'child_order_money_all' => $agent['child_order_money_all'],
  120. ] : null,
  121. 'parent_info' => $parentInfo,
  122. 'commission_orders_count' => count($commissionOrders),
  123. 'commission_orders' => $commissionOrders,
  124. 'rewards_count' => count($rewards),
  125. 'rewards' => $rewards,
  126. 'total_reward_amount' => array_sum(array_column($rewards, 'commission'))
  127. ];
  128. }
  129. /**
  130. * 格式化订单商品
  131. */
  132. private function formatOrderItems($items)
  133. {
  134. $result = [];
  135. foreach ($items as $item) {
  136. $result[] = [
  137. 'item_id' => $item->id,
  138. 'goods_name' => $item->goods_name,
  139. 'price' => $item->price,
  140. 'quantity' => $item->quantity,
  141. 'total_price' => $item->total_price,
  142. 'is_commission' => isset($item->ext['is_commission']) ? $item->ext['is_commission'] : true,
  143. 'commission_rate' => $item->ext['commission_rate'] ?? 0,
  144. ];
  145. }
  146. return $result;
  147. }
  148. /**
  149. * 比较前后数据变化
  150. */
  151. private function compareStats($before, $after)
  152. {
  153. $changes = [];
  154. // 用户佣金变化
  155. if ($before['user_commission'] != $after['user_commission']) {
  156. $changes['user_commission'] = [
  157. 'before' => $before['user_commission'],
  158. 'after' => $after['user_commission'],
  159. 'change' => $after['user_commission'] - $before['user_commission']
  160. ];
  161. }
  162. // 代理商信息变化
  163. if ($before['agent_info'] && $after['agent_info']) {
  164. $beforeAgent = $before['agent_info'];
  165. $afterAgent = $after['agent_info'];
  166. if ($beforeAgent['total_income'] != $afterAgent['total_income']) {
  167. $changes['agent_total_income'] = [
  168. 'before' => $beforeAgent['total_income'],
  169. 'after' => $afterAgent['total_income'],
  170. 'change' => $afterAgent['total_income'] - $beforeAgent['total_income']
  171. ];
  172. }
  173. if ($beforeAgent['child_order_money_all'] != $afterAgent['child_order_money_all']) {
  174. $changes['agent_child_order_money'] = [
  175. 'before' => $beforeAgent['child_order_money_all'],
  176. 'after' => $afterAgent['child_order_money_all'],
  177. 'change' => $afterAgent['child_order_money_all'] - $beforeAgent['child_order_money_all']
  178. ];
  179. }
  180. }
  181. // 分销订单数量变化
  182. if ($before['commission_orders_count'] != $after['commission_orders_count']) {
  183. $changes['commission_orders_count'] = [
  184. 'before' => $before['commission_orders_count'],
  185. 'after' => $after['commission_orders_count'],
  186. 'change' => $after['commission_orders_count'] - $before['commission_orders_count']
  187. ];
  188. }
  189. // 佣金记录数量变化
  190. if ($before['rewards_count'] != $after['rewards_count']) {
  191. $changes['rewards_count'] = [
  192. 'before' => $before['rewards_count'],
  193. 'after' => $after['rewards_count'],
  194. 'change' => $after['rewards_count'] - $before['rewards_count']
  195. ];
  196. }
  197. // 总佣金金额变化
  198. if ($before['total_reward_amount'] != $after['total_reward_amount']) {
  199. $changes['total_reward_amount'] = [
  200. 'before' => $before['total_reward_amount'],
  201. 'after' => $after['total_reward_amount'],
  202. 'change' => $after['total_reward_amount'] - $before['total_reward_amount']
  203. ];
  204. }
  205. return $changes;
  206. }
  207. /**
  208. * 获取最近的分销日志
  209. */
  210. private function getRecentCommissionLogs($userId)
  211. {
  212. return Db::name('commission_log')
  213. ->where('user_id', $userId)
  214. ->order('createtime desc')
  215. ->limit(5)
  216. ->select();
  217. }
  218. /**
  219. * 查询订单基本信息(不触发事件)
  220. */
  221. public function getOrderInfo()
  222. {
  223. $orderId = $this->request->param('order_id');
  224. if (empty($orderId)) {
  225. $this->error('请提供订单ID');
  226. }
  227. // 查询订单信息
  228. $order = OrderModel::with(['items', 'user'])
  229. ->where('id', $orderId)
  230. ->find();
  231. if (!$order) {
  232. $this->error('订单不存在');
  233. }
  234. // 查询用户信息
  235. $user = UserModel::where('id', $order->user_id)->find();
  236. // 查询当前分销数据
  237. $currentStats = $this->getCommissionStats($order, $user);
  238. $result = [
  239. 'order_info' => [
  240. 'id' => $order->id,
  241. 'order_sn' => $order->order_sn,
  242. 'user_id' => $order->user_id,
  243. 'status' => $order->status,
  244. 'pay_status' => $order->pay_status,
  245. 'total_price' => $order->total_price,
  246. 'pay_time' => $order->pay_time ? date('Y-m-d H:i:s', $order->pay_time) : null,
  247. 'createtime' => date('Y-m-d H:i:s', $order->createtime),
  248. ],
  249. 'user_info' => [
  250. 'id' => $user->id,
  251. 'username' => $user->username,
  252. 'nickname' => $user->nickname,
  253. 'mobile' => $user->mobile,
  254. 'parent_user_id' => $user->parent_user_id,
  255. 'bind_time' => $user->bind_time ? date('Y-m-d H:i:s', $user->bind_time) : null,
  256. 'commission' => $user->commission,
  257. ],
  258. 'order_items' => $this->formatOrderItems($order->items),
  259. 'current_commission_stats' => $currentStats
  260. ];
  261. $this->success('订单信息查询成功', $result);
  262. }
  263. /**
  264. * 批量测试多个订单
  265. */
  266. public function batchTestOrders()
  267. {
  268. // 检查是否为开发环境
  269. if (config('app_debug') !== true) {
  270. $this->error('此接口仅在开发环境下可用');
  271. }
  272. $orderIds = $this->request->param('order_ids');
  273. if (empty($orderIds) || !is_array($orderIds)) {
  274. $this->error('请提供订单ID数组');
  275. }
  276. $results = [];
  277. foreach ($orderIds as $orderId) {
  278. try {
  279. // 查询订单信息
  280. $order = OrderModel::with(['items', 'user'])->where('id', $orderId)->find();
  281. if (!$order) {
  282. $results[$orderId] = ['error' => '订单不存在'];
  283. continue;
  284. }
  285. $user = UserModel::where('id', $order->user_id)->find();
  286. if (!$user) {
  287. $results[$orderId] = ['error' => '用户不存在'];
  288. continue;
  289. }
  290. // 记录触发前状态
  291. $beforeStats = $this->getCommissionStats($order, $user);
  292. // 触发事件
  293. Hook::listen('order_paid_after', [
  294. 'order' => $order,
  295. 'user' => $user
  296. ]);
  297. // 记录触发后状态
  298. $afterStats = $this->getCommissionStats($order, $user);
  299. $results[$orderId] = [
  300. 'success' => true,
  301. 'order_sn' => $order->order_sn,
  302. 'user_nickname' => $user->nickname,
  303. 'changes' => $this->compareStats($beforeStats, $afterStats)
  304. ];
  305. } catch (\Exception $e) {
  306. $results[$orderId] = ['error' => $e->getMessage()];
  307. }
  308. }
  309. $this->success('批量测试完成', [
  310. 'total_orders' => count($orderIds),
  311. 'results' => $results
  312. ]);
  313. }
  314. }