SowHandler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Module\AppGame\Handler\Land;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\AppGame\Proto\LandInfoDto;
  5. use App\Module\Farm\Services\CropService;
  6. use App\Module\Farm\Services\LandService;
  7. use App\Module\Farm\Validations\CropPlantValidation;
  8. use App\Module\GameItems\Services\ItemService;
  9. use Google\Protobuf\Internal\Message;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. use UCore\Db\Helper;
  13. use UCore\DcatAdmin\Herlper;
  14. use Uraus\Kku\Common\LastData;
  15. use Uraus\Kku\Request\RequestLandSow;
  16. use Uraus\Kku\Response\ResponseLandSow;
  17. use UCore\Exception\LogicException;
  18. /**
  19. * 种植操作请求处理器
  20. */
  21. class SowHandler extends BaseHandler
  22. {
  23. /**
  24. * 是否需要登录
  25. * @var bool
  26. */
  27. protected bool $need_login = true;
  28. /**
  29. * 处理种植操作请求
  30. *
  31. * @param RequestLandSow $data 种植操作请求数据
  32. * @return ResponseLandSow 种植操作响应
  33. */
  34. public function handle(Message $data): Message
  35. {
  36. // 创建响应对象
  37. $response = new ResponseLandSow();
  38. // 获取请求参数
  39. $landId = $data->getLandId();
  40. $itemId = $data->getItemId();
  41. $itemInstanceId = $data->getItemInstanceId();
  42. $userId = $this->user_id;
  43. // 先进行验证,避免不必要的事务开销
  44. $validation = new CropPlantValidation([
  45. 'user_id' => $userId,
  46. 'land_id' => $landId,
  47. 'item_id' => $itemId,
  48. 'item_instance_id' => $itemInstanceId
  49. ]);
  50. // 验证数据
  51. $validation->validated();
  52. try {
  53. // 验证通过后,开启事务
  54. DB::beginTransaction();
  55. // 调用Farm模块的CropService种植作物
  56. $plantResult = CropService::plantCrop($userId, $landId, $itemId);
  57. if (!$plantResult) {
  58. throw new LogicException("种植失败,请检查土地状态");
  59. }
  60. // 获取种植日志ID
  61. $sowLogId = $plantResult['log_id'] ?? 0;
  62. // 消耗种子物品
  63. ItemService::consumeItem($userId, $itemId, $itemInstanceId, 1, [
  64. 'source_type' => 'land_sow',
  65. 'source_id' => $landId,
  66. 'details' => [
  67. 'land_id' => $landId,
  68. 'sow_id' => $sowLogId
  69. ]
  70. ]);
  71. // 提交事务
  72. DB::commit();
  73. // LastData 由中间件处理
  74. Log::info('用户种植成功', [
  75. 'user_id' => $userId,
  76. 'land_id' => $landId,
  77. 'seed_id' => $itemId,
  78. 'item_instance_id' => $itemInstanceId,
  79. 'crop_id' => $plantResult['crop']->id ?? 0,
  80. 'sow_log_id' => $sowLogId
  81. ]);
  82. } catch (\Exception $e) {
  83. // 系统异常,需要回滚事务
  84. if (DB::transactionLevel() > 0) {
  85. DB::rollBack();
  86. }
  87. $this->response->setCode(500);
  88. $this->response->setMsg('系统错误,请稍后再试');
  89. Log::error('种植操作异常', [
  90. 'user_id' => $userId ?? null,
  91. 'land_id' => $landId ?? null,
  92. 'item_id' => $itemId ?? null,
  93. 'error' => $e->getMessage(),
  94. 'trace' => $e->getTraceAsString()
  95. ]);
  96. }
  97. return $response;
  98. }
  99. }