| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?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;
- /**
- * 作物管理服务
- */
- 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 CropInfoDto|null
- */
- public static function plantCrop(int $userId, int $landId, int $seedId): ?CropInfoDto
- {
- 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): ?HarvestResultDto
- {
- 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 null;
- }
- }
- /**
- * 使用化肥
- *
- * @param int $userId
- * @param int $landId
- * @return bool
- */
- public static function useFertilizer(int $userId, int $landId,int $crop_growth_time): bool
- {
- 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 false;
- }
- }
- /**
- * 更新作物生长阶段
- *
- * @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
- * @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;
- }
- }
- }
|