Test.php 11 KB

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