长期访客

master
lion 6 days ago
parent 1838475068
commit 2aa62ff31e

@ -0,0 +1,65 @@
<?php
namespace App\Console\Commands;
use App\Models\GateLog;
use App\Models\Visit;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
/**
* 长期访客最后一条门岗为「离厂」且业务日期早于今天时将审核状态重置为待进厂audit_status=1
*/
class ResetLongTermVisitDailyStatusCommand extends Command
{
protected $signature = 'visit:reset-long-term-daily-status';
protected $description = '长期访客按日重置:仅当最后一条门岗为离厂且非今日离厂时,置为待进厂';
public function handle(): int
{
$today = Carbon::today()->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;
}
}

@ -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');
}
/**

@ -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',

Loading…
Cancel
Save