UrsUpdateTalentLevelCommand.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Module\UrsPromotion\Commands;
  3. use App\Module\UrsPromotion\Services\UrsTalentService;
  4. use App\Module\UrsPromotion\Services\UrsUserMappingService;
  5. use App\Module\UrsPromotion\Models\UrsUserMapping;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * URS用户达人等级更新命令
  10. *
  11. * 用于更新指定用户或批量更新用户的达人等级
  12. * 支持单个用户更新和批量更新功能
  13. * php artisan urs:update-talent-level 39296
  14. *
  15. */
  16. class UrsUpdateTalentLevelCommand extends Command
  17. {
  18. /**
  19. * 命令签名
  20. */
  21. protected $signature = 'urs:update-talent-level
  22. {user_id? : 农场用户ID,不指定则更新所有用户}
  23. {--batch-size=100 : 批处理大小}
  24. {--force : 强制重新计算等级}
  25. {--dry-run : 仅模拟运行,不实际执行更新}';
  26. /**
  27. * 命令描述
  28. */
  29. protected $description = 'URS用户达人等级更新命令 - 计算并更新用户的达人等级';
  30. /**
  31. * 执行命令
  32. */
  33. public function handle()
  34. {
  35. $userId = $this->argument('user_id');
  36. $batchSize = (int) $this->option('batch-size');
  37. $force = $this->option('force');
  38. $dryRun = $this->option('dry-run');
  39. $this->info('=== URS用户达人等级更新命令 ===');
  40. if ($dryRun) {
  41. $this->warn('模拟运行模式 - 不会实际执行更新操作');
  42. }
  43. try {
  44. if ($userId) {
  45. // 更新指定用户
  46. return $this->updateSingleUser((int) $userId, $force, $dryRun);
  47. } else {
  48. // 批量更新所有用户
  49. return $this->updateAllUsers($batchSize, $force, $dryRun);
  50. }
  51. } catch (\Exception $e) {
  52. $this->error('命令执行失败: ' . $e->getMessage());
  53. Log::error('URS达人等级更新命令执行失败', [
  54. 'user_id' => $userId,
  55. 'batch_size' => $batchSize,
  56. 'force' => $force,
  57. 'dry_run' => $dryRun,
  58. 'error' => $e->getMessage(),
  59. 'trace' => $e->getTraceAsString()
  60. ]);
  61. return 1;
  62. }
  63. }
  64. /**
  65. * 更新指定用户的达人等级
  66. *
  67. * @param int $userId 农场用户ID
  68. * @param bool $force 是否强制重新计算
  69. * @param bool $dryRun 是否仅模拟运行
  70. * @return int 命令退出码
  71. */
  72. private function updateSingleUser(int $userId, bool $force, bool $dryRun): int
  73. {
  74. $this->info("开始更新用户 {$userId} 的达人等级...");
  75. // 检查用户是否有URS映射关系
  76. $ursUserId = UrsUserMappingService::getMappingUrsUserId($userId);
  77. if ($ursUserId === 0) {
  78. $this->error("用户 {$userId} 没有URS映射关系,无法更新达人等级");
  79. return 1;
  80. }
  81. $this->info("找到URS用户ID: {$ursUserId}");
  82. if ($dryRun) {
  83. $this->info("模拟运行:将调用 UrsTalentService::updateTalentLevel({$userId})");
  84. return 0;
  85. }
  86. try {
  87. // 调用达人等级更新服务
  88. $talentDto = UrsTalentService::updateTalentLevel($userId);
  89. if ($talentDto) {
  90. $this->info("✓ 成功更新用户 {$userId} 的达人等级");
  91. $this->table(['属性', '值'], [
  92. ['URS用户ID', $talentDto->ursUserId],
  93. ['达人等级', $talentDto->talentLevel],
  94. ['等级名称', $talentDto->talentName],
  95. ['直推人数', $talentDto->directCount],
  96. ['间推人数', $talentDto->indirectCount],
  97. ['三推人数', $talentDto->thirdCount],
  98. ['团队总人数', $talentDto->promotionCount],
  99. ['最后更新时间', $talentDto->lastLevelUpdateTime ?? '未更新']
  100. ]);
  101. } else {
  102. $this->error("✗ 用户 {$userId} 达人等级更新失败");
  103. return 1;
  104. }
  105. Log::info('URS达人等级更新成功', [
  106. 'user_id' => $userId,
  107. 'urs_user_id' => $ursUserId,
  108. 'talent_level' => $talentDto->talentLevel,
  109. 'talent_name' => $talentDto->talentName,
  110. 'force' => $force
  111. ]);
  112. return 0;
  113. } catch (\Exception $e) {
  114. $this->error("✗ 用户 {$userId} 达人等级更新失败: " . $e->getMessage());
  115. Log::error('URS达人等级更新失败', [
  116. 'user_id' => $userId,
  117. 'urs_user_id' => $ursUserId,
  118. 'error' => $e->getMessage(),
  119. 'trace' => $e->getTraceAsString()
  120. ]);
  121. return 1;
  122. }
  123. }
  124. /**
  125. * 批量更新所有用户的达人等级
  126. *
  127. * @param int $batchSize 批处理大小
  128. * @param bool $force 是否强制重新计算
  129. * @param bool $dryRun 是否仅模拟运行
  130. * @return int 命令退出码
  131. */
  132. private function updateAllUsers(int $batchSize, bool $force, bool $dryRun): int
  133. {
  134. $this->info("开始批量更新所有用户的达人等级...");
  135. $this->info("批处理大小: {$batchSize}");
  136. // 获取所有有效的用户映射
  137. $totalUsers = UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID)->count();
  138. if ($totalUsers === 0) {
  139. $this->warn('没有找到需要更新的用户');
  140. return 0;
  141. }
  142. $this->info("找到 {$totalUsers} 个用户需要更新");
  143. if ($dryRun) {
  144. $this->info("模拟运行:将分批处理 {$totalUsers} 个用户");
  145. return 0;
  146. }
  147. $progressBar = $this->output->createProgressBar($totalUsers);
  148. $progressBar->start();
  149. $successCount = 0;
  150. $errorCount = 0;
  151. $processedCount = 0;
  152. // 分批处理用户
  153. UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID)
  154. ->chunk($batchSize, function ($mappings) use (
  155. &$successCount, &$errorCount, &$processedCount,
  156. $progressBar, $force
  157. ) {
  158. foreach ($mappings as $mapping) {
  159. try {
  160. $talentDto = UrsTalentService::updateTalentLevel($mapping->user_id);
  161. if ($talentDto) {
  162. $successCount++;
  163. } else {
  164. $errorCount++;
  165. }
  166. } catch (\Exception $e) {
  167. $errorCount++;
  168. Log::error('批量更新用户达人等级失败', [
  169. 'user_id' => $mapping->user_id,
  170. 'urs_user_id' => $mapping->urs_user_id,
  171. 'error' => $e->getMessage()
  172. ]);
  173. }
  174. $processedCount++;
  175. $progressBar->advance();
  176. }
  177. });
  178. $progressBar->finish();
  179. $this->newLine();
  180. // 显示更新结果
  181. $this->info('=== 更新结果统计 ===');
  182. $this->table(['项目', '数量'], [
  183. ['总用户数', $totalUsers],
  184. ['处理用户数', $processedCount],
  185. ['成功更新数', $successCount],
  186. ['失败数量', $errorCount]
  187. ]);
  188. Log::info('URS达人等级批量更新完成', [
  189. 'total_users' => $totalUsers,
  190. 'processed_count' => $processedCount,
  191. 'success_count' => $successCount,
  192. 'error_count' => $errorCount,
  193. 'batch_size' => $batchSize,
  194. 'force' => $force
  195. ]);
  196. if ($errorCount > 0) {
  197. $this->warn("有 {$errorCount} 个用户更新失败,请检查日志");
  198. return 1;
  199. }
  200. $this->info('所有用户达人等级更新完成');
  201. return 0;
  202. }
  203. }