HouseService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Module\Farm\Services;
  3. use App\Module\Farm\Dtos\HouseRankDto;
  4. use App\Module\Farm\Logics\HouseLogic;
  5. use App\Module\Farm\Models\FarmHouseConfig;
  6. use Illuminate\Support\Facades\Log;
  7. use UCore\Exception\LogicException;
  8. use UCore\Exception\ValidateException;
  9. /**
  10. * 房屋管理服务
  11. */
  12. class HouseService
  13. {
  14. /**
  15. * 获取房屋配置
  16. *
  17. * @param int $level
  18. * @return FarmHouseConfig|null
  19. */
  20. public static function getHouseConfig(int $level): ?FarmHouseConfig
  21. {
  22. try {
  23. $houseLogic = new HouseLogic();
  24. return $houseLogic->getHouseConfig($level);
  25. } catch (\Exception $e) {
  26. Log::error('获取房屋配置失败', [
  27. 'level' => $level,
  28. 'error' => $e->getMessage(),
  29. 'trace' => $e->getTraceAsString()
  30. ]);
  31. return null;
  32. }
  33. }
  34. /**
  35. * 获取所有房屋配置
  36. *
  37. * @return array
  38. */
  39. public static function getAllHouseConfigs(): array
  40. {
  41. try {
  42. $houseLogic = new HouseLogic();
  43. return $houseLogic->getAllHouseConfigs();
  44. } catch (\Exception $e) {
  45. Log::error('获取所有房屋配置失败', [
  46. 'error' => $e->getMessage(),
  47. 'trace' => $e->getTraceAsString()
  48. ]);
  49. return [];
  50. }
  51. }
  52. /**
  53. * 获取下一级房屋配置
  54. *
  55. * @param int $currentLevel
  56. * @return FarmHouseConfig|null
  57. */
  58. public static function getNextLevelConfig(int $currentLevel): ?FarmHouseConfig
  59. {
  60. try {
  61. $houseLogic = new HouseLogic();
  62. return $houseLogic->getNextLevelConfig($currentLevel);
  63. } catch (\Exception $e) {
  64. Log::error('获取下一级房屋配置失败', [
  65. 'current_level' => $currentLevel,
  66. 'error' => $e->getMessage(),
  67. 'trace' => $e->getTraceAsString()
  68. ]);
  69. return null;
  70. }
  71. }
  72. /**
  73. * 检查房屋升级条件
  74. *
  75. * @param int $userId 用户ID
  76. * @return array 检查结果,包含success字段表示是否满足条件,message字段表示错误信息,以及其他详细信息
  77. */
  78. public static function checkUpgradeRequirements(int $userId): true
  79. {
  80. // 获取用户当前房屋等级
  81. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  82. if (!$farmUser) {
  83. throw new LogicException('用户农场不存在');
  84. }
  85. $currentLevel = $farmUser->house_level;
  86. $nextLevel = $currentLevel + 1;
  87. // 获取下一级房屋配置
  88. $nextLevelConfig = self::getHouseConfig($nextLevel);
  89. if (!$nextLevelConfig) {
  90. Log::error('获取下一级房屋配置失败', [
  91. 'current_level' => $currentLevel,
  92. 'error' => '下一级房屋配置不存在'
  93. ]);
  94. throw new LogicException('已达到最高等级');
  95. }
  96. // 获取升级所需消耗组
  97. $consumeGroupId = $nextLevelConfig->upgrade_materials;
  98. if (!$consumeGroupId) {
  99. throw new LogicException('升级条件组不存在');
  100. }
  101. // 检查消耗条件
  102. $checkResult = \App\Module\Game\Services\ConsumeService::checkConsume($userId, $consumeGroupId);
  103. if($checkResult->error){
  104. throw new LogicException($checkResult->message);
  105. }
  106. return true;
  107. }
  108. /**
  109. * 获取房屋排行榜数据
  110. *
  111. * @param int $userId 当前用户ID
  112. * @param int $page 页码
  113. * @param int $pageSize 每页数量
  114. * @return HouseRankDto
  115. */
  116. public static function getHouseRankList(int $userId, int $page = 1, int $pageSize = 20): HouseRankDto
  117. {
  118. try {
  119. $houseLogic = new HouseLogic();
  120. return $houseLogic->getHouseRankList($userId, $page, $pageSize);
  121. } catch (\Exception $e) {
  122. Log::error('获取房屋排行榜失败', [
  123. 'user_id' => $userId,
  124. 'page' => $page,
  125. 'page_size' => $pageSize,
  126. 'error' => $e->getMessage(),
  127. 'trace' => $e->getTraceAsString()
  128. ]);
  129. return new HouseRankDto();
  130. }
  131. }
  132. /**
  133. * 执行房屋升级
  134. * 要求调用者已开启事务
  135. *
  136. * @param int $userId 用户ID
  137. * @return bool 升级结果
  138. */
  139. public static function executeHouseUpgrade(int $userId): bool
  140. {
  141. // 验证事务是否已开启
  142. \UCore\Db\Helper::check_tr();
  143. // 获取用户当前房屋等级
  144. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  145. $currentLevel = $farmUser->house_level;
  146. $nextLevel = $currentLevel + 1;
  147. // 获取下一级房屋配置
  148. $nextLevelConfig = self::getHouseConfig($nextLevel);
  149. $consumeGroupId = $nextLevelConfig->upgrade_materials;
  150. // 执行消耗
  151. $consumeResult = \App\Module\Game\Services\ConsumeService::executeConsume($userId, $consumeGroupId, 'house_upgrade', $farmUser->id);
  152. if ($consumeResult->error) {
  153. throw new LogicException($consumeResult->message ?? '消耗材料失败');
  154. }
  155. // 构建消耗的材料数组
  156. $consumedItems = [];
  157. $consumeGroup = \App\Module\Game\Models\GameConsumeGroup::find($consumeGroupId);
  158. if ($consumeGroup && $consumeGroup->consumeItems) {
  159. foreach ($consumeGroup->consumeItems as $item) {
  160. if ($item->consume_type == \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value) {
  161. $consumedItems[] = [
  162. 'item_id' => $item->target_id,
  163. 'amount' => $item->quantity
  164. ];
  165. }
  166. }
  167. }
  168. // 执行升级
  169. $houseLogic = new HouseLogic();
  170. $upgradeResult = $houseLogic->upgradeHouse($userId, $consumedItems);
  171. if (!$upgradeResult) {
  172. throw new LogicException('升级房屋失败');
  173. }
  174. return true;
  175. }
  176. }