FarmConfigLogic.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Module\Farm\Logics;
  3. use App\Module\Farm\Models\FarmConfig;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 农场配置逻辑类
  8. */
  9. class FarmConfigLogic
  10. {
  11. /**
  12. * 缓存前缀
  13. *
  14. * @var string
  15. */
  16. private $cachePrefix = 'farm_config:';
  17. /**
  18. * 缓存时间(秒)
  19. *
  20. * @var int
  21. */
  22. private $cacheTime = 3600; // 1小时
  23. /**
  24. * 获取配置值
  25. *
  26. * @param string $configKey 配置键
  27. * @param mixed $defaultValue 默认值
  28. * @return mixed
  29. */
  30. public function getConfigValue(string $configKey, $defaultValue = null)
  31. {
  32. try {
  33. // 尝试从缓存获取
  34. $cacheKey = $this->cachePrefix . $configKey;
  35. $cachedValue = Cache::get($cacheKey);
  36. if ($cachedValue !== null) {
  37. return $cachedValue;
  38. }
  39. // 从数据库获取
  40. $config = FarmConfig::where('config_key', $configKey)
  41. ->where('is_active', true)
  42. ->first();
  43. if (!$config) {
  44. // 配置不存在,返回默认值
  45. Cache::put($cacheKey, $defaultValue, $this->cacheTime);
  46. return $defaultValue;
  47. }
  48. $value = $config->getTypedValue();
  49. // 缓存配置值
  50. Cache::put($cacheKey, $value, $this->cacheTime);
  51. return $value;
  52. } catch (\Exception $e) {
  53. Log::error('获取农场配置失败', [
  54. 'config_key' => $configKey,
  55. 'error' => $e->getMessage(),
  56. 'trace' => $e->getTraceAsString()
  57. ]);
  58. return $defaultValue;
  59. }
  60. }
  61. /**
  62. * 设置配置值
  63. *
  64. * @param string $configKey 配置键
  65. * @param mixed $value 配置值
  66. * @return bool
  67. */
  68. public function setConfigValue(string $configKey, $value): bool
  69. {
  70. try {
  71. $config = FarmConfig::where('config_key', $configKey)->first();
  72. if (!$config) {
  73. Log::warning('尝试设置不存在的农场配置', [
  74. 'config_key' => $configKey,
  75. 'value' => $value
  76. ]);
  77. return false;
  78. }
  79. $config->setTypedValue($value);
  80. $config->save();
  81. // 清除缓存
  82. $cacheKey = $this->cachePrefix . $configKey;
  83. Cache::forget($cacheKey);
  84. Log::info('农场配置更新成功', [
  85. 'config_key' => $configKey,
  86. 'old_value' => $config->getOriginal('config_value'),
  87. 'new_value' => $config->config_value
  88. ]);
  89. return true;
  90. } catch (\Exception $e) {
  91. Log::error('设置农场配置失败', [
  92. 'config_key' => $configKey,
  93. 'value' => $value,
  94. 'error' => $e->getMessage(),
  95. 'trace' => $e->getTraceAsString()
  96. ]);
  97. return false;
  98. }
  99. }
  100. /**
  101. * 获取所有配置
  102. *
  103. * @return array
  104. */
  105. public function getAllConfigs(): array
  106. {
  107. try {
  108. $configs = FarmConfig::where('is_active', true)
  109. ->orderBy('config_key')
  110. ->get();
  111. $result = [];
  112. foreach ($configs as $config) {
  113. $result[$config->config_key] = $config->getTypedValue();
  114. }
  115. return $result;
  116. } catch (\Exception $e) {
  117. Log::error('获取所有农场配置失败', [
  118. 'error' => $e->getMessage(),
  119. 'trace' => $e->getTraceAsString()
  120. ]);
  121. return [];
  122. }
  123. }
  124. /**
  125. * 清除所有配置缓存
  126. *
  127. * @return void
  128. */
  129. public function clearCache(): void
  130. {
  131. try {
  132. $configs = FarmConfig::pluck('config_key');
  133. foreach ($configs as $configKey) {
  134. $cacheKey = $this->cachePrefix . $configKey;
  135. Cache::forget($cacheKey);
  136. }
  137. Log::info('农场配置缓存清除成功');
  138. } catch (\Exception $e) {
  139. Log::error('清除农场配置缓存失败', [
  140. 'error' => $e->getMessage(),
  141. 'trace' => $e->getTraceAsString()
  142. ]);
  143. }
  144. }
  145. /**
  146. * 获取农场初始化奖励组ID
  147. *
  148. * @return int
  149. */
  150. public function getInitRewardGroupId(): int
  151. {
  152. return (int) $this->getConfigValue('farm_init_reward_group_id', 0);
  153. }
  154. /**
  155. * 设置农场初始化奖励组ID
  156. *
  157. * @param int $groupId 奖励组ID
  158. * @return bool
  159. */
  160. public function setInitRewardGroupId(int $groupId): bool
  161. {
  162. return $this->setConfigValue('farm_init_reward_group_id', $groupId);
  163. }
  164. }