|
|
@@ -0,0 +1,275 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\GameItems\Logics;
|
|
|
+
|
|
|
+use App\Module\GameItems\Dtos\ItemChestOpenCostDto;
|
|
|
+use App\Module\GameItems\Enums\CHEST_COST_TYPE;
|
|
|
+use App\Module\GameItems\Models\ItemChestOpenCost;
|
|
|
+use App\Module\GameItems\Repositorys\ItemChestOpenCostRepository;
|
|
|
+use App\Module\User\Services\UserCurrencyService;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 宝箱开启消耗逻辑类
|
|
|
+ */
|
|
|
+class ChestOpenCostLogic
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * @var ItemChestOpenCostRepository
|
|
|
+ */
|
|
|
+ protected $costRepository;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var ItemLogic
|
|
|
+ */
|
|
|
+ protected $itemLogic;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ *
|
|
|
+ * @param ItemChestOpenCostRepository $costRepository
|
|
|
+ * @param ItemLogic $itemLogic
|
|
|
+ */
|
|
|
+ public function __construct(
|
|
|
+ ItemChestOpenCostRepository $costRepository,
|
|
|
+ ItemLogic $itemLogic
|
|
|
+ ) {
|
|
|
+ $this->costRepository = $costRepository;
|
|
|
+ $this->itemLogic = $itemLogic;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取宝箱的所有激活消耗配置
|
|
|
+ *
|
|
|
+ * @param int $chestId 宝箱ID
|
|
|
+ * @return ItemChestOpenCostDto[]
|
|
|
+ */
|
|
|
+ public function getActiveChestCosts(int $chestId): array
|
|
|
+ {
|
|
|
+ $costs = $this->costRepository->getActiveByChestId($chestId);
|
|
|
+ $result = [];
|
|
|
+
|
|
|
+ foreach ($costs as $cost) {
|
|
|
+ $result[] = ItemChestOpenCostDto::fromModel($cost, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证用户是否有足够的资源支付宝箱开启消耗
|
|
|
+ *
|
|
|
+ * @param int $userId 用户ID
|
|
|
+ * @param int $chestId 宝箱ID
|
|
|
+ * @param int $quantity 开启数量
|
|
|
+ * @return array [bool $isValid, string $message, array $costs]
|
|
|
+ */
|
|
|
+ public function validateChestOpenCosts(int $userId, int $chestId, int $quantity = 1): array
|
|
|
+ {
|
|
|
+ $costs = $this->costRepository->getActiveByChestId($chestId);
|
|
|
+
|
|
|
+ if ($costs->isEmpty()) {
|
|
|
+ return [true, '无需额外消耗', []];
|
|
|
+ }
|
|
|
+
|
|
|
+ $costDetails = [];
|
|
|
+
|
|
|
+ foreach ($costs as $cost) {
|
|
|
+ $totalQuantity = $cost->cost_quantity * $quantity;
|
|
|
+ $costDetails[] = [
|
|
|
+ 'type' => $cost->cost_type,
|
|
|
+ 'id' => $cost->cost_id,
|
|
|
+ 'quantity' => $totalQuantity,
|
|
|
+ 'model' => $cost
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 验证用户是否有足够的资源
|
|
|
+ switch ($cost->cost_type) {
|
|
|
+ case CHEST_COST_TYPE::ITEM->value:
|
|
|
+ // 验证物品数量
|
|
|
+ $userItem = $this->itemLogic->getUserItem($userId, $cost->cost_id);
|
|
|
+ if (!$userItem || $userItem->quantity < $totalQuantity) {
|
|
|
+ return [
|
|
|
+ false,
|
|
|
+ "物品不足,需要 {$totalQuantity} 个 " . ($cost->costItem->name ?? "ID:{$cost->cost_id}"),
|
|
|
+ $costDetails
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case CHEST_COST_TYPE::CURRENCY->value:
|
|
|
+ // 验证货币数量
|
|
|
+ $userCurrency = UserCurrencyService::getUserCurrency($userId, $cost->cost_id);
|
|
|
+ if ($userCurrency < $totalQuantity) {
|
|
|
+ return [
|
|
|
+ false,
|
|
|
+ "货币不足,需要 {$totalQuantity} 个 ID:{$cost->cost_id}",
|
|
|
+ $costDetails
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case CHEST_COST_TYPE::OTHER_RESOURCE->value:
|
|
|
+ // 验证其他资源,根据实际情况实现
|
|
|
+ // 这里仅作为示例,实际实现可能需要调用其他模块的服务
|
|
|
+ $hasEnoughResource = true; // 假设有足够资源
|
|
|
+ if (!$hasEnoughResource) {
|
|
|
+ return [
|
|
|
+ false,
|
|
|
+ "资源不足,需要 {$totalQuantity} 个 ID:{$cost->cost_id}",
|
|
|
+ $costDetails
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, '验证通过', $costDetails];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 扣除宝箱开启所需的消耗资源
|
|
|
+ *
|
|
|
+ * @param int $userId 用户ID
|
|
|
+ * @param array $costDetails 消耗详情数组
|
|
|
+ * @param string $reason 消耗原因
|
|
|
+ * @return bool 是否成功扣除
|
|
|
+ */
|
|
|
+ public function deductChestOpenCosts(int $userId, array $costDetails, string $reason = '开启宝箱'): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ foreach ($costDetails as $detail) {
|
|
|
+ switch ($detail['type']) {
|
|
|
+ case CHEST_COST_TYPE::ITEM->value:
|
|
|
+ // 扣除物品
|
|
|
+ $result = $this->itemLogic->consumeUserItem(
|
|
|
+ $userId,
|
|
|
+ $detail['id'],
|
|
|
+ $detail['quantity'],
|
|
|
+ $reason
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!$result) {
|
|
|
+ throw new \Exception("扣除物品失败: ID {$detail['id']}");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case CHEST_COST_TYPE::CURRENCY->value:
|
|
|
+ // 扣除货币
|
|
|
+ $result = UserCurrencyService::deductCurrency(
|
|
|
+ $userId,
|
|
|
+ $detail['id'],
|
|
|
+ $detail['quantity'],
|
|
|
+ $reason
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!$result) {
|
|
|
+ throw new \Exception("扣除货币失败: ID {$detail['id']}");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case CHEST_COST_TYPE::OTHER_RESOURCE->value:
|
|
|
+ // 扣除其他资源,根据实际情况实现
|
|
|
+ // 这里仅作为示例,实际实现可能需要调用其他模块的服务
|
|
|
+ $deductSuccess = true; // 假设扣除成功
|
|
|
+
|
|
|
+ if (!$deductSuccess) {
|
|
|
+ throw new \Exception("扣除资源失败: ID {$detail['id']}");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return true;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ Log::error('扣除宝箱开启消耗失败: ' . $e->getMessage(), [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'cost_details' => $costDetails,
|
|
|
+ 'exception' => $e
|
|
|
+ ]);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建宝箱开启消耗配置
|
|
|
+ *
|
|
|
+ * @param ItemChestOpenCostDto $dto
|
|
|
+ * @return ItemChestOpenCost|null
|
|
|
+ */
|
|
|
+ public function createChestOpenCost(ItemChestOpenCostDto $dto): ?ItemChestOpenCost
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ return $this->costRepository->create([
|
|
|
+ 'chest_id' => $dto->chestId,
|
|
|
+ 'cost_type' => $dto->costType,
|
|
|
+ 'cost_id' => $dto->costId,
|
|
|
+ 'cost_quantity' => $dto->costQuantity,
|
|
|
+ 'is_active' => $dto->isActive,
|
|
|
+ ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('创建宝箱开启消耗配置失败: ' . $e->getMessage(), [
|
|
|
+ 'dto' => $dto->toArray(),
|
|
|
+ 'exception' => $e
|
|
|
+ ]);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新宝箱开启消耗配置
|
|
|
+ *
|
|
|
+ * @param int $id
|
|
|
+ * @param ItemChestOpenCostDto $dto
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function updateChestOpenCost(int $id, ItemChestOpenCostDto $dto): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $cost = $this->costRepository->find($id);
|
|
|
+
|
|
|
+ if (!$cost) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->costRepository->update($id, [
|
|
|
+ 'chest_id' => $dto->chestId,
|
|
|
+ 'cost_type' => $dto->costType,
|
|
|
+ 'cost_id' => $dto->costId,
|
|
|
+ 'cost_quantity' => $dto->costQuantity,
|
|
|
+ 'is_active' => $dto->isActive,
|
|
|
+ ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('更新宝箱开启消耗配置失败: ' . $e->getMessage(), [
|
|
|
+ 'id' => $id,
|
|
|
+ 'dto' => $dto->toArray(),
|
|
|
+ 'exception' => $e
|
|
|
+ ]);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除宝箱开启消耗配置
|
|
|
+ *
|
|
|
+ * @param int $id
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function deleteChestOpenCost(int $id): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ return $this->costRepository->delete($id);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('删除宝箱开启消耗配置失败: ' . $e->getMessage(), [
|
|
|
+ 'id' => $id,
|
|
|
+ 'exception' => $e
|
|
|
+ ]);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|