| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace App\Module\Farm\Services;
- use App\Module\Farm\Dtos\LandInfoDto;
- use App\Module\Farm\Logics\LandLogic;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmLandUpgradeConfig;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Log;
- /**
- * 土地管理服务
- */
- class LandService
- {
- /**
- * 获取用户的所有土地
- *
- * @param int $userId
- * @return Collection|LandInfoDto[]
- */
- public static function getUserLands(int $userId): Collection
- {
- try {
- $landLogic = new LandLogic();
- return $landLogic->getUserLands($userId);
- } catch (\Exception $e) {
- Log::error('获取用户土地失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return collect();
- }
- }
- /**
- * 根据用户id和土地id获取土地信息
- *
- * @param int $userId 用户ID
- * @param int $landId 土地ID
- * @return LandInfoDto|null
- */
- public static function getUserLand(int $userId, int $landId): ?LandInfoDto
- {
- try {
- $land = FarmLand::where('user_id', $userId)
- ->where('id', $landId)
- ->first();
- if (!$land) {
- return null;
- }
- return LandInfoDto::fromModel($land);
- } catch (\Exception $e) {
- Log::error('获取用户土地信息失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return null;
- }
- }
- /**
- * 获取用户指定位置的土地
- *
- * @param int $userId
- * @param int $position
- * @return LandInfoDto|null
- */
- public static function getUserLandByPosition(int $userId, int $position): ?LandInfoDto
- {
- try {
- $landLogic = new LandLogic();
- return $landLogic->getUserLandByPosition($userId, $position);
- } catch (\Exception $e) {
- Log::error('获取用户指定位置土地失败', [
- 'user_id' => $userId,
- 'position' => $position,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return null;
- }
- }
- /**
- * 升级土地
- *
- * @param int $userId
- * @param int $landId
- * @param int $targetType
- * @return bool
- */
- public static function upgradeLand(int $userId, int $landId, int $targetType ): bool
- {
- try {
- $landLogic = new LandLogic();
- return $landLogic->upgradeLand($userId, $landId, $targetType);
- } catch (\Exception $e) {
- Log::error('升级土地失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'target_type' => $targetType,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return false;
- }
- }
- /**
- * 获取可用的升级路径
- *
- * @param int $userId 用户ID
- * @param int $landId 土地ID
- * @param int|null $toType 目标土地类型ID(可选)
- * @return FarmLandUpgradeConfig[]
- */
- public static function getAvailableUpgradePaths(int $userId, int $landId, ?int $toType = null): array
- {
- try {
- $landLogic = new LandLogic();
- return $landLogic->getAvailableUpgradePaths($userId, $landId, $toType);
- } catch (\Exception $e) {
- Log::error('获取可用升级路径失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'to_type' => $toType,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return [];
- }
- }
- }
|