SeedLogic.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Module\Farm\Logics;
  3. use App\Module\Farm\Models\FarmSeed;
  4. use App\Module\Farm\Models\FarmSeedOutput;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 种子管理逻辑
  10. */
  11. class SeedLogic
  12. {
  13. /**
  14. * 缓存键前缀
  15. *
  16. * @var string
  17. */
  18. private $cachePrefix = 'farm:seeds:';
  19. /**
  20. * 缓存过期时间(秒)
  21. *
  22. * @var int
  23. */
  24. private $cacheExpiration = 86400; // 24小时
  25. /**
  26. * 获取所有种子
  27. *
  28. * @return Collection
  29. */
  30. public function getAllSeeds(): Collection
  31. {
  32. // 尝试从缓存获取
  33. $cacheKey = $this->cachePrefix . 'all';
  34. $cachedSeeds = Cache::get($cacheKey);
  35. if ($cachedSeeds) {
  36. return $cachedSeeds;
  37. }
  38. // 从数据库获取
  39. $seeds = FarmSeed::with('outputs')->get();
  40. // 缓存结果
  41. Cache::put($cacheKey, $seeds, $this->cacheExpiration);
  42. return $seeds;
  43. }
  44. /**
  45. * 获取指定类型的种子
  46. *
  47. * @param int $type
  48. * @return Collection
  49. */
  50. public function getSeedsByType(int $type): Collection
  51. {
  52. try {
  53. // 尝试从缓存获取
  54. $cacheKey = $this->cachePrefix . 'type:' . $type;
  55. $cachedSeeds = Cache::get($cacheKey);
  56. if ($cachedSeeds) {
  57. return $cachedSeeds;
  58. }
  59. // 从数据库获取
  60. $seeds = FarmSeed::where('type', $type)
  61. ->with('outputs')
  62. ->get();
  63. // 缓存结果
  64. Cache::put($cacheKey, $seeds, $this->cacheExpiration);
  65. return $seeds;
  66. } catch (\Exception $e) {
  67. Log::error('获取指定类型种子失败', [
  68. 'type' => $type,
  69. 'error' => $e->getMessage(),
  70. 'trace' => $e->getTraceAsString()
  71. ]);
  72. throw $e; // 重新抛出异常,不隐藏错误
  73. }
  74. }
  75. /**
  76. * 获取种子详情
  77. *
  78. * @param int $seedId
  79. * @return FarmSeed|null
  80. */
  81. public function getSeedDetail(int $seedId): ?FarmSeed
  82. {
  83. try {
  84. // 尝试从缓存获取
  85. $cacheKey = $this->cachePrefix . 'id:' . $seedId;
  86. $cachedSeed = Cache::get($cacheKey);
  87. if ($cachedSeed) {
  88. return $cachedSeed;
  89. }
  90. // 从数据库获取
  91. $seed = FarmSeed::with('outputs')->find($seedId);
  92. if (!$seed) {
  93. return null;
  94. }
  95. // 缓存结果
  96. Cache::put($cacheKey, $seed, $this->cacheExpiration);
  97. return $seed;
  98. } catch (\Exception $e) {
  99. Log::error('获取种子详情失败', [
  100. 'seed_id' => $seedId,
  101. 'error' => $e->getMessage(),
  102. 'trace' => $e->getTraceAsString()
  103. ]);
  104. throw $e; // 重新抛出异常,不隐藏错误
  105. }
  106. }
  107. /**
  108. * 获取种子的产出配置
  109. *
  110. * @param int $seedId
  111. * @return Collection
  112. */
  113. public function getSeedOutputs(int $seedId): Collection
  114. {
  115. try {
  116. // 尝试从缓存获取
  117. $cacheKey = $this->cachePrefix . 'outputs:' . $seedId;
  118. $cachedOutputs = Cache::get($cacheKey);
  119. if ($cachedOutputs) {
  120. return $cachedOutputs;
  121. }
  122. // 从数据库获取
  123. $outputs = FarmSeedOutput::where('seed_id', $seedId)
  124. ->orderByDesc('probability')
  125. ->get();
  126. // 缓存结果
  127. Cache::put($cacheKey, $outputs, $this->cacheExpiration);
  128. return $outputs;
  129. } catch (\Exception $e) {
  130. Log::error('获取种子产出配置失败', [
  131. 'seed_id' => $seedId,
  132. 'error' => $e->getMessage(),
  133. 'trace' => $e->getTraceAsString()
  134. ]);
  135. throw $e; // 重新抛出异常,不隐藏错误
  136. }
  137. }
  138. /**
  139. * 根据物品ID获取种子
  140. *
  141. * @param int $itemId
  142. * @return FarmSeed|null
  143. */
  144. public function getSeedByItemId(int $itemId): ?FarmSeed
  145. {
  146. try {
  147. // 尝试从缓存获取
  148. $cacheKey = $this->cachePrefix . 'item:' . $itemId;
  149. $cachedSeed = Cache::get($cacheKey);
  150. if ($cachedSeed) {
  151. return $cachedSeed;
  152. }
  153. // 从数据库获取
  154. $seed = FarmSeed::where('item_id', $itemId)->first();
  155. if (!$seed) {
  156. return null;
  157. }
  158. // 缓存结果
  159. Cache::put($cacheKey, $seed, $this->cacheExpiration);
  160. return $seed;
  161. } catch (\Exception $e) {
  162. Log::error('根据物品ID获取种子失败', [
  163. 'item_id' => $itemId,
  164. 'error' => $e->getMessage(),
  165. 'trace' => $e->getTraceAsString()
  166. ]);
  167. throw $e; // 重新抛出异常,不隐藏错误
  168. }
  169. }
  170. /**
  171. * 清除种子缓存
  172. *
  173. * @param int|null $seedId
  174. * @return bool
  175. */
  176. public function clearSeedCache(?int $seedId = null): bool
  177. {
  178. try {
  179. if ($seedId) {
  180. // 清除指定种子的缓存
  181. Cache::forget($this->cachePrefix . 'id:' . $seedId);
  182. Cache::forget($this->cachePrefix . 'outputs:' . $seedId);
  183. // 获取种子信息
  184. $seed = FarmSeed::find($seedId);
  185. if ($seed) {
  186. // 清除种子类型缓存
  187. Cache::forget($this->cachePrefix . 'type:' . $seed->type);
  188. // 清除物品ID缓存
  189. Cache::forget($this->cachePrefix . 'item:' . $seed->item_id);
  190. }
  191. } else {
  192. // 清除所有种子缓存
  193. Cache::forget($this->cachePrefix . 'all');
  194. // 清除所有类型缓存
  195. for ($type = 1; $type <= 3; $type++) {
  196. Cache::forget($this->cachePrefix . 'type:' . $type);
  197. }
  198. }
  199. return true;
  200. } catch (\Exception $e) {
  201. Log::error('清除种子缓存失败', [
  202. 'seed_id' => $seedId,
  203. 'error' => $e->getMessage(),
  204. 'trace' => $e->getTraceAsString()
  205. ]);
  206. throw $e; // 重新抛出异常,不隐藏错误
  207. }
  208. }
  209. }