InitializeUserLandsCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Module\Farm\Commands;
  3. use App\Module\Farm\Logics\HouseLogic;
  4. use App\Module\Farm\Logics\LandLogic;
  5. use App\Module\Farm\Models\FarmLand;
  6. use App\Module\Farm\Models\FarmUser;
  7. use App\Module\Farm\Enums\LAND_TYPE;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. class InitializeUserLandsCommand extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'farm:initialize-user-lands {--user_id= : 指定用户ID,不指定则处理所有用户}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = '根据用户房屋等级初始化用户的土地';
  25. /**
  26. * Execute the console command.
  27. */
  28. public function handle()
  29. {
  30. $this->info('开始初始化用户土地...');
  31. // 获取用户ID参数
  32. $userId = $this->option('user_id');
  33. // 创建逻辑类实例
  34. $houseLogic = new HouseLogic();
  35. $landLogic = new LandLogic();
  36. try {
  37. // 根据是否指定用户ID获取用户列表
  38. if ($userId) {
  39. $users = FarmUser::where('user_id', $userId)->get();
  40. $this->info("处理指定用户 ID: {$userId}");
  41. } else {
  42. $users = FarmUser::all();
  43. $this->info("处理所有用户,共 " . $users->count() . " 个用户");
  44. }
  45. $bar = $this->output->createProgressBar($users->count());
  46. $bar->start();
  47. $totalCreated = 0;
  48. $totalUsers = 0;
  49. foreach ($users as $user) {
  50. $userId = $user->user_id;
  51. $houseLevel = $user->house_level;
  52. // 获取该等级房屋可用的土地数量
  53. $availableLands = $houseLogic->getAvailableLandsCount($houseLevel);
  54. // 获取用户当前的土地
  55. $userLands = FarmLand::where('user_id', $userId)->get();
  56. $currentLandsCount = $userLands->count();
  57. // 如果当前土地数量小于可用土地数量,需要创建新土地
  58. if ($currentLandsCount < $availableLands) {
  59. $landsToAdd = $availableLands - $currentLandsCount;
  60. // 获取已使用的位置
  61. $usedPositions = $userLands->pluck('position')->toArray();
  62. // 开始创建新土地
  63. DB::beginTransaction();
  64. try {
  65. // 从位置1开始,找到未使用的位置创建土地
  66. $position = 1;
  67. $createdCount = 0;
  68. while ($createdCount < $landsToAdd) {
  69. // 如果位置未被使用,创建新土地
  70. if (!in_array($position, $usedPositions)) {
  71. $land = $landLogic->createLand($userId, $position, LAND_TYPE::NORMAL->value);
  72. if ($land) {
  73. $createdCount++;
  74. $totalCreated++;
  75. }
  76. }
  77. $position++;
  78. // 防止无限循环
  79. if ($position > 100) {
  80. $this->warn("用户 {$userId} 创建土地时达到最大位置限制");
  81. break;
  82. }
  83. }
  84. DB::commit();
  85. if ($createdCount > 0) {
  86. $totalUsers++;
  87. }
  88. } catch (\Exception $e) {
  89. DB::rollBack();
  90. $this->error("用户 {$userId} 创建土地失败: " . $e->getMessage());
  91. Log::error('初始化用户土地失败', [
  92. 'user_id' => $userId,
  93. 'error' => $e->getMessage(),
  94. 'trace' => $e->getTraceAsString()
  95. ]);
  96. }
  97. }
  98. $bar->advance();
  99. }
  100. $bar->finish();
  101. $this->newLine(2);
  102. $this->info("初始化完成!共为 {$totalUsers} 个用户创建了 {$totalCreated} 块土地。");
  103. } catch (\Exception $e) {
  104. $this->error('初始化用户土地失败: ' . $e->getMessage());
  105. Log::error('初始化用户土地失败', [
  106. 'error' => $e->getMessage(),
  107. 'trace' => $e->getTraceAsString()
  108. ]);
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. }