| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Module\AppGame\Handler\Land;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\Farm\Services\LandService;
- use App\Module\Farm\Validations\LandUpgradeValidation;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Request\RequestLandUp;
- use Uraus\Kku\Response\ResponseLandUp;
- use UCore\Exception\LogicException;
- /**
- * 处理土地升级请求
- */
- class UpHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- *
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理土地升级请求
- *
- * @param RequestLandUp $data 土地升级请求数据
- * @return ResponseLandUp 土地升级响应
- */
- public function handle(Message $data): Message
- {
- // 创建响应对象
- $response = new ResponseLandUp();
- // 获取请求参数
- $landId = $data->getLandId();
- $userId = $this->user_id;
- // 先进行验证,避免不必要的事务开销
- $validation = new LandUpgradeValidation([
- 'user_id' => $userId,
- 'land_id' => $landId
- ]);
- // 验证数据
- $validation->validated();
- // 从验证结果中获取升级配置和土地信息
- $upgradeConfig = $validation->upgrade_config;
- $land = $validation->land;
- $targetType = $upgradeConfig->to_type_id;
- dd($validation);
- try {
- // 验证通过后,开启事务
- DB::beginTransaction();
- // 执行业务逻辑(不再需要验证)
- $result = LandService::upgradeLand($userId, $landId, $targetType);
- if (!$result) {
- throw new LogicException("升级土地失败");
- }
- // 提交事务
- DB::commit();
- // 记录成功日志
- Log::info('用户土地升级成功', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'old_type' => $land->land_type,
- 'new_type' => $targetType
- ]);
- } catch (\Exception $e) {
- // 系统异常,需要回滚事务
- if (DB::transactionLevel() > 0) {
- DB::rollBack();
- }
- Log::error('用户土地升级系统异常', [
- 'user_id' => $userId,
- 'land_id' => $landId ?? null,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw new LogicException('系统异常,请稍后重试');
- }
- return $response;
- }
- }
|