From 2aa62ff31e2f2490af8c78f7603d5289f650b575 Mon Sep 17 00:00:00 2001 From: lion <120344285@qq.com> Date: Tue, 19 May 2026 09:59:10 +0800 Subject: [PATCH] =?UTF-8?q?=E9=95=BF=E6=9C=9F=E8=AE=BF=E5=AE=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ResetLongTermVisitDailyStatusCommand.php | 65 +++++++++++++++++++ app/Console/Kernel.php | 3 +- app/Models/Visit.php | 37 +++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/ResetLongTermVisitDailyStatusCommand.php diff --git a/app/Console/Commands/ResetLongTermVisitDailyStatusCommand.php b/app/Console/Commands/ResetLongTermVisitDailyStatusCommand.php new file mode 100644 index 0000000..7c79348 --- /dev/null +++ b/app/Console/Commands/ResetLongTermVisitDailyStatusCommand.php @@ -0,0 +1,65 @@ +format('Y-m-d'); + $count = 0; + + Visit::query() + ->where('long_time', 1) + ->whereIn('audit_status', [3, 4]) + ->orderBy('id') + ->chunkById(200, function ($visits) use ($today, &$count) { + foreach ($visits as $visit) { + $lastLog = GateLog::query() + ->where('visit_id', $visit->id) + ->orderByDesc('id') + ->first(); + if (!$lastLog && !empty($visit->code)) { + $lastLog = GateLog::query() + ->where('code', $visit->code) + ->orderByDesc('id') + ->first(); + } + + if (!$lastLog || (int) $lastLog->action !== 2) { + continue; + } + + $bizDate = null; + if (!empty($lastLog->biz_date)) { + $bizDate = Carbon::parse($lastLog->biz_date)->format('Y-m-d'); + } elseif (!empty($lastLog->created_at)) { + $bizDate = Carbon::parse($lastLog->created_at)->format('Y-m-d'); + } + if ($bizDate === null || $bizDate >= $today) { + continue; + } + + $visit->audit_status = 1; + $visit->saveQuietly(); + $count++; + } + }); + + $this->info("已重置 {$count} 条长期访客为待进厂(audit_status=1)。"); + + return self::SUCCESS; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index d8bc1d2..e2e484c 100755 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,7 +15,8 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // $schedule->command('inspire')->hourly(); + // 每日 00:10:长期访客昨日及以前已离厂的,重置为待进厂 + $schedule->command('visit:reset-long-term-daily-status')->dailyAt('00:10'); } /** diff --git a/app/Models/Visit.php b/app/Models/Visit.php index a2c333a..e395719 100755 --- a/app/Models/Visit.php +++ b/app/Models/Visit.php @@ -3,6 +3,7 @@ namespace App\Models; use EasyWeChat\Factory; +use Illuminate\Support\Carbon; class Visit extends SoftDeletesModel { @@ -10,6 +11,42 @@ class Visit extends SoftDeletesModel protected $appends = ['type_text', 'audit_status_text', 'file_detail']; + protected static function boot() + { + parent::boot(); + + static::saving(function (Visit $visit) { + $visit->applyConstructionCrossDayAsLongTerm(); + }); + } + + /** + * 施工访客且施工起止不为同一天:自动长期访客,并同步 start_date / end_date。 + */ + public function applyConstructionCrossDayAsLongTerm(): void + { + if ((int) $this->type !== 2) { + return; + } + $ws = $this->work_start_time; + $we = $this->work_end_time; + if (empty($ws) || empty($we)) { + return; + } + try { + $d1 = Carbon::parse($ws)->format('Y-m-d'); + $d2 = Carbon::parse($we)->format('Y-m-d'); + } catch (\Throwable $e) { + return; + } + if ($d1 === $d2) { + return; + } + $this->long_time = 1; + $this->start_date = $d1; + $this->end_date = $d2; + } + protected $casts = [ 'follw_people' => 'json', 'cars' => 'json',