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

79 lines
2.5 KiB

10 months ago
<?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;
9 months ago
// 数组follw_people中的follw_people数据置空置
$list = [];
foreach ($visit['follw_people'] as $follwPerson) {
unset($follwPerson['follw_people_person_no']);
$list[] = $follwPerson;
}
$visit['follw_people'] = $list;
10 months ago
$visit->save();
// 记录重置日志
9 months ago
// VisitLog::log($visit->id, VisitLog::TYPE_UPDATE, "系统自动重置:长期拜访预约审核状态已重置为待审核");
10 months ago
$resetCount++;
$this->info("已重置访客:{$visit->name}{$visit->code}");
}
$this->info("重置完成,共处理 {$resetCount} 条记录");
Log::info("长期拜访预约重置任务完成,共处理 {$resetCount} 条记录");
} catch (\Exception $e) {
Log::error('重置长期拜访预约失败:' . $e->getMessage());
$this->error('重置失败:' . $e->getMessage());
}
}
}