|
|
@@ -3,9 +3,18 @@
|
|
|
namespace App\Module\AppGame\Handler\Shop;
|
|
|
|
|
|
use App\Module\AppGame\Handler\BaseHandler;
|
|
|
+use App\Module\Fund\Enums\LOG_TYPE;
|
|
|
+use App\Module\Fund\Service\User as FundUser;
|
|
|
+use App\Module\GameItems\Services\ItemService;
|
|
|
+use App\Module\Shop\Models\ShopItem;
|
|
|
use Google\Protobuf\Internal\Message;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+use Uraus\Kku\Common\DataItem;
|
|
|
+use Uraus\Kku\Common\LastData;
|
|
|
use Uraus\Kku\Request\RequestShopBuy;
|
|
|
use Uraus\Kku\Response\ResponseShopBuy;
|
|
|
+use UCore\Exception\LogicException;
|
|
|
|
|
|
/**
|
|
|
* 处理商品购买请求
|
|
|
@@ -29,7 +38,142 @@ class BuyHandler extends BaseHandler
|
|
|
// 创建响应对象
|
|
|
$response = new ResponseShopBuy();
|
|
|
|
|
|
- // TODO: 实现具体逻辑
|
|
|
+ try {
|
|
|
+ // 获取请求参数
|
|
|
+ $goodId = $data->getGoodId();
|
|
|
+ $number = $data->getNumber();
|
|
|
+ $userId = $this->user_id;
|
|
|
+
|
|
|
+ // 参数验证
|
|
|
+ if ($goodId <= 0) {
|
|
|
+ throw new LogicException("商品ID无效");
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($number <= 0) {
|
|
|
+ throw new LogicException("购买数量必须大于0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取商品信息
|
|
|
+ $shopItem = ShopItem::findOrFail($goodId);
|
|
|
+
|
|
|
+ if (!$shopItem->is_active) {
|
|
|
+ throw new LogicException("该商品已下架");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查购买限制
|
|
|
+ if ($shopItem->max_buy > 0) {
|
|
|
+ // 获取用户已购买数量
|
|
|
+ $boughtCount = $shopItem->getUserBoughtCount($userId);
|
|
|
+
|
|
|
+ if ($boughtCount + $number > $shopItem->max_buy) {
|
|
|
+ throw new LogicException("超出购买限制,最多还能购买" . ($shopItem->max_buy - $boughtCount) . "个");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算总价
|
|
|
+ $totalPrice = $shopItem->price * $number;
|
|
|
+
|
|
|
+ // 开始事务
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ // 扣除用户货币
|
|
|
+ $fundResult = FundUser::handle(
|
|
|
+ $userId,
|
|
|
+ $shopItem->currency_id,
|
|
|
+ -$totalPrice,
|
|
|
+ LOG_TYPE::TRADE,
|
|
|
+ $goodId,
|
|
|
+ "购买商品:{$shopItem->name} x {$number}"
|
|
|
+ );
|
|
|
+
|
|
|
+ if (is_string($fundResult)) {
|
|
|
+ throw new LogicException("购买失败:" . $fundResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加物品到用户背包
|
|
|
+ ItemService::addItem(
|
|
|
+ $userId,
|
|
|
+ $shopItem->item_id,
|
|
|
+ $number * $shopItem->item_quantity,
|
|
|
+ [
|
|
|
+ 'source_type' => 'shop_buy',
|
|
|
+ 'source_id' => $goodId,
|
|
|
+ 'details' => [
|
|
|
+ 'shop_item_id' => $goodId,
|
|
|
+ 'shop_item_name' => $shopItem->name,
|
|
|
+ 'price' => $shopItem->price,
|
|
|
+ 'quantity' => $number
|
|
|
+ ]
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ // 记录购买记录
|
|
|
+ $shopItem->recordPurchase($userId, $number, $totalPrice);
|
|
|
+
|
|
|
+ // 提交事务
|
|
|
+ DB::commit();
|
|
|
+
|
|
|
+ // 创建LastData对象,用于返回物品信息
|
|
|
+ $lastData = new LastData();
|
|
|
+ $itemList = [];
|
|
|
+
|
|
|
+ // 创建物品数据
|
|
|
+ $dataItem = new DataItem();
|
|
|
+ $dataItem->setItemId($shopItem->item_id);
|
|
|
+ $dataItem->setQuantity($number * $shopItem->item_quantity);
|
|
|
+ $itemList[] = $dataItem;
|
|
|
+
|
|
|
+ // 设置物品列表到LastData
|
|
|
+ $lastData->setItems($itemList);
|
|
|
+
|
|
|
+ // 设置LastData到响应
|
|
|
+ $this->response->setLastData($lastData);
|
|
|
+
|
|
|
+ // 设置响应状态
|
|
|
+ $this->response->setCode(0);
|
|
|
+ $this->response->setMsg('购买成功');
|
|
|
+
|
|
|
+ // 记录日志
|
|
|
+ Log::info('用户购买商品成功', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'good_id' => $goodId,
|
|
|
+ 'number' => $number,
|
|
|
+ 'total_price' => $totalPrice,
|
|
|
+ 'item_id' => $shopItem->item_id,
|
|
|
+ 'item_quantity' => $shopItem->item_quantity
|
|
|
+ ]);
|
|
|
+
|
|
|
+ } catch (LogicException $e) {
|
|
|
+ // 回滚事务
|
|
|
+ if (DB::transactionLevel() > 0) {
|
|
|
+ DB::rollBack();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置错误响应
|
|
|
+ $this->response->setCode(400);
|
|
|
+ $this->response->setMsg($e->getMessage());
|
|
|
+
|
|
|
+ Log::warning('用户购买商品失败', [
|
|
|
+ 'user_id' => $this->user_id,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ // 回滚事务
|
|
|
+ if (DB::transactionLevel() > 0) {
|
|
|
+ DB::rollBack();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置错误响应
|
|
|
+ $this->response->setCode(500);
|
|
|
+ $this->response->setMsg('系统错误,请稍后再试');
|
|
|
+
|
|
|
+ Log::error('购买商品操作异常', [
|
|
|
+ 'user_id' => $this->user_id,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
|
|
|
return $response;
|
|
|
}
|