Test.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. use app\common\Service\Commission\Agent as AgentService;
  9. /**
  10. * 测试控制器
  11. * 仅用于开发和测试环境
  12. */
  13. class Test extends Api
  14. {
  15. protected $noNeedLogin = ['getCommissionStats'];
  16. protected $noNeedRight = ['*'];
  17. /**
  18. * 测试支付后分销事件
  19. */
  20. public function testOrderPaidEvent()
  21. {
  22. // 检查是否为开发环境
  23. // if (config('app_debug') !== true) {
  24. // $this->error('此接口仅在开发环境下可用');
  25. // }
  26. $orderId = $this->request->param('order_id');
  27. if (empty($orderId)) {
  28. $this->error('请提供订单ID');
  29. }
  30. // try {
  31. // 查询订单信息
  32. $order = OrderModel::with('items')
  33. ->where('id', $orderId)
  34. ->find();
  35. if (!$order) {
  36. $this->error('订单不存在');
  37. }
  38. // 查询用户信息
  39. $user = UserModel::where('id', $order->user_id)->find();
  40. if (!$user) {
  41. $this->error('用户不存在');
  42. }
  43. // 手动触发支付后事件
  44. $eventData = [
  45. 'order' => $order,
  46. 'user' => $user
  47. ];
  48. Hook::listen('order_paid_after', $eventData);
  49. $this->success('支付后事件测试完成',);
  50. // } catch (\Exception $e) {
  51. // $this->error('测试失败: ' . $e->getMessage());
  52. // }
  53. }
  54. /**
  55. * 获取分销统计数据
  56. */
  57. public function getCommissionStats()
  58. {
  59. //$userId = $payload['user_id'];
  60. $userId = $this->request->param('user_id');
  61. if (empty($userId)) {
  62. $this->error('请提供用户ID');
  63. }
  64. $agent = new AgentService($userId);
  65. if ($agent->user) {
  66. Db::transaction(function () use ($agent) {
  67. $agent->runAgentUpgradePlan();
  68. });
  69. }
  70. }
  71. /**
  72. * 格式化订单商品
  73. */
  74. private function formatOrderItems($items)
  75. {
  76. $result = [];
  77. foreach ($items as $item) {
  78. $result[] = [
  79. 'item_id' => $item->id,
  80. 'goods_name' => $item->goods_name,
  81. 'price' => $item->price,
  82. 'quantity' => $item->quantity,
  83. 'total_price' => $item->total_price,
  84. 'is_commission' => isset($item->ext['is_commission']) ? $item->ext['is_commission'] : true,
  85. 'commission_rate' => $item->ext['commission_rate'] ?? 0,
  86. ];
  87. }
  88. return $result;
  89. }
  90. /**
  91. * 比较前后数据变化
  92. */
  93. private function compareStats($before, $after)
  94. {
  95. $changes = [];
  96. // 用户佣金变化
  97. if ($before['user_commission'] != $after['user_commission']) {
  98. $changes['user_commission'] = [
  99. 'before' => $before['user_commission'],
  100. 'after' => $after['user_commission'],
  101. 'change' => $after['user_commission'] - $before['user_commission']
  102. ];
  103. }
  104. // 代理商信息变化
  105. if ($before['agent_info'] && $after['agent_info']) {
  106. $beforeAgent = $before['agent_info'];
  107. $afterAgent = $after['agent_info'];
  108. if ($beforeAgent['total_income'] != $afterAgent['total_income']) {
  109. $changes['agent_total_income'] = [
  110. 'before' => $beforeAgent['total_income'],
  111. 'after' => $afterAgent['total_income'],
  112. 'change' => $afterAgent['total_income'] - $beforeAgent['total_income']
  113. ];
  114. }
  115. if ($beforeAgent['child_order_money_all'] != $afterAgent['child_order_money_all']) {
  116. $changes['agent_child_order_money'] = [
  117. 'before' => $beforeAgent['child_order_money_all'],
  118. 'after' => $afterAgent['child_order_money_all'],
  119. 'change' => $afterAgent['child_order_money_all'] - $beforeAgent['child_order_money_all']
  120. ];
  121. }
  122. }
  123. // 分销订单数量变化
  124. if ($before['commission_orders_count'] != $after['commission_orders_count']) {
  125. $changes['commission_orders_count'] = [
  126. 'before' => $before['commission_orders_count'],
  127. 'after' => $after['commission_orders_count'],
  128. 'change' => $after['commission_orders_count'] - $before['commission_orders_count']
  129. ];
  130. }
  131. // 佣金记录数量变化
  132. if ($before['rewards_count'] != $after['rewards_count']) {
  133. $changes['rewards_count'] = [
  134. 'before' => $before['rewards_count'],
  135. 'after' => $after['rewards_count'],
  136. 'change' => $after['rewards_count'] - $before['rewards_count']
  137. ];
  138. }
  139. // 总佣金金额变化
  140. if ($before['total_reward_amount'] != $after['total_reward_amount']) {
  141. $changes['total_reward_amount'] = [
  142. 'before' => $before['total_reward_amount'],
  143. 'after' => $after['total_reward_amount'],
  144. 'change' => $after['total_reward_amount'] - $before['total_reward_amount']
  145. ];
  146. }
  147. return $changes;
  148. }
  149. /**
  150. * 获取最近的分销日志
  151. */
  152. private function getRecentCommissionLogs($userId)
  153. {
  154. return Db::name('commission_log')
  155. ->where('user_id', $userId)
  156. ->order('createtime desc')
  157. ->limit(5)
  158. ->select();
  159. }
  160. /**
  161. * 查询订单基本信息(不触发事件)
  162. */
  163. public function getOrderInfo()
  164. {
  165. $orderId = $this->request->param('order_id');
  166. if (empty($orderId)) {
  167. $this->error('请提供订单ID');
  168. }
  169. // 查询订单信息
  170. $order = OrderModel::with(['items', 'user'])
  171. ->where('id', $orderId)
  172. ->find();
  173. if (!$order) {
  174. $this->error('订单不存在');
  175. }
  176. // 查询用户信息
  177. $user = UserModel::where('id', $order->user_id)->find();
  178. // 查询当前分销数据
  179. $currentStats = $this->getCommissionStats($order, $user);
  180. $result = [
  181. 'order_info' => [
  182. 'id' => $order->id,
  183. 'order_sn' => $order->order_sn,
  184. 'user_id' => $order->user_id,
  185. 'status' => $order->status,
  186. 'pay_status' => $order->pay_status,
  187. 'total_price' => $order->total_price,
  188. 'pay_time' => $order->pay_time ? date('Y-m-d H:i:s', $order->pay_time) : null,
  189. 'createtime' => date('Y-m-d H:i:s', $order->createtime),
  190. ],
  191. 'user_info' => [
  192. 'id' => $user->id,
  193. 'username' => $user->username,
  194. 'nickname' => $user->nickname,
  195. 'mobile' => $user->mobile,
  196. 'parent_user_id' => $user->parent_user_id,
  197. 'bind_time' => $user->bind_time ? date('Y-m-d H:i:s', $user->bind_time) : null,
  198. 'commission' => $user->commission,
  199. ],
  200. 'order_items' => $this->formatOrderItems($order->items),
  201. 'current_commission_stats' => $currentStats
  202. ];
  203. $this->success('订单信息查询成功', $result);
  204. }
  205. /**
  206. * 批量测试多个订单
  207. */
  208. public function batchTestOrders()
  209. {
  210. // 检查是否为开发环境
  211. if (config('app_debug') !== true) {
  212. $this->error('此接口仅在开发环境下可用');
  213. }
  214. $orderIds = $this->request->param('order_ids');
  215. if (empty($orderIds) || !is_array($orderIds)) {
  216. $this->error('请提供订单ID数组');
  217. }
  218. $results = [];
  219. foreach ($orderIds as $orderId) {
  220. try {
  221. // 查询订单信息
  222. $order = OrderModel::with(['items', 'user'])->where('id', $orderId)->find();
  223. if (!$order) {
  224. $results[$orderId] = ['error' => '订单不存在'];
  225. continue;
  226. }
  227. $user = UserModel::where('id', $order->user_id)->find();
  228. if (!$user) {
  229. $results[$orderId] = ['error' => '用户不存在'];
  230. continue;
  231. }
  232. // 记录触发前状态
  233. $beforeStats = $this->getCommissionStats($order, $user);
  234. // 触发事件
  235. Hook::listen('order_paid_after', [
  236. 'order' => $order,
  237. 'user' => $user
  238. ]);
  239. // 记录触发后状态
  240. $afterStats = $this->getCommissionStats($order, $user);
  241. $results[$orderId] = [
  242. 'success' => true,
  243. 'order_sn' => $order->order_sn,
  244. 'user_nickname' => $user->nickname,
  245. 'changes' => $this->compareStats($beforeStats, $afterStats)
  246. ];
  247. } catch (\Exception $e) {
  248. $results[$orderId] = ['error' => $e->getMessage()];
  249. }
  250. }
  251. $this->success('批量测试完成', [
  252. 'total_orders' => count($orderIds),
  253. 'results' => $results
  254. ]);
  255. }
  256. }