| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <?php
- namespace App\Module\Shop\Services;
- 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\Events\ShopItemPurchased;
- use App\Module\Shop\Logics\ShopLogic;
- use App\Module\Shop\Models\ShopItem;
- use App\Module\Shop\Models\ShopPromotion;
- use App\Module\Shop\Models\ShopPromotionItem;
- use App\Module\Shop\Models\ShopPurchaseLog;
- use Exception;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use UCore\Exception\LogicException;
- /**
- * 商店服务类
- *
- * 提供商店相关的服务,包括获取商品列表、购买商品等功能。
- * 该类是商店模块对外提供服务的主要入口,封装了商店操作的复杂逻辑。
- *
- * 所有方法均为静态方法,可直接通过类名调用。
- */
- class ShopService
- {
- /**
- * 获取商店商品列表
- *
- * @param array $filters 过滤条件
- * @return Collection 商品列表
- */
- public static function getShopItems(array $filters = []): Collection
- {
- $shopLogic = new ShopLogic();
- return $shopLogic->getShopItems($filters);
- }
- /**
- * 获取商店分类列表
- *
- * @param bool $onlyActive 是否只获取激活的分类
- * @return Collection 分类列表
- */
- public static function getShopCategories(bool $onlyActive = true): Collection
- {
- $shopLogic = new ShopLogic();
- return $shopLogic->getShopCategories($onlyActive);
- }
- /**
- * 获取商店分类树
- *
- * @param bool $onlyActive 是否只获取激活的分类
- * @return array 分类树
- */
- public static function getShopCategoryTree(bool $onlyActive = true): array
- {
- $shopLogic = new ShopLogic();
- return $shopLogic->getShopCategoryTree($onlyActive);
- }
- /**
- * 购买商品
- *
- * @param int $userId 用户ID
- * @param int $shopItemId 商品ID
- * @param int $quantity 购买数量
- * @param array $options 选项
- * @return array|bool 购买结果或失败标志
- */
- public static function buyShopItem(int $userId, int $shopItemId, int $quantity = 1, array $options = [])
- {
- try {
- // 获取商品信息
- $shopItem = ShopItem::findOrFail($shopItemId);
- // 检查购买限制
- $shopLogic = new ShopLogic();
- list($canBuy, $errorMessage) = $shopLogic->checkBuyLimit($userId, $shopItem, $quantity);
- if (!$canBuy) {
- throw new LogicException($errorMessage);
- }
- // 计算总价
- $totalPrice = $shopItem->price * $quantity;
- // 开始事务
- DB::beginTransaction();
- // 扣除用户货币
- $fundResult = FundUser::handle(
- $userId,
- $shopItem->currency_id,
- -$totalPrice,
- LOG_TYPE::TRADE,
- $shopItemId,
- "购买商品:{$shopItem->name} x {$quantity}"
- );
- if (is_string($fundResult)) {
- throw new LogicException("购买失败:" . $fundResult);
- }
- // 添加物品到用户背包
- $itemResult = ItemService::addItem(
- $userId,
- $shopItem->item_id,
- $quantity * $shopItem->item_quantity,
- [
- 'source_type' => 'shop_buy',
- 'source_id' => $shopItemId,
- 'details' => [
- 'shop_item_id' => $shopItemId,
- 'shop_item_name' => $shopItem->name,
- 'price' => $shopItem->price,
- 'quantity' => $quantity
- ]
- ]
- );
- // 记录购买记录
- $purchaseLog = $shopLogic->recordPurchase($userId, $shopItem, $quantity, $totalPrice);
- // 触发商品购买事件
- event(new ShopItemPurchased($userId, $shopItem, $quantity, $totalPrice, $purchaseLog->id));
- // 提交事务
- DB::commit();
- // 返回结果
- return [
- 'success' => true,
- 'shop_item' => [
- 'id' => $shopItem->id,
- 'name' => $shopItem->name,
- 'price' => $shopItem->price,
- 'currency_id' => $shopItem->currency_id
- ],
- 'item' => [
- 'id' => $shopItem->item_id,
- 'name' => $shopItem->item->name,
- 'quantity' => $quantity * $shopItem->item_quantity
- ],
- 'total_price' => $totalPrice,
- 'purchase_log_id' => $purchaseLog->id
- ];
- } catch (Exception $e) {
- // 回滚事务
- if (DB::transactionLevel() > 0) {
- DB::rollBack();
- }
- Log::error('购买商品失败', [
- 'user_id' => $userId,
- 'shop_item_id' => $shopItemId,
- 'quantity' => $quantity,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return false;
- }
- }
- /**
- * 获取用户购买记录
- *
- * @param int $userId 用户ID
- * @param array $filters 过滤条件
- * @return Collection 购买记录
- */
- public static function getUserPurchaseHistory(int $userId, array $filters = []): Collection
- {
- $shopLogic = new ShopLogic();
- return $shopLogic->getUserPurchaseHistory($userId, $filters);
- }
- /**
- * 获取商品详情
- *
- * @param int $shopItemId 商品ID
- * @return ShopItem|null 商品详情
- */
- public static function getShopItemDetail(int $shopItemId): ?ShopItem
- {
- return ShopItem::with(['item', 'category', 'promotions'])->find($shopItemId);
- }
- /**
- * 获取促销活动列表
- *
- * @param array $filters 过滤条件
- * @return Collection 促销活动列表
- */
- public static function getPromotions(array $filters = []): Collection
- {
- $shopLogic = new ShopLogic();
- return $shopLogic->getPromotions($filters);
- }
- /**
- * 获取促销活动详情
- *
- * @param int $promotionId 促销活动ID
- * @return ShopPromotion|null 促销活动详情
- */
- public static function getPromotionDetail(int $promotionId): ?ShopPromotion
- {
- $shopLogic = new ShopLogic();
- return $shopLogic->getPromotionDetail($promotionId);
- }
- /**
- * 添加商品到促销活动
- *
- * @param int $promotionId 促销活动ID
- * @param int $shopItemId 商品ID
- * @param int|null $customDiscountValue 自定义折扣值
- * @return ShopPromotionItem 促销商品关联
- */
- public static function addItemToPromotion(int $promotionId, int $shopItemId, ?int $customDiscountValue = null): ShopPromotionItem
- {
- $shopLogic = new ShopLogic();
- return $shopLogic->addItemToPromotion($promotionId, $shopItemId, $customDiscountValue);
- }
- /**
- * 从促销活动中移除商品
- *
- * @param int $promotionId 促销活动ID
- * @param int $shopItemId 商品ID
- * @return bool 是否成功
- */
- public static function removeItemFromPromotion(int $promotionId, int $shopItemId): bool
- {
- $shopLogic = new ShopLogic();
- return $shopLogic->removeItemFromPromotion($promotionId, $shopItemId);
- }
- /**
- * 获取商品的折扣价格
- *
- * @param int $shopItemId 商品ID
- * @return array 价格信息
- */
- public static function getItemPriceInfo(int $shopItemId): array
- {
- $shopItem = ShopItem::findOrFail($shopItemId);
- $originalPrice = $shopItem->price;
- $discountedPrice = $shopItem->getDiscountedPrice();
- $promotion = $shopItem->getActivePromotion();
- return [
- 'original_price' => $originalPrice,
- 'discounted_price' => $discountedPrice,
- 'has_discount' => $discountedPrice < $originalPrice,
- 'discount_percentage' => $originalPrice > 0 ? round((1 - $discountedPrice / $originalPrice) * 100) : 0,
- 'promotion' => $promotion ? [
- 'id' => $promotion->id,
- 'name' => $promotion->name,
- 'discount_type' => $promotion->discount_type,
- 'discount_value' => $promotion->discount_value,
- 'end_time' => $promotion->end_time ? $promotion->end_time->toDateTimeString() : null
- ] : null
- ];
- }
- }
|