PesticideHandler.php 3.8 KB

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