| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace App\Module\AppGame\Handler\Shop;
- use App\Module\AppGame\Handler\BaseHandler;
- use App\Module\Fund\Enums\LOG_TYPE;
- use App\Module\Fund\Logic\User as FundUser;
- use App\Module\GameItems\Services\ItemService;
- use App\Module\Shop\Validations\ShopBuyValidation;
- use Google\Protobuf\Internal\Message;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Uraus\Kku\Request\RequestShopBuy;
- use Uraus\Kku\Response\ResponseShopBuy;
- use UCore\Exception\LogicException;
- /**
- * 处理商品购买请求
- */
- class BuyHandler extends BaseHandler
- {
- /**
- * 是否需要登录
- * @var bool
- */
- protected bool $need_login = true;
- /**
- * 处理商品购买请求
- *
- * @param RequestShopBuy $data 商品购买请求数据
- * @return ResponseShopBuy 商品购买响应
- */
- public function handle(Message $data): Message
- {
- // 创建响应对象
- $response = new ResponseShopBuy();
- try {
- // 获取请求参数
- $goodId = $data->getGoodId();
- $number = $data->getNumber();
- $userId = $this->user_id;
- // 先进行验证,避免不必要的事务开销
- $validation = new ShopBuyValidation([
- 'user_id' => $userId,
- 'good_id' => $goodId,
- 'number' => $number
- ]);
- // 验证数据
- $validation->validated();
- // 从验证结果中获取商品信息
- $shopItem = $validation->shop_item;
- // 验证通过后,开启事务
- DB::beginTransaction();
- // TODO: 使用消耗组和奖励组系统进行购买
- // 这里需要实现消耗组和奖励组的逻辑,暂时使用旧的逻辑作为占位符
- // 暂时的处理:如果商品有消耗组,需要调用消耗组服务
- if ($shopItem->consumeGroup) {
- // TODO: 调用消耗组服务扣除资源
- // $consumeResult = ConsumeGroupService::consume($userId, $shopItem->consume_group_id, $number);
- // if (!$consumeResult['success']) {
- // throw new LogicException($consumeResult['message']);
- // }
- // 暂时的占位符逻辑 - 假设扣除100金币
- $fundResult = FundUser::handle(
- $userId,
- 1, // 假设货币类型1是金币
- -100 * $number, // 假设每个商品100金币
- LOG_TYPE::TRADE,
- $goodId,
- "购买商品:{$shopItem->name} x {$number}"
- );
- if (is_string($fundResult)) {
- throw new LogicException("购买失败:" . $fundResult);
- }
- }
- // 暂时的处理:如果商品有奖励组,需要调用奖励组服务
- if ($shopItem->rewardGroup) {
- // TODO: 调用奖励组服务发放奖励
- // $rewardResult = RewardGroupService::giveReward($userId, $shopItem->reward_group_id, $number);
- // if (!$rewardResult['success']) {
- // throw new LogicException($rewardResult['message']);
- // }
- // 暂时的占位符逻辑 - 假设给予物品ID为1的物品
- ItemService::addItem(
- $userId,
- 1, // 假设物品ID为1
- $number, // 数量
- [
- 'source_type' => 'shop_buy',
- 'source_id' => $goodId,
- 'details' => [
- 'shop_item_id' => $goodId,
- 'shop_item_name' => $shopItem->name,
- 'quantity' => $number
- ]
- ]
- );
- }
- // 记录购买记录(总价暂时设为0,因为不再有固定价格)
- $shopItem->recordPurchase($userId, $number, 0);
- // 提交事务
- DB::commit();
- // 设置响应状态
- $this->response->setCode(0);
- $this->response->setMsg('购买成功');
- // 记录日志
- Log::info('用户购买商品成功', [
- 'user_id' => $userId,
- 'good_id' => $goodId,
- 'number' => $number,
- 'consume_group_id' => $shopItem->consume_group_id,
- 'reward_group_id' => $shopItem->reward_group_id,
- 'shop_item_name' => $shopItem->name
- ]);
- } catch (\UCore\Exception\ValidateException $e) {
- // 验证失败,此时可能还没有开启事务
- $this->response->setCode(400);
- $this->response->setMsg($e->getMessage());
- Log::warning('商品购买验证失败', [
- 'user_id' => $userId ?? null,
- 'good_id' => $goodId ?? null,
- 'number' => $number ?? 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,
- 'good_id' => $goodId ?? null,
- 'number' => $number ?? 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,
- 'good_id' => $goodId ?? null,
- 'number' => $number ?? null,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- return $response;
- }
- }
|