| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace App\Module\Farm\AdminControllers\Tools;
- use Dcat\Admin\Grid\Tools\AbstractTool;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- class SyncFarmHouseJsonTool extends AbstractTool
- {
- protected $shouldDisplay;
- protected $style = 'btn btn-primary waves-effect';
- public function __construct(bool $shouldDisplay = true)
- {
- $this->shouldDisplay = $shouldDisplay;
- }
- public function title()
- {
- return '生成JSON';
- }
- public function confirm()
- {
- return '确定要生成房屋配置JSON数据吗?';
- }
- public function handle(Request $request)
- {
- try {
- // 直接调用命令生成JSON
- $process = new \Symfony\Component\Process\Process(['php', 'artisan', 'farm:generate-house-json']);
- $process->setWorkingDirectory(base_path());
- $process->run();
- if (!$process->isSuccessful()) {
- Log::error('Generate farm_house.json failed: ' . $process->getErrorOutput());
- return $this->response()->error('生成失败:' . $process->getErrorOutput());
- }
- return $this->response()->success('生成成功')->refresh();
- } catch (\Exception $e) {
- Log::error('Generate farm_house.json exception: '.$e->getMessage());
- return $this->response()->error('生成失败:'.$e->getMessage());
- }
- }
- public function render()
- {
- if (!$this->shouldDisplay) {
- return '';
- }
- return parent::render();
- }
- public static function shouldDisplay(): bool
- {
- $jsonPath = public_path('json/farm_house.json');
- if (!file_exists($jsonPath)) {
- return true;
- }
- $json = json_decode(file_get_contents($jsonPath), true);
- $generatedAt = \Carbon\Carbon::parse($json['generated_at']);
- $lastUpdated = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmHouseConfig::max('updated_at'));
- return $generatedAt->lt($lastUpdated);
- }
- }
|