CheckUserLandsAfterLoginJob.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Module\Farm\Jobs;
  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\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Queue\SerializesModels;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. use UCore\Queue\QueueJob;
  16. /**
  17. * 登录后检查用户土地数量是否符合房屋等级的队列任务(异步处理)
  18. */
  19. class CheckUserLandsAfterLoginJob extends QueueJob
  20. {
  21. /**
  22. * 用户ID
  23. *
  24. * @var int
  25. */
  26. protected $userId;
  27. /**
  28. * 创建一个新的任务实例
  29. *
  30. * @param int $userId 用户ID
  31. * @return void
  32. */
  33. public function __construct(int $userId)
  34. {
  35. $this->userId = $userId;
  36. }
  37. /**
  38. * 执行任务
  39. *
  40. * @return bool
  41. */
  42. public function run(): bool
  43. {
  44. try {
  45. Log::info('开始检查用户土地数量', [
  46. 'user_id' => $this->userId
  47. ]);
  48. // 获取用户信息
  49. $farmUser = FarmUser::where('user_id', $this->userId)->first();
  50. if (!$farmUser) {
  51. Log::warning('用户农场信息不存在', [
  52. 'user_id' => $this->userId
  53. ]);
  54. return true; // 返回true表示任务已完成,不需要重试
  55. }
  56. // 获取用户当前房屋等级
  57. $houseLevel = $farmUser->house_level;
  58. // 获取房屋等级对应的可用土地数量
  59. $houseLogic = new HouseLogic();
  60. $availableLands = $houseLogic->getAvailableLandsCount($houseLevel);
  61. // 获取用户当前的土地数量
  62. $currentLandsCount = FarmLand::where('user_id', $this->userId)->count();
  63. Log::info('用户土地数量检查', [
  64. 'user_id' => $this->userId,
  65. 'house_level' => $houseLevel,
  66. 'available_lands' => $availableLands,
  67. 'current_lands_count' => $currentLandsCount
  68. ]);
  69. // 如果用户土地数量少于可用土地数量,则创建新土地
  70. if ($currentLandsCount < $availableLands) {
  71. $this->createMissingLands($availableLands, $currentLandsCount);
  72. }
  73. return true;
  74. } catch (\Exception $e) {
  75. Log::error('检查用户土地数量失败', [
  76. 'user_id' => $this->userId,
  77. 'error' => $e->getMessage(),
  78. 'trace' => $e->getTraceAsString()
  79. ]);
  80. return false; // 返回false表示任务失败,需要重试
  81. }
  82. }
  83. /**
  84. * 创建缺少的土地
  85. *
  86. * @param int $availableLands 可用土地数量
  87. * @param int $currentLandsCount 当前土地数量
  88. * @return void
  89. */
  90. protected function createMissingLands(int $availableLands, int $currentLandsCount): void
  91. {
  92. // 计算需要创建的土地数量
  93. $landsToCreate = $availableLands - $currentLandsCount;
  94. if ($landsToCreate <= 0) {
  95. return;
  96. }
  97. Log::info('开始创建缺少的土地', [
  98. 'user_id' => $this->userId,
  99. 'lands_to_create' => $landsToCreate
  100. ]);
  101. // 获取用户当前的土地
  102. $userLands = FarmLand::where('user_id', $this->userId)->get();
  103. // 获取已使用的位置
  104. $usedPositions = $userLands->pluck('position')->toArray();
  105. // 创建土地逻辑实例
  106. $landLogic = new LandLogic();
  107. // 开始创建新土地
  108. DB::beginTransaction();
  109. try {
  110. // 从位置1开始,找到未使用的位置创建土地
  111. $position = 1;
  112. $createdCount = 0;
  113. while ($createdCount < $landsToCreate) {
  114. // 如果位置未被使用,创建新土地
  115. if (!in_array($position, $usedPositions)) {
  116. $land = $landLogic->createLand($this->userId, $position, LAND_TYPE::NORMAL->value);
  117. if ($land) {
  118. $createdCount++;
  119. Log::info('创建新土地成功', [
  120. 'user_id' => $this->userId,
  121. 'land_id' => $land->id,
  122. 'position' => $position
  123. ]);
  124. }
  125. }
  126. $position++;
  127. // 防止无限循环
  128. if ($position > 100) {
  129. Log::warning('创建土地时达到最大位置限制', [
  130. 'user_id' => $this->userId,
  131. 'max_position' => 100
  132. ]);
  133. break;
  134. }
  135. }
  136. DB::commit();
  137. Log::info('创建缺少的土地完成', [
  138. 'user_id' => $this->userId,
  139. 'created_count' => $createdCount
  140. ]);
  141. } catch (\Exception $e) {
  142. DB::rollBack();
  143. Log::error('创建缺少的土地失败', [
  144. 'user_id' => $this->userId,
  145. 'error' => $e->getMessage(),
  146. 'trace' => $e->getTraceAsString()
  147. ]);
  148. throw $e;
  149. }
  150. }
  151. /**
  152. * 获取任务数据
  153. *
  154. * @return array
  155. */
  156. public function payload()
  157. {
  158. return [
  159. 'user_id' => $this->userId
  160. ];
  161. }
  162. }