| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- <?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 $itemId 种子物品ID
- * @return array|null 返回包含CropInfoDto和日志ID的数组,格式为['crop' => CropInfoDto, 'log_id' => int]
- */
- public static function plantCrop(int $userId, int $landId, int $itemId): ?array
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->plantCrop($userId, $landId, $itemId);
- } catch (\Exception $e) {
- Log::error('种植作物失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'item_id' => $itemId,
- '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();
- }
- }
- /**
- * 使用化肥(通过作物ID)
- *
- * @param int $cropId 作物ID
- * @param int $cropGrowthTime 减少的生长时间(秒)
- * @return Res
- */
- public static function useFertilizer(int $cropId, int $cropGrowthTime): Res
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->useFertilizerByCropId($cropId, $cropGrowthTime);
- } catch (\Exception $e) {
- Log::error('使用化肥失败', [
- 'crop_id' => $cropId,
- 'crop_growth_time' => $cropGrowthTime,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return Res::error('使用化肥失败');
- }
- }
- /**
- * 使用化肥(通过土地ID,兼容旧接口)
- *
- * @param int $userId
- * @param int $landId
- * @param int $cropGrowthTime
- * @return Res
- * @deprecated 建议使用 useFertilizer($cropId, $cropGrowthTime) 方法
- */
- public static function useFertilizerByLandId(int $userId, int $landId, int $cropGrowthTime): Res
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->useFertilizer($userId, $landId, $cropGrowthTime);
- } catch (\Exception $e) {
- Log::error('使用化肥失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'crop_growth_time' => $cropGrowthTime,
- '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
- * @param int $toolItemId 使用的工具物品ID,0表示手动铲除
- * @return array 返回操作结果和状态变更信息
- */
- public static function removeCrop(int $userId, int $landId, int $toolItemId = 0): array
- {
- try {
- // 获取铲除前的土地状态
- $land = \App\Module\Farm\Models\FarmLand::where('id', $landId)
- ->where('user_id', $userId)
- ->first();
- if (!$land) {
- throw new \Exception('土地不存在');
- }
- $oldStatus = $land->status;
- $cropLogic = new CropLogic();
- $result = $cropLogic->removeCrop($userId, $landId, $toolItemId);
- if ($result) {
- // 获取铲除后的土地状态
- $land->refresh();
- $newStatus = $land->status;
- return [
- 'success' => true,
- 'status_changed' => $oldStatus !== $newStatus,
- 'old_status' => $oldStatus,
- 'new_status' => $newStatus
- ];
- }
- return ['success' => false];
- } catch (\Exception $e) {
- Log::error('铲除作物失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e; // 重新抛出异常,由调用方处理
- }
- }
- /**
- * 强制删除作物(物理删除,谨慎使用)
- *
- * 此方法会永久删除作物记录,主要用于:
- * 1. 数据清理和维护
- * 2. 测试环境的数据重置
- * 3. 特殊的管理员操作
- *
- * @param int $userId 用户ID
- * @param int $landId 土地ID
- * @param string $reason 删除原因
- * @return array 返回操作结果
- */
- public static function forceDeleteCrop(int $userId, int $landId, string $reason = '管理员操作'): array
- {
- try {
- $cropLogic = new CropLogic();
- $result = $cropLogic->forceDeleteCrop($userId, $landId, $reason);
- return [
- 'success' => $result,
- 'message' => $result ? '强制删除作物成功' : '强制删除作物失败'
- ];
- } catch (\Exception $e) {
- Log::error('强制删除作物失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'reason' => $reason,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e;
- }
- }
- /**
- * 恢复软删除的作物
- *
- * @param int $userId 用户ID
- * @param int $landId 土地ID
- * @return array 返回操作结果
- */
- public static function restoreCrop(int $userId, int $landId): array
- {
- try {
- $cropLogic = new CropLogic();
- $result = $cropLogic->restoreCrop($userId, $landId);
- return [
- 'success' => $result,
- 'message' => $result ? '恢复作物成功' : '恢复作物失败'
- ];
- } catch (\Exception $e) {
- Log::error('恢复作物失败', [
- 'user_id' => $userId,
- 'land_id' => $landId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e;
- }
- }
- /**
- * 获取软删除的作物信息
- *
- * @param int $landId 土地ID
- * @return CropInfoDto|null
- */
- public static function getTrashedCropByLandId(int $landId): ?CropInfoDto
- {
- try {
- $cropLogic = new CropLogic();
- return $cropLogic->getTrashedCropByLandId($landId);
- } catch (\Exception $e) {
- Log::error('获取软删除作物信息失败', [
- 'land_id' => $landId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return null;
- }
- }
- }
|