ShopService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace App\Module\Shop\Services;
  3. use App\Module\Fund\Enums\LOG_TYPE;
  4. use App\Module\Fund\Logic\User as FundUser;
  5. use App\Module\GameItems\Services\ItemService;
  6. use App\Module\Shop\Events\ShopItemPurchased;
  7. use App\Module\Shop\Logics\ShopLogic;
  8. use App\Module\Shop\Models\ShopItem;
  9. use App\Module\Shop\Models\ShopPromotion;
  10. use App\Module\Shop\Models\ShopPromotionItem;
  11. use App\Module\Shop\Dtos\ShopItemDto;
  12. use App\Module\Shop\Dtos\ShopCategoryDto;
  13. use Exception;
  14. use Illuminate\Database\Eloquent\Collection;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Log;
  17. use UCore\Exception\LogicException;
  18. /**
  19. * 商店服务类
  20. *
  21. * 提供商店相关的服务,包括获取商品列表、购买商品等功能。
  22. * 该类是商店模块对外提供服务的主要入口,封装了商店操作的复杂逻辑。
  23. *
  24. * 所有方法均为静态方法,可直接通过类名调用。
  25. */
  26. class ShopService
  27. {
  28. /**
  29. * 获取商店商品列表
  30. *
  31. * @param array $filters 过滤条件
  32. * @param int|null $userId 用户ID(可选,用于获取用户购买数量)
  33. * @return array ShopItemDto数组
  34. */
  35. public static function getShopItems(array $filters = [], ?int $userId = null): array
  36. {
  37. $shopLogic = new ShopLogic();
  38. $shopItems = $shopLogic->getShopItems($filters);
  39. // 将模型集合转换为DTO数组
  40. return $shopItems->map(function ($shopItem) use ($userId) {
  41. return ShopItemDto::fromModel($shopItem, $userId);
  42. })->values()->all();
  43. }
  44. /**
  45. * 获取商店分类列表
  46. *
  47. * @param bool $onlyActive 是否只获取激活的分类
  48. * @return array ShopCategoryDto数组
  49. */
  50. public static function getShopCategories(bool $onlyActive = true): array
  51. {
  52. $shopLogic = new ShopLogic();
  53. $categories = $shopLogic->getShopCategories($onlyActive);
  54. // 将模型集合转换为DTO数组
  55. return $categories->map(function ($category) {
  56. return ShopCategoryDto::fromModel($category);
  57. })->values()->all();
  58. }
  59. /**
  60. * 获取商店分类树
  61. *
  62. * @param bool $onlyActive 是否只获取激活的分类
  63. * @return array 分类树
  64. */
  65. public static function getShopCategoryTree(bool $onlyActive = true): array
  66. {
  67. $shopLogic = new ShopLogic();
  68. return $shopLogic->getShopCategoryTree($onlyActive);
  69. }
  70. /**
  71. * 购买商品
  72. * 注意:由于商品结构变更,此方法需要根据消耗组和奖励组重新实现
  73. *
  74. * @param int $userId 用户ID
  75. * @param int $shopItemId 商品ID
  76. * @param int $quantity 购买数量
  77. * @param array $options 选项
  78. * @return array|bool 购买结果或失败标志
  79. */
  80. public static function buyShopItem(int $userId, int $shopItemId, int $quantity = 1, array $options = [])
  81. {
  82. try {
  83. // 获取商品信息
  84. $shopItem = ShopItem::with(['consumeGroup', 'rewardGroup'])->findOrFail($shopItemId);
  85. // 检查购买限制
  86. $shopLogic = new ShopLogic();
  87. list($canBuy, $errorMessage) = $shopLogic->checkBuyLimit($userId, $shopItem, $quantity);
  88. if (!$canBuy) {
  89. throw new LogicException($errorMessage);
  90. }
  91. // 开始事务
  92. DB::beginTransaction();
  93. // TODO: 实现消耗组逻辑 - 扣除用户资源
  94. if ($shopItem->consumeGroup) {
  95. // 这里需要实现消耗组的扣除逻辑
  96. // $consumeResult = ConsumeService::consume($userId, $shopItem->consume_group_id, $quantity);
  97. // if (!$consumeResult) {
  98. // throw new LogicException("资源不足");
  99. // }
  100. }
  101. // TODO: 实现奖励组逻辑 - 给予用户奖励
  102. $rewardResults = [];
  103. if ($shopItem->rewardGroup) {
  104. // 这里需要实现奖励组的发放逻辑
  105. // $rewardResults = RewardService::giveReward($userId, $shopItem->reward_group_id, $quantity);
  106. }
  107. // 记录购买记录
  108. $purchaseLog = $shopLogic->recordPurchase($userId, $shopItem, $quantity, 0); // 总价暂时为0
  109. // 触发商品购买事件
  110. event(new ShopItemPurchased($userId, $shopItem, $quantity, 0, $purchaseLog->id));
  111. // 提交事务
  112. DB::commit();
  113. // 返回结果
  114. return [
  115. 'success' => true,
  116. 'shop_item' => [
  117. 'id' => $shopItem->id,
  118. 'name' => $shopItem->name,
  119. 'consume_group' => $shopItem->consumeGroup ? $shopItem->consumeGroup->name : null,
  120. 'reward_group' => $shopItem->rewardGroup ? $shopItem->rewardGroup->name : null
  121. ],
  122. 'rewards' => $rewardResults,
  123. 'purchase_log_id' => $purchaseLog->id
  124. ];
  125. } catch (Exception $e) {
  126. // 回滚事务
  127. if (DB::transactionLevel() > 0) {
  128. DB::rollBack();
  129. }
  130. Log::error('购买商品失败', [
  131. 'user_id' => $userId,
  132. 'shop_item_id' => $shopItemId,
  133. 'quantity' => $quantity,
  134. 'error' => $e->getMessage(),
  135. 'trace' => $e->getTraceAsString()
  136. ]);
  137. return false;
  138. }
  139. }
  140. /**
  141. * 获取用户购买记录
  142. *
  143. * @param int $userId 用户ID
  144. * @param array $filters 过滤条件
  145. * @return Collection 购买记录
  146. */
  147. public static function getUserPurchaseHistory(int $userId, array $filters = []): Collection
  148. {
  149. $shopLogic = new ShopLogic();
  150. return $shopLogic->getUserPurchaseHistory($userId, $filters);
  151. }
  152. /**
  153. * 获取商品详情
  154. *
  155. * @param int $shopItemId 商品ID
  156. * @param int|null $userId 用户ID(可选,用于获取用户购买数量)
  157. * @return ShopItemDto|null 商品详情DTO
  158. */
  159. public static function getShopItemDetail(int $shopItemId, ?int $userId = null): ?ShopItemDto
  160. {
  161. $shopItem = ShopItem::with(['category', 'consumeGroup', 'rewardGroup', 'promotions'])->find($shopItemId);
  162. if (!$shopItem) {
  163. return null;
  164. }
  165. return ShopItemDto::fromModel($shopItem, $userId);
  166. }
  167. /**
  168. * 获取促销活动列表
  169. *
  170. * @param array $filters 过滤条件
  171. * @return Collection 促销活动列表
  172. */
  173. public static function getPromotions(array $filters = []): Collection
  174. {
  175. $shopLogic = new ShopLogic();
  176. return $shopLogic->getPromotions($filters);
  177. }
  178. /**
  179. * 获取促销活动详情
  180. *
  181. * @param int $promotionId 促销活动ID
  182. * @return ShopPromotion|null 促销活动详情
  183. */
  184. public static function getPromotionDetail(int $promotionId): ?ShopPromotion
  185. {
  186. $shopLogic = new ShopLogic();
  187. return $shopLogic->getPromotionDetail($promotionId);
  188. }
  189. /**
  190. * 添加商品到促销活动
  191. *
  192. * @param int $promotionId 促销活动ID
  193. * @param int $shopItemId 商品ID
  194. * @param int|null $customDiscountValue 自定义折扣值
  195. * @return ShopPromotionItem 促销商品关联
  196. */
  197. public static function addItemToPromotion(int $promotionId, int $shopItemId, ?int $customDiscountValue = null): ShopPromotionItem
  198. {
  199. $shopLogic = new ShopLogic();
  200. return $shopLogic->addItemToPromotion($promotionId, $shopItemId, $customDiscountValue);
  201. }
  202. /**
  203. * 从促销活动中移除商品
  204. *
  205. * @param int $promotionId 促销活动ID
  206. * @param int $shopItemId 商品ID
  207. * @return bool 是否成功
  208. */
  209. public static function removeItemFromPromotion(int $promotionId, int $shopItemId): bool
  210. {
  211. $shopLogic = new ShopLogic();
  212. return $shopLogic->removeItemFromPromotion($promotionId, $shopItemId);
  213. }
  214. /**
  215. * 获取商品的价格信息
  216. * 注意:由于商品不再有固定价格,此方法需要根据消耗组重新实现
  217. *
  218. * @param int $shopItemId 商品ID
  219. * @return array 价格信息
  220. */
  221. public static function getItemPriceInfo(int $shopItemId): array
  222. {
  223. $shopItem = ShopItem::with(['consumeGroup', 'rewardGroup'])->findOrFail($shopItemId);
  224. $promotion = $shopItem->getActivePromotion();
  225. return [
  226. 'consume_group' => $shopItem->consumeGroup ? [
  227. 'id' => $shopItem->consumeGroup->id,
  228. 'name' => $shopItem->consumeGroup->name,
  229. 'description' => $shopItem->consumeGroup->description
  230. ] : null,
  231. 'reward_group' => $shopItem->rewardGroup ? [
  232. 'id' => $shopItem->rewardGroup->id,
  233. 'name' => $shopItem->rewardGroup->name,
  234. 'description' => $shopItem->rewardGroup->description
  235. ] : null,
  236. 'promotion' => $promotion ? [
  237. 'id' => $promotion->id,
  238. 'name' => $promotion->name,
  239. 'discount_type' => $promotion->discount_type,
  240. 'discount_value' => $promotion->discount_value,
  241. 'end_time' => $promotion->end_time ? $promotion->end_time->toDateTimeString() : null
  242. ] : null
  243. ];
  244. }
  245. }