| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace App\Module\AppGame\Handler\Land;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\AppGame\Proto\LandInfoDto;
- use App\Module\Farm\Services\CropService;
- use App\Module\Farm\Services\LandService;
- use App\Module\Farm\Validations\CropHarvestValidation;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Common\LastData;
- use Uraus\Kku\Request\RequestLandHarvest;
- use Uraus\Kku\Response\ResponseLandHarvest;
- use UCore\Exception\LogicException;
- /**
- * 处理收获操作请求
- */
- class HarvestHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理收获操作请求
- *
- * @param RequestLandHarvest $data 收获操作请求数据
- * @return ResponseLandHarvest 收获操作响应
- */
- public function handle(Message $data): Message
- {
- // 创建响应对象
- $response = new ResponseLandHarvest();
- try {
- // 获取请求参数
- $landId = $data->getLandId();
- $userId = $this->user_id;
- // 先进行验证,避免不必要的事务开销
- $validation = new CropHarvestValidation([
- 'user_id' => $userId,
- 'land_id' => $landId
- ]);
- // 验证数据
- $validation->validated();
- // 验证通过后,开启事务
- DB::beginTransaction();
- // 调用收获服务
- $harvestResult = CropService::harvestCrop($userId, $landId);
- if ($harvestResult->error) {
- throw new LogicException($harvestResult->message ?: "收获失败,请检查土地状态或作物生长阶段");
- }
- // 提交事务
- DB::commit();
- // 获取更新后的土地信息并返回
- $updatedLand = LandService::getUserLand($userId, $landId);
- if ($updatedLand) {
- // 创建LastData对象
- $lastData = new LastData();
- $landData = LandInfoDto::toDataLand($updatedLand);
- $lastData->setLands([$landData]);
- // 设置LastData到响应
- $this->response->setLastData($lastData);
- }
- // 设置响应状态
- $this->response->setCode(0);
- $this->response->setMsg('收获成功');
- Log::info('用户收获成功', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'harvest_result' => $harvestResult->data ?? []
- ]);
- } catch (\UCore\Exception\ValidateException $e) {
- // 验证失败,此时可能还没有开启事务
- $this->response->setCode(400);
- $this->response->setMsg($e->getMessage());
- Log::warning('收获验证失败', [
- 'user_id' => $userId ?? null,
- 'land_id' => $landId ?? null,
- 'error' => $e->getMessage()
- ]);
- } catch (LogicException $e) {
- // 业务逻辑异常,需要回滚事务
- if (DB::transactionLevel() > 0) {
- DB::rollBack();
- }
- $this->response->setCode(400);
- $this->response->setMsg($e->getMessage());
- Log::warning('用户收获失败', [
- 'user_id' => $userId ?? null,
- 'land_id' => $landId ?? null,
- 'error' => $e->getMessage()
- ]);
- } catch (\Exception $e) {
- // 系统异常,需要回滚事务
- if (DB::transactionLevel() > 0) {
- DB::rollBack();
- }
- $this->response->setCode(500);
- $this->response->setMsg('系统错误,请稍后再试');
- Log::error('收获操作异常', [
- 'user_id' => $userId ?? null,
- 'land_id' => $landId ?? null,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- return $response;
- }
- }
|