TestPromotionHandlerCommand.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace App\Module\AppGame\Commands;
  3. use App\Module\AppGame\Handler\Promotion\InfoHandler;
  4. use App\Module\AppGame\Handler\Promotion\ListHandler;
  5. use App\Module\UrsPromotion\Services\UrsUserMappingService;
  6. use Illuminate\Console\Command;
  7. use Uraus\Kku\Request\RequestPromotionInfo;
  8. use Uraus\Kku\Request\RequestPromotionList;
  9. use Uraus\Kku\Common\RequestPage;
  10. use Uraus\Kku\Response;
  11. /**
  12. * 测试推广Handler功能
  13. */
  14. class TestPromotionHandlerCommand extends Command
  15. {
  16. /**
  17. * 命令签名
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'test:promotion-handler {user_id : 农场用户ID}';
  22. /**
  23. * 命令描述
  24. *
  25. * @var string
  26. */
  27. protected $description = '测试推广数据获取Handler功能';
  28. /**
  29. * 执行命令
  30. */
  31. public function handle()
  32. {
  33. $userId = (int)$this->argument('user_id');
  34. $this->info("开始测试推广Handler功能");
  35. $this->info("农场用户ID: {$userId}");
  36. $this->line('');
  37. // 检查用户映射关系
  38. $this->info("=== 检查用户映射关系 ===");
  39. $ursUserId = UrsUserMappingService::getUrsUserId($userId);
  40. if (!$ursUserId) {
  41. $this->error("用户未进入URS推广系统,无法测试");
  42. return;
  43. }
  44. $this->info("URS用户ID: {$ursUserId}");
  45. $this->line('');
  46. // 测试InfoHandler
  47. $this->info("=== 测试推广信息Handler ===");
  48. $this->testInfoHandler($userId);
  49. $this->line('');
  50. // 测试ListHandler
  51. $this->info("=== 测试推广列表Handler ===");
  52. $this->testListHandler($userId);
  53. $this->line('');
  54. $this->info("✅ 推广Handler测试完成!");
  55. }
  56. /**
  57. * 测试InfoHandler
  58. *
  59. * @param int $userId
  60. */
  61. private function testInfoHandler(int $userId): void
  62. {
  63. try {
  64. // 创建模拟响应对象
  65. $response = new Response();
  66. // 创建Handler
  67. $handler = new InfoHandler($response);
  68. $handler->user_id = $userId;
  69. // 创建请求数据
  70. $request = new RequestPromotionInfo();
  71. $request->setTimes(time());
  72. // 执行Handler
  73. $result = $handler->handle($request);
  74. // 输出结果
  75. $this->info("推广信息获取成功:");
  76. $this->info("- 总人数: " . $result->getTotalCount());
  77. $this->info("- 直推人数: " . $result->getDirectCount());
  78. $this->info("- 间推人数: " . $result->getIndirectCount());
  79. $this->info("- 今日团队新增: " . $result->getDayRecentCount());
  80. $this->info("- 今日直推新增: " . $result->getDayDirectCount());
  81. $this->info("- 团队活跃人数: " . $result->getActiveCount());
  82. $this->info("- 直推活跃人数: " . $result->getDirectActiveCount());
  83. // 显示收益信息
  84. $todayReward = $result->getTodayReward();
  85. if ($todayReward) {
  86. $this->info("- 今日收益:");
  87. $this->displayRewardInfo($todayReward, " ");
  88. } else {
  89. $this->info("- 今日收益: 无");
  90. }
  91. $totalReward = $result->getTotalReward();
  92. if ($totalReward) {
  93. $this->info("- 总收益:");
  94. $this->displayRewardInfo($totalReward, " ");
  95. } else {
  96. $this->info("- 总收益: 无");
  97. }
  98. } catch (\Exception $e) {
  99. $this->error("InfoHandler测试失败: " . $e->getMessage());
  100. $this->error("错误详情: " . $e->getTraceAsString());
  101. }
  102. }
  103. /**
  104. * 测试ListHandler
  105. *
  106. * @param int $userId
  107. */
  108. private function testListHandler(int $userId): void
  109. {
  110. try {
  111. // 创建模拟响应对象
  112. $response = new Response();
  113. // 创建Handler
  114. $handler = new ListHandler($response);
  115. $handler->user_id = $userId;
  116. // 创建请求数据
  117. $request = new RequestPromotionList();
  118. // 设置分页参数
  119. $page = new RequestPage();
  120. $page->setPage(1);
  121. $page->setPerPage(10);
  122. $request->setPage($page);
  123. $request->setLevel(0); // 全部等级
  124. // 执行Handler
  125. $result = $handler->handle($request);
  126. // 输出结果
  127. $this->info("推广列表获取成功:");
  128. $responsePage = $result->getPage();
  129. if ($responsePage) {
  130. $this->info("- 当前页: " . $responsePage->getCurrentPage());
  131. $this->info("- 每页大小: " . $responsePage->getPerPage());
  132. $this->info("- 总记录数: " . $responsePage->getTotal());
  133. $this->info("- 总页数: " . $responsePage->getLastPage());
  134. $this->info("- 是否有下一页: " . ($responsePage->getHasMore() ? '是' : '否'));
  135. }
  136. $list = $result->getList();
  137. $this->info("- 当前页记录数: " . count($list));
  138. // 显示前3条记录详情
  139. $displayCount = min(3, count($list));
  140. for ($i = 0; $i < $displayCount; $i++) {
  141. $item = $list[$i];
  142. $this->info(" 第" . ($i + 1) . "条:");
  143. $this->info(" 用户ID: " . $item->getUserId());
  144. $this->info(" 昵称: " . $item->getNickname());
  145. $this->info(" 头像: " . $item->getAvatar());
  146. $this->info(" 财富: " . $item->getFund2());
  147. $this->info(" 贡献: " . $item->getContribution());
  148. $this->info(" 房屋等级: " . $item->getHouseLevel());
  149. }
  150. if (count($list) > 3) {
  151. $this->info(" ... 还有 " . (count($list) - 3) . " 条记录");
  152. }
  153. } catch (\Exception $e) {
  154. $this->error("ListHandler测试失败: " . $e->getMessage());
  155. $this->error("错误详情: " . $e->getTraceAsString());
  156. }
  157. }
  158. /**
  159. * 显示奖励信息
  160. *
  161. * @param \Uraus\Kku\Common\Reward $reward
  162. * @param string $prefix
  163. */
  164. private function displayRewardInfo($reward, string $prefix = ""): void
  165. {
  166. // 显示代币奖励
  167. $coins = $reward->getCoins();
  168. if ($coins && count($coins) > 0) {
  169. $this->info($prefix . "代币奖励:");
  170. foreach ($coins as $coin) {
  171. $typeName = $this->getCoinTypeName($coin->getType());
  172. $this->info($prefix . " - {$typeName}: " . $coin->getQuantity());
  173. }
  174. }
  175. // 显示物品奖励
  176. $items = $reward->getItems();
  177. if ($items && count($items) > 0) {
  178. $this->info($prefix . "物品奖励: " . count($items) . "种");
  179. }
  180. // 显示其他奖励类型
  181. $gods = $reward->getGods();
  182. if ($gods && count($gods) > 0) {
  183. $this->info($prefix . "图腾奖励: " . count($gods) . "个");
  184. }
  185. $lands = $reward->getLands();
  186. if ($lands && count($lands) > 0) {
  187. $this->info($prefix . "土地奖励: " . count($lands) . "块");
  188. }
  189. $pets = $reward->getPets();
  190. if ($pets && count($pets) > 0) {
  191. $this->info($prefix . "宠物奖励: " . count($pets) . "只");
  192. }
  193. }
  194. /**
  195. * 获取代币类型名称
  196. *
  197. * @param int $type
  198. * @return string
  199. */
  200. private function getCoinTypeName(int $type): string
  201. {
  202. $typeNames = [
  203. 1 => '金币',
  204. 2 => '钻石',
  205. 3 => '钻石(冻结)'
  206. ];
  207. return $typeNames[$type] ?? "未知类型({$type})";
  208. }
  209. }