| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- namespace App\Module\Farm\Services;
- use App\Module\Farm\Dtos\CropInfoDto;
- use App\Module\Farm\Dtos\HarvestResultDto;
- use App\Module\Farm\Logics\CropLogic;
- use Illuminate\Support\Facades\Log;
- use UCore\Dto\Res;
- /**
- * 作物管理服务
- */
- class CropService
- {
- /**
- * 获取作物信息
- *
- * @param int $cropId
- * @return CropInfoDto|null
- */
- public static function getCropInfo(int $cropId): ?CropInfoDto
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->getCropInfo($cropId);
- } catch (\Exception $e) {
- Log::error('获取作物信息失败', [
- 'crop_id' => $cropId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return null;
- }
- }
- /**
- * 获取土地上的作物信息
- *
- * @param int $landId
- * @return CropInfoDto|null
- */
- public static function getCropByLandId(int $landId): ?CropInfoDto
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->getCropByLandId($landId);
- } catch (\Exception $e) {
- Log::error('获取土地作物信息失败', [
- 'land_id' => $landId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return null;
- }
- }
- /**
- * 种植作物
- *
- * @param int $userId
- * @param int $landId
- * @param int $seedId
- * @return array|null 返回包含CropInfoDto和日志ID的数组,格式为['crop' => CropInfoDto, 'log_id' => int]
- */
- public static function plantCrop(int $userId, int $landId, int $seedId): ?array
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->plantCrop($userId, $landId, $seedId);
- } catch (\Exception $e) {
- Log::error('种植作物失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'seed_id' => $seedId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return null;
- }
- }
- /**
- * 收获作物
- *
- * @param int $userId
- * @param int $landId
- * @return HarvestResultDto|null
- */
- public static function harvestCrop(int $userId, int $landId): Res
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->harvestCrop($userId, $landId);
- } catch (\Exception $e) {
- Log::error('收获作物失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return Res::error();
- }
- }
- /**
- * 使用化肥
- *
- * @param int $userId
- * @param int $landId
- * @return Res
- */
- public static function useFertilizer(int $userId, int $landId,int $crop_growth_time): Res
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->useFertilizer($userId, $landId,$crop_growth_time);
- } catch (\Exception $e) {
- Log::error('使用化肥失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return Res::error('');
- }
- }
- /**
- * 更新作物生长阶段
- *
- * @param int $cropid
- * @return bool
- */
- public static function updateGrowthStage(int $cropid): bool
- {
- // 检查施肥后是否需要更新生长阶段
- $cropLogic = new CropLogic();
- return $cropLogic->updateGrowthStage($cropid);
- }
- /**
- * 清理灾害
- *
- * @param int $userId
- * @param int $landId
- * @param int $disasterType
- * @return bool
- */
- public static function clearDisaster(int $userId, int $landId, int $disasterType): bool
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->clearDisaster($userId, $landId, $disasterType);
- } catch (\Exception $e) {
- Log::error('清理灾害失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'disaster_type' => $disasterType,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return false;
- }
- }
- /**
- * 使用物品去除灾害(带概率判断)
- *
- * @param int $userId 用户ID
- * @param int $landId 土地ID
- * @param int $itemId 物品ID
- * @param int $disasterType 灾害类型
- * @param string $sourceType 消耗来源类型
- * @return array 操作结果
- * @throws \Exception
- */
- public static function removeDisasterWithItem(int $userId, int $landId, int $itemId, int $disasterType, string $sourceType): array
- {
- try {
- $disasterRemovalLogic = new \App\Module\Farm\Logics\DisasterRemovalLogic();
- return $disasterRemovalLogic->removeDisaster($userId, $landId, $itemId, $disasterType, $sourceType);
- } catch (\Exception $e) {
- Log::error('使用物品去除灾害失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'item_id' => $itemId,
- 'disaster_type' => $disasterType,
- 'source_type' => $sourceType,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e;
- }
- }
- /**
- * 使用物品去除灾害(带验证和概率判断)
- *
- * @param int $userId 用户ID
- * @param int $landId 土地ID
- * @param int $itemId 物品ID
- * @param int $disasterType 灾害类型
- * @param string $sourceType 消耗来源类型
- * @return array 操作结果
- * @throws \Exception
- */
- public static function removeDisasterWithValidation(int $userId, int $landId, int $itemId, int $disasterType, string $sourceType): array
- {
- try {
- // 使用Validation进行数据验证
- $validation = new \App\Module\Farm\Validations\DisasterRemovalValidation([
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'item_id' => $itemId,
- 'disaster_type' => $disasterType
- ]);
- // 验证数据
- $validation->validated();
- // 验证通过后,执行业务逻辑
- $disasterRemovalLogic = new \App\Module\Farm\Logics\DisasterRemovalLogic();
- return $disasterRemovalLogic->removeDisaster($userId, $landId, $itemId, $disasterType, $sourceType);
- } catch (\Exception $e) {
- Log::error('使用物品去除灾害失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'item_id' => $itemId,
- 'disaster_type' => $disasterType,
- 'source_type' => $sourceType,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e;
- }
- }
- /**
- * 铲除作物
- *
- * @param int $userId
- * @param int $landId
- * @return bool
- */
- public static function removeCrop(int $userId, int $landId): bool
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->removeCrop($userId, $landId);
- } catch (\Exception $e) {
- Log::error('铲除作物失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return false;
- }
- }
- }
|