RemoveCropHandler.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Module\AppGame\Handler\Land;
  3. use App\Module\AppGame\Handler\BaseHandler;
  4. use App\Module\Farm\Services\CropService;
  5. use App\Module\Farm\Validations\CropRemoveValidation;
  6. use App\Module\GameItems\Services\ItemService;
  7. use Google\Protobuf\Internal\Message;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Uraus\Kku\Request\RequestLandRemoveCrop;
  11. use Uraus\Kku\Response\ResponseLandRemoveCrop;
  12. use UCore\Exception\LogicException;
  13. /**
  14. * 处理铲除作物操作请求
  15. */
  16. class RemoveCropHandler extends BaseHandler
  17. {
  18. /**
  19. * 是否需要登录
  20. * @var bool
  21. */
  22. protected bool $need_login = true;
  23. /**
  24. * 处理铲除作物操作请求
  25. *
  26. * @param RequestLandRemoveCrop $data 铲除作物操作请求数据
  27. * @return ResponseLandRemoveCrop 铲除作物操作响应
  28. */
  29. public function handle(Message $data): Message
  30. {
  31. // 创建响应对象
  32. $response = new ResponseLandRemoveCrop();
  33. try {
  34. // 获取请求参数
  35. $landId = $data->getLandId();
  36. $toolItemId = $data->getUserItemId() ?? 0; // 铲除工具ID,可选
  37. $userId = $this->user_id;
  38. // 先进行验证,避免不必要的事务开销
  39. $validation = new CropRemoveValidation([
  40. 'user_id' => $userId,
  41. 'land_id' => $landId,
  42. 'tool_item_id' => $toolItemId
  43. ]);
  44. // 验证数据
  45. $validation->validated();
  46. // 验证通过后,开启事务
  47. DB::beginTransaction();
  48. // 调用铲除作物服务
  49. $result = CropService::removeCrop($userId, $landId);
  50. if (!$result) {
  51. throw new LogicException("铲除作物失败,请检查土地状态");
  52. }
  53. // 如果使用了工具,消耗工具
  54. if ($toolItemId > 0) {
  55. ItemService::consumeItem($userId, $toolItemId, null, 1, [
  56. 'source_type' => 'land_remove_crop',
  57. 'source_id' => $landId,
  58. 'details' => ['land_id' => $landId]
  59. ]);
  60. }
  61. // 提交事务
  62. DB::commit();
  63. // 设置响应状态
  64. $this->response->setCode(0);
  65. $this->response->setMsg('铲除作物成功');
  66. Log::info('用户铲除作物成功', [
  67. 'user_id' => $userId,
  68. 'land_id' => $landId,
  69. 'tool_item_id' => $toolItemId
  70. ]);
  71. } catch (\UCore\Exception\ValidateException $e) {
  72. // 验证失败,此时可能还没有开启事务
  73. $this->response->setCode(400);
  74. $this->response->setMsg($e->getMessage());
  75. Log::warning('铲除作物验证失败', [
  76. 'user_id' => $userId ?? null,
  77. 'land_id' => $landId ?? null,
  78. 'tool_item_id' => $toolItemId ?? null,
  79. 'error' => $e->getMessage()
  80. ]);
  81. } catch (LogicException $e) {
  82. // 业务逻辑异常,需要回滚事务
  83. if (DB::transactionLevel() > 0) {
  84. DB::rollBack();
  85. }
  86. $this->response->setCode(400);
  87. $this->response->setMsg($e->getMessage());
  88. Log::warning('用户铲除作物失败', [
  89. 'user_id' => $userId ?? null,
  90. 'land_id' => $landId ?? null,
  91. 'tool_item_id' => $toolItemId ?? null,
  92. 'error' => $e->getMessage()
  93. ]);
  94. } catch (\Exception $e) {
  95. // 系统异常,需要回滚事务
  96. if (DB::transactionLevel() > 0) {
  97. DB::rollBack();
  98. }
  99. $this->response->setCode(500);
  100. $this->response->setMsg('系统错误,请稍后再试');
  101. Log::error('铲除作物操作异常', [
  102. 'user_id' => $userId ?? null,
  103. 'land_id' => $landId ?? null,
  104. 'tool_item_id' => $toolItemId ?? null,
  105. 'error' => $e->getMessage(),
  106. 'trace' => $e->getTraceAsString()
  107. ]);
  108. }
  109. return $response;
  110. }
  111. }