HarvestHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\CropHarvestValidation;
  8. use Google\Protobuf\Internal\Message;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Uraus\Kku\Common\LastData;
  12. use Uraus\Kku\Request\RequestLandHarvest;
  13. use Uraus\Kku\Response\ResponseLandHarvest;
  14. use UCore\Exception\LogicException;
  15. /**
  16. * 处理收获操作请求
  17. */
  18. class HarvestHandler extends BaseHandler
  19. {
  20. /**
  21. * 是否需要登录
  22. * @var bool
  23. */
  24. protected bool $need_login = true;
  25. /**
  26. * 处理收获操作请求
  27. *
  28. * @param RequestLandHarvest $data 收获操作请求数据
  29. * @return ResponseLandHarvest 收获操作响应
  30. */
  31. public function handle(Message $data): Message
  32. {
  33. // 创建响应对象
  34. $response = new ResponseLandHarvest();
  35. try {
  36. // 获取请求参数
  37. $landId = $data->getLandId();
  38. $userId = $this->user_id;
  39. // 先进行验证,避免不必要的事务开销
  40. $validation = new CropHarvestValidation([
  41. 'user_id' => $userId,
  42. 'land_id' => $landId
  43. ]);
  44. // 验证数据
  45. $validation->validated();
  46. // 验证通过后,开启事务
  47. DB::beginTransaction();
  48. // 调用收获服务
  49. $harvestResult = CropService::harvestCrop($userId, $landId);
  50. if ($harvestResult->error) {
  51. throw new LogicException($harvestResult->message ?: "收获失败,请检查土地状态或作物生长阶段");
  52. }
  53. // 提交事务
  54. DB::commit();
  55. // 获取更新后的土地信息并返回
  56. $updatedLand = LandService::getUserLand($userId, $landId);
  57. if ($updatedLand) {
  58. // 创建LastData对象
  59. $lastData = new LastData();
  60. $landData = LandInfoDto::toDataLand($updatedLand);
  61. $lastData->setLands([$landData]);
  62. // 设置LastData到响应
  63. $this->response->setLastData($lastData);
  64. }
  65. // 设置响应状态
  66. $this->response->setCode(0);
  67. $this->response->setMsg('收获成功');
  68. Log::info('用户收获成功', [
  69. 'user_id' => $userId,
  70. 'land_id' => $landId,
  71. 'harvest_result' => $harvestResult->data ?? []
  72. ]);
  73. } catch (\UCore\Exception\ValidateException $e) {
  74. // 验证失败,此时可能还没有开启事务
  75. $this->response->setCode(400);
  76. $this->response->setMsg($e->getMessage());
  77. Log::warning('收获验证失败', [
  78. 'user_id' => $userId ?? null,
  79. 'land_id' => $landId ?? null,
  80. 'error' => $e->getMessage()
  81. ]);
  82. } catch (LogicException $e) {
  83. // 业务逻辑异常,需要回滚事务
  84. if (DB::transactionLevel() > 0) {
  85. DB::rollBack();
  86. }
  87. $this->response->setCode(400);
  88. $this->response->setMsg($e->getMessage());
  89. Log::warning('用户收获失败', [
  90. 'user_id' => $userId ?? null,
  91. 'land_id' => $landId ?? 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. 'error' => $e->getMessage(),
  105. 'trace' => $e->getTraceAsString()
  106. ]);
  107. }
  108. return $response;
  109. }
  110. }