ChestOpenCostLogic.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace App\Module\GameItems\Logics;
  3. use App\Module\GameItems\Dtos\ItemChestOpenCostDto;
  4. use App\Module\GameItems\Enums\CHEST_COST_TYPE;
  5. use App\Module\GameItems\Models\ItemChestOpenCost;
  6. use App\Module\GameItems\Repositorys\ItemChestOpenCostRepository;
  7. use App\Module\User\Services\UserCurrencyService;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. /**
  11. * 宝箱开启消耗逻辑类
  12. */
  13. class ChestOpenCostLogic
  14. {
  15. /**
  16. * @var ItemChestOpenCostRepository
  17. */
  18. protected $costRepository;
  19. /**
  20. * @var ItemLogic
  21. */
  22. protected $itemLogic;
  23. /**
  24. * 构造函数
  25. *
  26. * @param ItemChestOpenCostRepository $costRepository
  27. * @param ItemLogic $itemLogic
  28. */
  29. public function __construct(
  30. ItemChestOpenCostRepository $costRepository,
  31. ItemLogic $itemLogic
  32. ) {
  33. $this->costRepository = $costRepository;
  34. $this->itemLogic = $itemLogic;
  35. }
  36. /**
  37. * 获取宝箱的所有激活消耗配置
  38. *
  39. * @param int $chestId 宝箱ID
  40. * @return ItemChestOpenCostDto[]
  41. */
  42. public function getActiveChestCosts(int $chestId): array
  43. {
  44. $costs = $this->costRepository->getActiveByChestId($chestId);
  45. $result = [];
  46. foreach ($costs as $cost) {
  47. $result[] = ItemChestOpenCostDto::fromModel($cost, true);
  48. }
  49. return $result;
  50. }
  51. /**
  52. * 验证用户是否有足够的资源支付宝箱开启消耗
  53. *
  54. * @param int $userId 用户ID
  55. * @param int $chestId 宝箱ID
  56. * @param int $quantity 开启数量
  57. * @return array [bool $isValid, string $message, array $costs]
  58. */
  59. public function validateChestOpenCosts(int $userId, int $chestId, int $quantity = 1): array
  60. {
  61. $costs = $this->costRepository->getActiveByChestId($chestId);
  62. if ($costs->isEmpty()) {
  63. return [true, '无需额外消耗', []];
  64. }
  65. $costDetails = [];
  66. foreach ($costs as $cost) {
  67. $totalQuantity = $cost->cost_quantity * $quantity;
  68. $costDetails[] = [
  69. 'type' => $cost->cost_type,
  70. 'id' => $cost->cost_id,
  71. 'quantity' => $totalQuantity,
  72. 'model' => $cost
  73. ];
  74. // 验证用户是否有足够的资源
  75. switch ($cost->cost_type) {
  76. case CHEST_COST_TYPE::ITEM->value:
  77. // 验证物品数量
  78. $userItem = $this->itemLogic->getUserItem($userId, $cost->cost_id);
  79. if (!$userItem || $userItem->quantity < $totalQuantity) {
  80. return [
  81. false,
  82. "物品不足,需要 {$totalQuantity} 个 " . ($cost->costItem->name ?? "ID:{$cost->cost_id}"),
  83. $costDetails
  84. ];
  85. }
  86. break;
  87. case CHEST_COST_TYPE::CURRENCY->value:
  88. // 验证货币数量
  89. $userCurrency = UserCurrencyService::getUserCurrency($userId, $cost->cost_id);
  90. if ($userCurrency < $totalQuantity) {
  91. return [
  92. false,
  93. "货币不足,需要 {$totalQuantity} 个 ID:{$cost->cost_id}",
  94. $costDetails
  95. ];
  96. }
  97. break;
  98. case CHEST_COST_TYPE::OTHER_RESOURCE->value:
  99. // 验证其他资源,根据实际情况实现
  100. // 这里仅作为示例,实际实现可能需要调用其他模块的服务
  101. $hasEnoughResource = true; // 假设有足够资源
  102. if (!$hasEnoughResource) {
  103. return [
  104. false,
  105. "资源不足,需要 {$totalQuantity} 个 ID:{$cost->cost_id}",
  106. $costDetails
  107. ];
  108. }
  109. break;
  110. }
  111. }
  112. return [true, '验证通过', $costDetails];
  113. }
  114. /**
  115. * 扣除宝箱开启所需的消耗资源
  116. *
  117. * @param int $userId 用户ID
  118. * @param array $costDetails 消耗详情数组
  119. * @param string $reason 消耗原因
  120. * @return bool 是否成功扣除
  121. */
  122. public function deductChestOpenCosts(int $userId, array $costDetails, string $reason = '开启宝箱'): bool
  123. {
  124. try {
  125. DB::beginTransaction();
  126. foreach ($costDetails as $detail) {
  127. switch ($detail['type']) {
  128. case CHEST_COST_TYPE::ITEM->value:
  129. // 扣除物品
  130. $result = $this->itemLogic->consumeUserItem(
  131. $userId,
  132. $detail['id'],
  133. $detail['quantity'],
  134. $reason
  135. );
  136. if (!$result) {
  137. throw new \Exception("扣除物品失败: ID {$detail['id']}");
  138. }
  139. break;
  140. case CHEST_COST_TYPE::CURRENCY->value:
  141. // 扣除货币
  142. $result = UserCurrencyService::deductCurrency(
  143. $userId,
  144. $detail['id'],
  145. $detail['quantity'],
  146. $reason
  147. );
  148. if (!$result) {
  149. throw new \Exception("扣除货币失败: ID {$detail['id']}");
  150. }
  151. break;
  152. case CHEST_COST_TYPE::OTHER_RESOURCE->value:
  153. // 扣除其他资源,根据实际情况实现
  154. // 这里仅作为示例,实际实现可能需要调用其他模块的服务
  155. $deductSuccess = true; // 假设扣除成功
  156. if (!$deductSuccess) {
  157. throw new \Exception("扣除资源失败: ID {$detail['id']}");
  158. }
  159. break;
  160. }
  161. }
  162. DB::commit();
  163. return true;
  164. } catch (\Exception $e) {
  165. DB::rollBack();
  166. Log::error('扣除宝箱开启消耗失败: ' . $e->getMessage(), [
  167. 'user_id' => $userId,
  168. 'cost_details' => $costDetails,
  169. 'exception' => $e
  170. ]);
  171. return false;
  172. }
  173. }
  174. /**
  175. * 创建宝箱开启消耗配置
  176. *
  177. * @param ItemChestOpenCostDto $dto
  178. * @return ItemChestOpenCost|null
  179. */
  180. public function createChestOpenCost(ItemChestOpenCostDto $dto): ?ItemChestOpenCost
  181. {
  182. try {
  183. return $this->costRepository->create([
  184. 'chest_id' => $dto->chestId,
  185. 'cost_type' => $dto->costType,
  186. 'cost_id' => $dto->costId,
  187. 'cost_quantity' => $dto->costQuantity,
  188. 'is_active' => $dto->isActive,
  189. ]);
  190. } catch (\Exception $e) {
  191. Log::error('创建宝箱开启消耗配置失败: ' . $e->getMessage(), [
  192. 'dto' => $dto->toArray(),
  193. 'exception' => $e
  194. ]);
  195. return null;
  196. }
  197. }
  198. /**
  199. * 更新宝箱开启消耗配置
  200. *
  201. * @param int $id
  202. * @param ItemChestOpenCostDto $dto
  203. * @return bool
  204. */
  205. public function updateChestOpenCost(int $id, ItemChestOpenCostDto $dto): bool
  206. {
  207. try {
  208. $cost = $this->costRepository->find($id);
  209. if (!$cost) {
  210. return false;
  211. }
  212. return $this->costRepository->update($id, [
  213. 'chest_id' => $dto->chestId,
  214. 'cost_type' => $dto->costType,
  215. 'cost_id' => $dto->costId,
  216. 'cost_quantity' => $dto->costQuantity,
  217. 'is_active' => $dto->isActive,
  218. ]);
  219. } catch (\Exception $e) {
  220. Log::error('更新宝箱开启消耗配置失败: ' . $e->getMessage(), [
  221. 'id' => $id,
  222. 'dto' => $dto->toArray(),
  223. 'exception' => $e
  224. ]);
  225. return false;
  226. }
  227. }
  228. /**
  229. * 删除宝箱开启消耗配置
  230. *
  231. * @param int $id
  232. * @return bool
  233. */
  234. public function deleteChestOpenCost(int $id): bool
  235. {
  236. try {
  237. return $this->costRepository->delete($id);
  238. } catch (\Exception $e) {
  239. Log::error('删除宝箱开启消耗配置失败: ' . $e->getMessage(), [
  240. 'id' => $id,
  241. 'exception' => $e
  242. ]);
  243. return false;
  244. }
  245. }
  246. }