You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ss-visit/app/Console/Commands/ResetLongTermVisitsCommand.php

73 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Console\Commands;
use App\Models\Visit;
use App\Models\VisitLog;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Carbon\Carbon;
class ResetLongTermVisitsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'visit:reset-long-term';
/**
* The console command description.
*
* @var string
*/
protected $description = '重置长期拜访预约的审核状态每天零点1分执行';
/**
* Execute the console command.
*/
public function handle()
{
$this->info('开始检查长期拜访预约...');
try {
$today = Carbon::today();
// 查找长期拜访预约long_time = 1且在有效期内start_date <= 今天 <= end_date
$longTermVisits = Visit::where('start_date', '<=', $today)
->where('end_date', '>=', $today)
// 3已进厂4已离厂
->whereIn('audit_status', [3, 4])
->get();
if ($longTermVisits->isEmpty()) {
$this->info('没有需要重置的长期拜访预约');
return;
}
$this->info("找到 {$longTermVisits->count()} 条需要重置的长期拜访预约");
$resetCount = 0;
foreach ($longTermVisits as $visit) {
// 重置审核状态为已通过1
$visit->audit_status = 1;
$visit->save();
// 记录重置日志
// VisitLog::log($visit->id, VisitLog::TYPE_UPDATE, "系统自动重置:长期拜访预约审核状态已重置为待审核");
$resetCount++;
$this->info("已重置访客:{$visit->name}{$visit->code}");
}
$this->info("重置完成,共处理 {$resetCount} 条记录");
Log::info("长期拜访预约重置任务完成,共处理 {$resetCount} 条记录");
} catch (\Exception $e) {
Log::error('重置长期拜访预约失败:' . $e->getMessage());
$this->error('重置失败:' . $e->getMessage());
}
}
}