| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace App\Module\Farm\AdminControllers\Tools;
- use App\Module\Game\DCache\FarmHouseJsonConfig;
- use App\Module\Game\DCache\FarmLandJsonConfig;
- 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();
- }
- /**
- * 检查配置表同步状态
- *
- * @param string $type 配置表类型,可选值:house, land, all
- * @return array
- */
- public static function checkSyncStatus(string $type = 'house'): array
- {
- if ($type === 'house') {
- return self::checkHouseConfigStatus();
- } elseif ($type === 'land') {
- return self::checkLandConfigStatus();
- } else {
- // 检查所有配置表
- $houseStatus = self::checkHouseConfigStatus();
- $landStatus = self::checkLandConfigStatus();
- // 如果都已同步,返回成功状态
- if ($houseStatus['is_synced'] && $landStatus['is_synced']) {
- return [
- 'is_synced' => true,
- 'message' => '所有配置表已同步',
- 'should_display' => false
- ];
- }
- // 否则返回需要同步的状态
- $message = '';
- if (!$houseStatus['is_synced']) {
- $message .= '房屋配置表需要同步; ';
- }
- if (!$landStatus['is_synced']) {
- $message .= '土地配置表需要同步; ';
- }
- return [
- 'is_synced' => false,
- 'message' => $message,
- 'should_display' => true
- ];
- }
- }
- /**
- * 检查房屋配置表状态
- *
- * @return array
- */
- private static function checkHouseConfigStatus(): array
- {
- try {
- $json = FarmHouseJsonConfig::getData();
- $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
- $lastUpdated = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmHouseConfig::max('updated_at'));
- if ($generatedAt->lt($lastUpdated)) {
- return [
- 'is_synced' => false,
- 'message' => "房屋配置表需要更新(生成于 {$generatedAt->format('Y-m-d H:i:s')},最后修改于 {$lastUpdated->format('Y-m-d H:i:s')})",
- 'should_display' => true
- ];
- } else {
- return [
- 'is_synced' => true,
- 'message' => "房屋配置表已是最新(生成于 {$generatedAt->format('Y-m-d H:i:s')})",
- 'should_display' => false
- ];
- }
- } catch (\Exception $e) {
- Log::error('Check farm house config sync status failed: '.$e->getMessage());
- return [
- 'is_synced' => false,
- 'message' => '检查房屋配置表状态失败:'.$e->getMessage(),
- 'should_display' => true
- ];
- }
- }
- /**
- * 检查土地配置表状态
- *
- * @return array
- */
- private static function checkLandConfigStatus(): array
- {
- try {
- $json = FarmLandJsonConfig::getData();
- // 如果没有生成时间戳,说明需要生成
- if (!isset($json['generated_ts'])) {
- return [
- 'is_synced' => false,
- 'message' => '土地配置表需要生成',
- 'should_display' => true
- ];
- }
- $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
- // 获取土地类型和升级配置的最后更新时间
- $lastUpdatedType = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmLandType::max('updated_at') ?: '2000-01-01');
- $lastUpdatedUpgrade = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmLandUpgradeConfig::max('updated_at') ?: '2000-01-01');
- // 取最新的更新时间
- $lastUpdated = $lastUpdatedType->gt($lastUpdatedUpgrade) ? $lastUpdatedType : $lastUpdatedUpgrade;
- if ($generatedAt->lt($lastUpdated)) {
- return [
- 'is_synced' => false,
- 'message' => "土地配置表需要更新(生成于 {$generatedAt->format('Y-m-d H:i:s')},最后修改于 {$lastUpdated->format('Y-m-d H:i:s')})",
- 'should_display' => true
- ];
- } else {
- return [
- 'is_synced' => true,
- 'message' => "土地配置表已是最新(生成于 {$generatedAt->format('Y-m-d H:i:s')})",
- 'should_display' => false
- ];
- }
- } catch (\Exception $e) {
- Log::error('Check farm land config sync status failed: '.$e->getMessage());
- return [
- 'is_synced' => false,
- 'message' => '检查土地配置表状态失败:'.$e->getMessage(),
- 'should_display' => true
- ];
- }
- }
- }
|