SeedLogic.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. try {
  33. // 尝试从缓存获取
  34. $cacheKey = $this->cachePrefix . 'all';
  35. $cachedSeeds = Cache::get($cacheKey);
  36. if ($cachedSeeds) {
  37. return $cachedSeeds;
  38. }
  39. // 从数据库获取
  40. $seeds = FarmSeed::with('outputs')->get();
  41. // 缓存结果
  42. Cache::put($cacheKey, $seeds, $this->cacheExpiration);
  43. return $seeds;
  44. } catch (\Exception $e) {
  45. Log::error('获取所有种子失败', [
  46. 'error' => $e->getMessage(),
  47. 'trace' => $e->getTraceAsString()
  48. ]);
  49. return collect();
  50. }
  51. }
  52. /**
  53. * 获取指定类型的种子
  54. *
  55. * @param int $type
  56. * @return Collection
  57. */
  58. public function getSeedsByType(int $type): Collection
  59. {
  60. try {
  61. // 尝试从缓存获取
  62. $cacheKey = $this->cachePrefix . 'type:' . $type;
  63. $cachedSeeds = Cache::get($cacheKey);
  64. if ($cachedSeeds) {
  65. return $cachedSeeds;
  66. }
  67. // 从数据库获取
  68. $seeds = FarmSeed::where('type', $type)
  69. ->with('outputs')
  70. ->get();
  71. // 缓存结果
  72. Cache::put($cacheKey, $seeds, $this->cacheExpiration);
  73. return $seeds;
  74. } catch (\Exception $e) {
  75. Log::error('获取指定类型种子失败', [
  76. 'type' => $type,
  77. 'error' => $e->getMessage(),
  78. 'trace' => $e->getTraceAsString()
  79. ]);
  80. return collect();
  81. }
  82. }
  83. /**
  84. * 获取种子详情
  85. *
  86. * @param int $seedId
  87. * @return FarmSeed|null
  88. */
  89. public function getSeedDetail(int $seedId): ?FarmSeed
  90. {
  91. try {
  92. // 尝试从缓存获取
  93. $cacheKey = $this->cachePrefix . 'id:' . $seedId;
  94. $cachedSeed = Cache::get($cacheKey);
  95. if ($cachedSeed) {
  96. return $cachedSeed;
  97. }
  98. // 从数据库获取
  99. $seed = FarmSeed::with('outputs')->find($seedId);
  100. if (!$seed) {
  101. return null;
  102. }
  103. // 缓存结果
  104. Cache::put($cacheKey, $seed, $this->cacheExpiration);
  105. return $seed;
  106. } catch (\Exception $e) {
  107. Log::error('获取种子详情失败', [
  108. 'seed_id' => $seedId,
  109. 'error' => $e->getMessage(),
  110. 'trace' => $e->getTraceAsString()
  111. ]);
  112. return null;
  113. }
  114. }
  115. /**
  116. * 获取种子的产出配置
  117. *
  118. * @param int $seedId
  119. * @return Collection
  120. */
  121. public function getSeedOutputs(int $seedId): Collection
  122. {
  123. try {
  124. // 尝试从缓存获取
  125. $cacheKey = $this->cachePrefix . 'outputs:' . $seedId;
  126. $cachedOutputs = Cache::get($cacheKey);
  127. if ($cachedOutputs) {
  128. return $cachedOutputs;
  129. }
  130. // 从数据库获取
  131. $outputs = FarmSeedOutput::where('seed_id', $seedId)
  132. ->orderByDesc('probability')
  133. ->get();
  134. // 缓存结果
  135. Cache::put($cacheKey, $outputs, $this->cacheExpiration);
  136. return $outputs;
  137. } catch (\Exception $e) {
  138. Log::error('获取种子产出配置失败', [
  139. 'seed_id' => $seedId,
  140. 'error' => $e->getMessage(),
  141. 'trace' => $e->getTraceAsString()
  142. ]);
  143. return collect();
  144. }
  145. }
  146. /**
  147. * 根据物品ID获取种子
  148. *
  149. * @param int $itemId
  150. * @return FarmSeed|null
  151. */
  152. public function getSeedByItemId(int $itemId): ?FarmSeed
  153. {
  154. try {
  155. // 尝试从缓存获取
  156. $cacheKey = $this->cachePrefix . 'item:' . $itemId;
  157. $cachedSeed = Cache::get($cacheKey);
  158. if ($cachedSeed) {
  159. return $cachedSeed;
  160. }
  161. // 从数据库获取
  162. $seed = FarmSeed::where('item_id', $itemId)->first();
  163. if (!$seed) {
  164. return null;
  165. }
  166. // 缓存结果
  167. Cache::put($cacheKey, $seed, $this->cacheExpiration);
  168. return $seed;
  169. } catch (\Exception $e) {
  170. Log::error('根据物品ID获取种子失败', [
  171. 'item_id' => $itemId,
  172. 'error' => $e->getMessage(),
  173. 'trace' => $e->getTraceAsString()
  174. ]);
  175. return null;
  176. }
  177. }
  178. /**
  179. * 清除种子缓存
  180. *
  181. * @param int|null $seedId
  182. * @return bool
  183. */
  184. public function clearSeedCache(?int $seedId = null): bool
  185. {
  186. try {
  187. if ($seedId) {
  188. // 清除指定种子的缓存
  189. Cache::forget($this->cachePrefix . 'id:' . $seedId);
  190. Cache::forget($this->cachePrefix . 'outputs:' . $seedId);
  191. // 获取种子信息
  192. $seed = FarmSeed::find($seedId);
  193. if ($seed) {
  194. // 清除种子类型缓存
  195. Cache::forget($this->cachePrefix . 'type:' . $seed->type);
  196. // 清除物品ID缓存
  197. Cache::forget($this->cachePrefix . 'item:' . $seed->item_id);
  198. }
  199. } else {
  200. // 清除所有种子缓存
  201. Cache::forget($this->cachePrefix . 'all');
  202. // 清除所有类型缓存
  203. for ($type = 1; $type <= 3; $type++) {
  204. Cache::forget($this->cachePrefix . 'type:' . $type);
  205. }
  206. }
  207. return true;
  208. } catch (\Exception $e) {
  209. Log::error('清除种子缓存失败', [
  210. 'seed_id' => $seedId,
  211. 'error' => $e->getMessage(),
  212. 'trace' => $e->getTraceAsString()
  213. ]);
  214. return false;
  215. }
  216. }
  217. }