RefreshCheckTool.php 5.4 KB

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