WateringHandler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Enums\DISASTER_TYPE;
  6. use Google\Protobuf\Internal\Message;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. use Uraus\Kku\Request\RequestLandWatering;
  10. use Uraus\Kku\Response\ResponseLandWatering;
  11. use UCore\Exception\LogicException;
  12. /**
  13. * 处理浇水操作请求
  14. */
  15. class WateringHandler extends BaseHandler
  16. {
  17. /**
  18. * 是否需要登录
  19. * @var bool
  20. */
  21. protected bool $need_login = true;
  22. /**
  23. * 处理浇水操作请求
  24. *
  25. * @param RequestLandWatering $data 浇水操作请求数据
  26. * @return ResponseLandWatering 浇水操作响应
  27. */
  28. public function handle(Message $data): Message
  29. {
  30. // 创建响应对象
  31. $response = new ResponseLandWatering();
  32. try {
  33. // 获取请求参数
  34. $landId = $data->getLandId();
  35. $userItemId = $data->getItemId();
  36. $userId = $this->user_id;
  37. // 先进行验证,避免不必要的事务开销
  38. $validation = new \App\Module\Farm\Validations\DisasterRemovalValidation([
  39. 'user_id' => $userId,
  40. 'land_id' => $landId,
  41. 'item_id' => $userItemId,
  42. 'disaster_type' => DISASTER_TYPE::DROUGHT->value
  43. ]);
  44. // 验证数据
  45. $validation->validated();
  46. // 验证通过后,开启事务
  47. DB::beginTransaction();
  48. // 执行业务逻辑(不再需要验证)
  49. $result = CropService::removeDisasterWithItem(
  50. $userId,
  51. $landId,
  52. $userItemId,
  53. DISASTER_TYPE::DROUGHT->value,
  54. 'land_watering'
  55. );
  56. // 提交事务
  57. DB::commit();
  58. // 设置响应状态
  59. $this->response->setCode(0);
  60. $this->response->setMsg($result['message']);
  61. } catch (\UCore\Exception\ValidateException $e) {
  62. // 验证失败,此时可能还没有开启事务
  63. $this->response->setCode(400);
  64. $this->response->setMsg($e->getMessage());
  65. Log::warning('用户浇水验证失败', [
  66. 'user_id' => $this->user_id,
  67. 'land_id' => $landId ?? null,
  68. 'item_id' => $userItemId ?? null,
  69. 'error' => $e->getMessage()
  70. ]);
  71. } catch (LogicException $e) {
  72. // 业务逻辑异常,需要回滚事务
  73. if (DB::transactionLevel() > 0) {
  74. DB::rollBack();
  75. }
  76. // 设置错误响应
  77. $this->response->setCode(400);
  78. $this->response->setMsg($e->getMessage());
  79. Log::warning('用户浇水失败', [
  80. 'user_id' => $this->user_id,
  81. 'land_id' => $landId ?? null,
  82. 'item_id' => $userItemId ?? null,
  83. 'error' => $e->getMessage(),
  84. 'trace' => $e->getTraceAsString()
  85. ]);
  86. } catch (\Exception $e) {
  87. // 系统异常,需要回滚事务
  88. if (DB::transactionLevel() > 0) {
  89. DB::rollBack();
  90. }
  91. // 设置错误响应
  92. $this->response->setCode(500);
  93. $this->response->setMsg('系统错误,请稍后再试');
  94. Log::error('浇水操作异常', [
  95. 'user_id' => $this->user_id,
  96. 'land_id' => $landId ?? null,
  97. 'item_id' => $userItemId ?? null,
  98. 'error' => $e->getMessage(),
  99. 'trace' => $e->getTraceAsString()
  100. ]);
  101. }
  102. return $response;
  103. }
  104. }