| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace App\Module\Farm\Logics;
- use App\Module\Farm\Models\FarmSeed;
- use App\Module\Farm\Models\FarmSeedOutput;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- /**
- * 种子管理逻辑
- */
- class SeedLogic
- {
- /**
- * 缓存键前缀
- *
- * @var string
- */
- private $cachePrefix = 'farm:seeds:';
-
- /**
- * 缓存过期时间(秒)
- *
- * @var int
- */
- private $cacheExpiration = 86400; // 24小时
-
- /**
- * 获取所有种子
- *
- * @return Collection
- */
- public function getAllSeeds(): Collection
- {
- try {
- // 尝试从缓存获取
- $cacheKey = $this->cachePrefix . 'all';
- $cachedSeeds = Cache::get($cacheKey);
-
- if ($cachedSeeds) {
- return $cachedSeeds;
- }
-
- // 从数据库获取
- $seeds = FarmSeed::with('outputs')->get();
-
- // 缓存结果
- Cache::put($cacheKey, $seeds, $this->cacheExpiration);
-
- return $seeds;
- } catch (\Exception $e) {
- Log::error('获取所有种子失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return collect();
- }
- }
-
- /**
- * 获取指定类型的种子
- *
- * @param int $type
- * @return Collection
- */
- public function getSeedsByType(int $type): Collection
- {
- try {
- // 尝试从缓存获取
- $cacheKey = $this->cachePrefix . 'type:' . $type;
- $cachedSeeds = Cache::get($cacheKey);
-
- if ($cachedSeeds) {
- return $cachedSeeds;
- }
-
- // 从数据库获取
- $seeds = FarmSeed::where('type', $type)
- ->with('outputs')
- ->get();
-
- // 缓存结果
- Cache::put($cacheKey, $seeds, $this->cacheExpiration);
-
- return $seeds;
- } catch (\Exception $e) {
- Log::error('获取指定类型种子失败', [
- 'type' => $type,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return collect();
- }
- }
-
- /**
- * 获取种子详情
- *
- * @param int $seedId
- * @return FarmSeed|null
- */
- public function getSeedDetail(int $seedId): ?FarmSeed
- {
- try {
- // 尝试从缓存获取
- $cacheKey = $this->cachePrefix . 'id:' . $seedId;
- $cachedSeed = Cache::get($cacheKey);
-
- if ($cachedSeed) {
- return $cachedSeed;
- }
-
- // 从数据库获取
- $seed = FarmSeed::with('outputs')->find($seedId);
-
- if (!$seed) {
- return null;
- }
-
- // 缓存结果
- Cache::put($cacheKey, $seed, $this->cacheExpiration);
-
- return $seed;
- } catch (\Exception $e) {
- Log::error('获取种子详情失败', [
- 'seed_id' => $seedId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return null;
- }
- }
-
- /**
- * 获取种子的产出配置
- *
- * @param int $seedId
- * @return Collection
- */
- public function getSeedOutputs(int $seedId): Collection
- {
- try {
- // 尝试从缓存获取
- $cacheKey = $this->cachePrefix . 'outputs:' . $seedId;
- $cachedOutputs = Cache::get($cacheKey);
-
- if ($cachedOutputs) {
- return $cachedOutputs;
- }
-
- // 从数据库获取
- $outputs = FarmSeedOutput::where('seed_id', $seedId)
- ->orderByDesc('probability')
- ->get();
-
- // 缓存结果
- Cache::put($cacheKey, $outputs, $this->cacheExpiration);
-
- return $outputs;
- } catch (\Exception $e) {
- Log::error('获取种子产出配置失败', [
- 'seed_id' => $seedId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return collect();
- }
- }
-
- /**
- * 根据物品ID获取种子
- *
- * @param int $itemId
- * @return FarmSeed|null
- */
- public function getSeedByItemId(int $itemId): ?FarmSeed
- {
- try {
- // 尝试从缓存获取
- $cacheKey = $this->cachePrefix . 'item:' . $itemId;
- $cachedSeed = Cache::get($cacheKey);
-
- if ($cachedSeed) {
- return $cachedSeed;
- }
-
- // 从数据库获取
- $seed = FarmSeed::where('item_id', $itemId)->first();
-
- if (!$seed) {
- return null;
- }
-
- // 缓存结果
- Cache::put($cacheKey, $seed, $this->cacheExpiration);
-
- return $seed;
- } catch (\Exception $e) {
- Log::error('根据物品ID获取种子失败', [
- 'item_id' => $itemId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return null;
- }
- }
-
- /**
- * 清除种子缓存
- *
- * @param int|null $seedId
- * @return bool
- */
- public function clearSeedCache(?int $seedId = null): bool
- {
- try {
- if ($seedId) {
- // 清除指定种子的缓存
- Cache::forget($this->cachePrefix . 'id:' . $seedId);
- Cache::forget($this->cachePrefix . 'outputs:' . $seedId);
-
- // 获取种子信息
- $seed = FarmSeed::find($seedId);
-
- if ($seed) {
- // 清除种子类型缓存
- Cache::forget($this->cachePrefix . 'type:' . $seed->type);
-
- // 清除物品ID缓存
- Cache::forget($this->cachePrefix . 'item:' . $seed->item_id);
- }
- } else {
- // 清除所有种子缓存
- Cache::forget($this->cachePrefix . 'all');
-
- // 清除所有类型缓存
- for ($type = 1; $type <= 3; $type++) {
- Cache::forget($this->cachePrefix . 'type:' . $type);
- }
- }
-
- return true;
- } catch (\Exception $e) {
- Log::error('清除种子缓存失败', [
- 'seed_id' => $seedId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
-
- return false;
- }
- }
- }
|