RefreshCheckTool.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Module\Fund\AdminControllers\Tools;
  3. use App\Module\Game\DCache\FundCurrencyJsonConfig;
  4. use App\Module\Fund\Models\FundCurrencyModel;
  5. use Carbon\CarbonInterface;
  6. use Dcat\Admin\Grid\Tools\AbstractTool;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 基金配置表状态检查工具
  10. *
  11. * 用于检查基金配置表的同步状态
  12. */
  13. class RefreshCheckTool extends AbstractTool
  14. {
  15. protected $shouldDisplay;
  16. protected $style = 'btn btn-default waves-effect';
  17. public function __construct(bool $shouldDisplay = true)
  18. {
  19. $this->shouldDisplay = $shouldDisplay;
  20. }
  21. public function title()
  22. {
  23. return '检查状态';
  24. }
  25. public function render()
  26. {
  27. if (!$this->shouldDisplay) {
  28. return '';
  29. }
  30. return parent::render();
  31. }
  32. /**
  33. * 检查配置表同步状态
  34. *
  35. * @return array
  36. */
  37. public static function checkSyncStatus(): array
  38. {
  39. try {
  40. $json = FundCurrencyJsonConfig::getData();
  41. // 如果没有生成时间戳,说明需要生成
  42. if (!isset($json['generated_ts'])) {
  43. return [
  44. 'is_synced' => false,
  45. 'message' => '【货币配置表】需要生成,尚未找到配置数据',
  46. 'should_display' => true
  47. ];
  48. }
  49. $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
  50. $lastUpdated = \Carbon\Carbon::parse(FundCurrencyModel::max('update_time') ?: '2000-01-01');
  51. // 使用绝对时间差,避免相对时间导致的"X小时后"这样的显示问题
  52. $options = ['syntax' => CarbonInterface::DIFF_ABSOLUTE];
  53. if ($generatedAt->lt($lastUpdated)) {
  54. return [
  55. 'is_synced' => false,
  56. 'message' => "【货币配置表】需要更新,生成于 {$generatedAt->diffForHumans(null, $options)},数据库最后更新于 {$lastUpdated->diffForHumans(null, $options)}",
  57. 'should_display' => true
  58. ];
  59. } else {
  60. return [
  61. 'is_synced' => true,
  62. 'message' => "【货币配置表】已同步,生成于 {$generatedAt->diffForHumans(null, $options)}",
  63. 'should_display' => false
  64. ];
  65. }
  66. } catch (\Exception $e) {
  67. Log::error('Check fund currency config sync status failed: '.$e->getMessage());
  68. return [
  69. 'is_synced' => false,
  70. 'message' => '【货币配置表】检查状态失败:'.$e->getMessage(),
  71. 'should_display' => true
  72. ];
  73. }
  74. }
  75. public static function shouldDisplay(): bool
  76. {
  77. return self::checkSyncStatus()['should_display'];
  78. }
  79. }