RefreshCheckTool.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Module\Farm\AdminControllers\Tools;
  3. use App\Module\Game\DCache\FarmHouseJsonConfig;
  4. use Dcat\Admin\Grid\Tools\AbstractTool;
  5. use Illuminate\Support\Facades\Log;
  6. class RefreshCheckTool extends AbstractTool
  7. {
  8. protected $shouldDisplay;
  9. protected $style = 'btn btn-default waves-effect';
  10. public function __construct(bool $shouldDisplay = true)
  11. {
  12. $this->shouldDisplay = $shouldDisplay;
  13. }
  14. public function title()
  15. {
  16. return '检查状态';
  17. }
  18. public function render()
  19. {
  20. if (!$this->shouldDisplay) {
  21. return '';
  22. }
  23. return parent::render();
  24. }
  25. public static function checkSyncStatus(): array
  26. {
  27. try {
  28. $json = FarmHouseJsonConfig::getData([],true);
  29. $generatedAt = \Carbon\Carbon::parse($json['generated_at']);
  30. $lastUpdated = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmHouseConfig::max('updated_at'));
  31. if ($generatedAt->lt($lastUpdated)) {
  32. return [
  33. 'is_synced' => false,
  34. 'message' => "配置文件需要更新(生成于 {$generatedAt->format('Y-m-d H:i:s')},最后修改于 {$lastUpdated->format('Y-m-d H:i:s')})",
  35. 'should_display' => true
  36. ];
  37. } else {
  38. return [
  39. 'is_synced' => true,
  40. 'message' => "配置文件已是最新(生成于 {$generatedAt->format('Y-m-d H:i:s')})",
  41. 'should_display' => false
  42. ];
  43. }
  44. } catch (\Exception $e) {
  45. Log::error('Check farm house config sync status failed: '.$e->getMessage());
  46. return [
  47. 'is_synced' => false,
  48. 'message' => '检查配置文件状态失败:'.$e->getMessage(),
  49. 'should_display' => true
  50. ];
  51. }
  52. }
  53. }