| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace App\Module\Fund\AdminControllers\Tools;
- use App\Module\Game\DCache\FundCurrencyJsonConfig;
- use App\Module\Fund\Models\FundCurrencyModel;
- use Carbon\CarbonInterface;
- use Dcat\Admin\Grid\Tools\AbstractTool;
- use Illuminate\Support\Facades\Log;
- /**
- * 基金配置表状态检查工具
- *
- * 用于检查基金配置表的同步状态
- */
- class RefreshCheckTool extends AbstractTool
- {
- protected $shouldDisplay;
- protected $style = 'btn btn-default waves-effect';
- public function __construct(bool $shouldDisplay = true)
- {
- $this->shouldDisplay = $shouldDisplay;
- }
- public function title()
- {
- return '检查状态';
- }
- public function render()
- {
- if (!$this->shouldDisplay) {
- return '';
- }
- return parent::render();
- }
- /**
- * 检查配置表同步状态
- *
- * @return array
- */
- public static function checkSyncStatus(): array
- {
- try {
- $json = FundCurrencyJsonConfig::getData();
-
- // 如果没有生成时间戳,说明需要生成
- if (!isset($json['generated_ts'])) {
- return [
- 'is_synced' => false,
- 'message' => '【货币配置表】需要生成,尚未找到配置数据',
- 'should_display' => true
- ];
- }
-
- $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
- $lastUpdated = \Carbon\Carbon::parse(FundCurrencyModel::max('update_time') ?: '2000-01-01');
- // 使用绝对时间差,避免相对时间导致的"X小时后"这样的显示问题
- $options = ['syntax' => CarbonInterface::DIFF_ABSOLUTE];
-
- if ($generatedAt->lt($lastUpdated)) {
- return [
- 'is_synced' => false,
- 'message' => "【货币配置表】需要更新,生成于 {$generatedAt->diffForHumans(null, $options)},数据库最后更新于 {$lastUpdated->diffForHumans(null, $options)}",
- 'should_display' => true
- ];
- } else {
- return [
- 'is_synced' => true,
- 'message' => "【货币配置表】已同步,生成于 {$generatedAt->diffForHumans(null, $options)}",
- 'should_display' => false
- ];
- }
- } catch (\Exception $e) {
- Log::error('Check fund currency config sync status failed: '.$e->getMessage());
- return [
- 'is_synced' => false,
- 'message' => '【货币配置表】检查状态失败:'.$e->getMessage(),
- 'should_display' => true
- ];
- }
- }
- public static function shouldDisplay(): bool
- {
- return self::checkSyncStatus()['should_display'];
- }
- }
|