PickLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace App\Module\Farm\Logics;
  3. use App\Module\Farm\Models\FarmCrop;
  4. use App\Module\Farm\Models\FarmCropLog;
  5. use App\Module\Farm\Dtos\PickResultDto;
  6. use App\Module\Farm\Dtos\PickInfoDto;
  7. use App\Module\Farm\Events\CropPickedEvent;
  8. use App\Module\GameItems\Services\ItemService;
  9. use Carbon\Carbon;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. /**
  13. * 摘取逻辑类
  14. * 处理作物摘取的核心业务逻辑
  15. */
  16. class PickLogic
  17. {
  18. /**
  19. * 执行摘取操作的核心方法
  20. *
  21. * @param int $pickerId 摘取者ID
  22. * @param int $cropId 作物ID
  23. * @param int $pickAmount 摘取数量
  24. * @param string $pickSource 摘取来源
  25. * @param int|null $sourceId 来源ID
  26. * @return PickResultDto
  27. * @throws \Exception
  28. */
  29. public function executePick(int $pickerId, int $cropId, int $pickAmount, string $pickSource, ?int $sourceId = null): PickResultDto
  30. {
  31. // 事务检查确保在事务中执行
  32. if (!DB::transactionLevel()) {
  33. throw new \Exception('摘取操作必须在数据库事务中执行');
  34. }
  35. // 验证摘取状态 - 直接验证而不使用验证器
  36. $crop = FarmCrop::find($cropId);
  37. if (!$crop) {
  38. throw new \Exception('作物不存在');
  39. }
  40. // 检查作物是否处于成熟期
  41. if ($crop->growth_stage !== \App\Module\Farm\Enums\GROWTH_STAGE::MATURE) {
  42. throw new \Exception('作物未成熟,无法摘取');
  43. }
  44. // 检查是否有最终产出
  45. if ($crop->final_output_amount <= 0) {
  46. throw new \Exception('作物没有产出,无法摘取');
  47. }
  48. // 检查摘取数量是否大于0
  49. if ($pickAmount <= 0) {
  50. throw new \Exception('摘取数量必须大于0');
  51. }
  52. // 计算摘取比例
  53. $pickRatio = $pickAmount / $crop->final_output_amount;
  54. // 获取冷却时间配置
  55. $cooldownSeconds = $crop->seed->pick_cooldown_seconds ?? 1800; // 默认30分钟
  56. // 更新作物的摘取相关字段
  57. $crop->picked_amount += $pickAmount;
  58. $crop->final_output_amount -= $pickAmount;
  59. $crop->last_pick_time = now();
  60. $crop->pick_cooldown_end = now()->addSeconds($cooldownSeconds);
  61. $crop->save();
  62. // 记录摘取事件到作物日志
  63. $eventData = [
  64. 'picker_id' => $pickerId,
  65. 'pick_amount' => $pickAmount,
  66. 'remaining_amount' => $crop->pickable_amount,
  67. 'pick_ratio' => $pickRatio,
  68. 'pick_source' => $pickSource,
  69. 'source_id' => $sourceId,
  70. 'item_id' => $crop->final_output_item_id,
  71. 'total_picked' => $crop->picked_amount,
  72. 'cooldown_end' => $crop->pick_cooldown_end->toDateTimeString(),
  73. 'ip_address' => request()->ip(),
  74. 'user_agent' => request()->userAgent(),
  75. 'growth_stage' => $crop->growth_stage->value,
  76. 'land_type' => $crop->land->land_type ?? 1,
  77. ];
  78. $cropLog = FarmCropLog::logPicked(
  79. $crop->user_id,
  80. $crop->land_id,
  81. $crop->id,
  82. $crop->seed_id,
  83. $eventData
  84. );
  85. // 给摘取者添加物品到背包
  86. ItemService::addItem($pickerId, $crop->final_output_item_id, $pickAmount, [
  87. 'source_type' => $pickSource,
  88. 'source_id' => $sourceId,
  89. 'details' => [
  90. 'crop_id' => $cropId,
  91. 'pick_amount' => $pickAmount,
  92. 'pick_ratio' => $pickRatio,
  93. ],
  94. 'ip_address' => request()->ip(),
  95. 'device_info' => request()->userAgent(),
  96. ]);
  97. // 触发摘取事件
  98. event(new CropPickedEvent(
  99. $pickerId,
  100. $crop->user_id,
  101. $crop->land_id,
  102. $crop->id,
  103. $crop->final_output_item_id,
  104. $pickAmount,
  105. $crop->final_output_amount,
  106. $crop->pickable_amount,
  107. $pickSource,
  108. $sourceId,
  109. $crop->land,
  110. $crop,
  111. $cropLog
  112. ));
  113. // 记录操作日志
  114. Log::info('作物摘取成功', [
  115. 'picker_id' => $pickerId,
  116. 'crop_id' => $cropId,
  117. 'pick_amount' => $pickAmount,
  118. 'pick_source' => $pickSource,
  119. 'source_id' => $sourceId,
  120. 'crop_log_id' => $cropLog->id,
  121. ]);
  122. // 返回摘取结果DTO
  123. return PickResultDto::fromPickOperation([
  124. 'crop_id' => $crop->id,
  125. 'item_id' => $crop->final_output_item_id,
  126. 'pick_amount' => $pickAmount,
  127. 'remaining_amount' => $crop->pickable_amount,
  128. 'pick_ratio' => $pickRatio,
  129. 'pick_time' => $crop->last_pick_time,
  130. 'pick_source' => $pickSource,
  131. 'source_id' => $sourceId,
  132. 'pick_log_id' => $cropLog->id,
  133. 'can_pick_again' => $crop->pickable_amount > 0,
  134. 'next_pick_time' => $crop->pick_cooldown_end,
  135. ]);
  136. }
  137. /**
  138. * 获取作物摘取信息
  139. *
  140. * @param int $cropId 作物ID
  141. * @return PickInfoDto|null
  142. */
  143. public function getPickInfo(int $cropId): ?PickInfoDto
  144. {
  145. $crop = FarmCrop::find($cropId);
  146. if (!$crop) {
  147. return null;
  148. }
  149. return PickInfoDto::fromCrop($crop);
  150. }
  151. /**
  152. * 批量摘取多个作物
  153. *
  154. * @param int $pickerId 摘取者ID
  155. * @param array $cropRequests 摘取请求数组
  156. * @return array
  157. */
  158. public function batchPick(int $pickerId, array $cropRequests): array
  159. {
  160. $results = [];
  161. foreach ($cropRequests as $request) {
  162. try {
  163. $result = $this->executePick(
  164. $pickerId,
  165. $request['crop_id'],
  166. $request['amount'],
  167. $request['source'],
  168. $request['source_id'] ?? null
  169. );
  170. $results[] = [
  171. 'success' => true,
  172. 'crop_id' => $request['crop_id'],
  173. 'result' => $result,
  174. ];
  175. } catch (\Exception $e) {
  176. $results[] = [
  177. 'success' => false,
  178. 'crop_id' => $request['crop_id'],
  179. 'error' => $e->getMessage(),
  180. ];
  181. Log::warning('批量摘取失败', [
  182. 'picker_id' => $pickerId,
  183. 'crop_id' => $request['crop_id'],
  184. 'error' => $e->getMessage(),
  185. ]);
  186. }
  187. }
  188. return $results;
  189. }
  190. }