RefreshCheckTool.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Module\Farm\AdminControllers\Tools;
  3. use App\Module\Game\DCache\FarmHouseJsonConfig;
  4. use App\Module\Game\DCache\FarmLandJsonConfig;
  5. use Carbon\CarbonInterface;
  6. use Dcat\Admin\Grid\Tools\AbstractTool;
  7. use Illuminate\Support\Facades\Log;
  8. class RefreshCheckTool extends AbstractTool
  9. {
  10. protected $shouldDisplay;
  11. protected $style = 'btn btn-default waves-effect';
  12. public function __construct(bool $shouldDisplay = true)
  13. {
  14. $this->shouldDisplay = $shouldDisplay;
  15. }
  16. public function title()
  17. {
  18. return '检查状态';
  19. }
  20. public function render()
  21. {
  22. if (!$this->shouldDisplay) {
  23. return '';
  24. }
  25. return parent::render();
  26. }
  27. /**
  28. * 检查配置表同步状态
  29. *
  30. * @param string $type 配置表类型,可选值:house, land, all
  31. * @return array
  32. */
  33. public static function checkSyncStatus(string $type = 'house'): array
  34. {
  35. if ($type === 'house') {
  36. return self::checkHouseConfigStatus();
  37. } elseif ($type === 'land') {
  38. return self::checkLandConfigStatus();
  39. } else {
  40. // 检查所有配置表
  41. $houseStatus = self::checkHouseConfigStatus();
  42. $landStatus = self::checkLandConfigStatus();
  43. // 如果都已同步,返回成功状态
  44. if ($houseStatus['is_synced'] && $landStatus['is_synced']) {
  45. return [
  46. 'is_synced' => true,
  47. 'message' => '【农场配置】所有配置表已同步',
  48. 'should_display' => false
  49. ];
  50. }
  51. // 否则返回需要同步的状态
  52. $message = '【农场配置】';
  53. if (!$houseStatus['is_synced']) {
  54. $message .= '房屋配置表需要更新; ';
  55. }
  56. if (!$landStatus['is_synced']) {
  57. $message .= '土地配置表需要更新; ';
  58. }
  59. return [
  60. 'is_synced' => false,
  61. 'message' => $message,
  62. 'should_display' => true
  63. ];
  64. }
  65. }
  66. /**
  67. * 检查房屋配置表状态
  68. *
  69. * @return array
  70. */
  71. private static function checkHouseConfigStatus(): array
  72. {
  73. try {
  74. $json = FarmHouseJsonConfig::getData();
  75. $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
  76. $lastUpdated = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmHouseConfig::max('updated_at'));
  77. // 使用绝对时间差,避免相对时间导致的"X小时后"这样的显示问题
  78. $options = ['syntax' => \Carbon\CarbonInterface::DIFF_ABSOLUTE];
  79. if ($generatedAt->lt($lastUpdated)) {
  80. return [
  81. 'is_synced' => false,
  82. 'message' => "【房屋配置表】需要更新,生成于 {$generatedAt->diffForHumans(null, $options)},数据库最后更新于 {$lastUpdated->diffForHumans(null, $options)}",
  83. 'should_display' => true
  84. ];
  85. } else {
  86. return [
  87. 'is_synced' => true,
  88. 'message' => "【房屋配置表】已同步,生成于 {$generatedAt->diffForHumans(null, $options)}",
  89. 'should_display' => false
  90. ];
  91. }
  92. } catch (\Exception $e) {
  93. Log::error('Check farm house config sync status failed: '.$e->getMessage());
  94. return [
  95. 'is_synced' => false,
  96. 'message' => '【房屋配置表】检查状态失败:'.$e->getMessage(),
  97. 'should_display' => true
  98. ];
  99. }
  100. }
  101. /**
  102. * 检查土地配置表状态
  103. *
  104. * @return array
  105. */
  106. private static function checkLandConfigStatus(): array
  107. {
  108. try {
  109. $json = FarmLandJsonConfig::getData();
  110. // 如果没有生成时间戳,说明需要生成
  111. if (!isset($json['generated_ts'])) {
  112. return [
  113. 'is_synced' => false,
  114. 'message' => '【土地配置表】需要生成,尚未找到配置数据',
  115. 'should_display' => true
  116. ];
  117. }
  118. $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
  119. // 获取土地类型和升级配置的最后更新时间
  120. $lastUpdatedType = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmLandType::max('updated_at') ?: '2000-01-01');
  121. $lastUpdatedUpgrade = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmLandUpgradeConfig::max('updated_at') ?: '2000-01-01');
  122. // 取最新的更新时间
  123. $lastUpdated = $lastUpdatedType->gt($lastUpdatedUpgrade) ? $lastUpdatedType : $lastUpdatedUpgrade;
  124. // 使用绝对时间差,避免相对时间导致的"X小时后"这样的显示问题
  125. $options = ['syntax' => CarbonInterface::DIFF_ABSOLUTE];
  126. if ($generatedAt->lt($lastUpdated)) {
  127. return [
  128. 'is_synced' => false,
  129. 'message' => "【土地配置表】需要更新,生成于 {$generatedAt->diffForHumans(null, $options)},数据库最后更新于 {$lastUpdated->diffForHumans(null, $options)}",
  130. 'should_display' => true
  131. ];
  132. } else {
  133. return [
  134. 'is_synced' => true,
  135. 'message' => "【土地配置表】已同步,生成于 {$generatedAt->diffForHumans(null, $options)}",
  136. 'should_display' => false
  137. ];
  138. }
  139. } catch (\Exception $e) {
  140. Log::error('Check farm land config sync status failed: '.$e->getMessage());
  141. return [
  142. 'is_synced' => false,
  143. 'message' => '【土地配置表】检查状态失败:'.$e->getMessage(),
  144. 'should_display' => true
  145. ];
  146. }
  147. }
  148. }