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.
102 lines
3.2 KiB
102 lines
3.2 KiB
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\EventCalendar;
|
|
use App\Services\AdminNoticeService;
|
|
use App\Services\EventCalendarService;
|
|
use App\Services\OperationLogService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AutomaticallyStartActivity extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
* @var string
|
|
*/
|
|
protected $signature = 'run:automatically-start-activity';
|
|
|
|
/**
|
|
* The console command description.
|
|
* @var string
|
|
*/
|
|
protected $description = 'Command description';
|
|
|
|
protected OperationLogService $logService;
|
|
|
|
protected int $user_id;
|
|
|
|
public function __construct(OperationLogService $log)
|
|
{
|
|
parent::__construct();
|
|
$this->logService = $log;
|
|
$this->logService->menuTitle = 'event_calendar';
|
|
|
|
// 默认超级管理员修改
|
|
$this->user_id = DB::table('admin_users')->where('username', 'Admin')
|
|
->value('id');
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$now_times = time();
|
|
$now_time = date("Y-m-d H:i:s", $now_times);
|
|
// 结束活动,调整为默认模式
|
|
$where = [
|
|
['status', '=', 1]
|
|
];
|
|
$runRes = EventCalendar::query()->where($where)->orderBy('created_at')
|
|
->first();
|
|
|
|
$start = true;
|
|
$EventCalendarService = new EventCalendarService($this->logService);
|
|
// 数据存在,且结束时间到了,结束时间
|
|
if ($runRes) {
|
|
$end_time = $runRes['end_time'];
|
|
$run_id = $runRes['id'];
|
|
$is_manual = $runRes['is_manual'];
|
|
if ($end_time && strtotime($end_time) <= $now_times) {
|
|
$EventCalendarService->updateStatus($run_id, $this->user_id, 2);
|
|
} elseif ($is_manual == 1) {
|
|
// 手动开始的不切换模式
|
|
$start = false;
|
|
} else {
|
|
$start = false;
|
|
}
|
|
}
|
|
|
|
// 启动 新模式
|
|
if ($start) {
|
|
$where = [
|
|
['status', '=', 0]
|
|
];
|
|
$res = EventCalendar::query()->where($where)->where(function ($query) use ($now_time) {
|
|
$query->whereRaw(
|
|
"'{$now_time}' BETWEEN start_time AND end_time"
|
|
);
|
|
})->orderBy('start_time')
|
|
->first(['id']);
|
|
if ($res) {
|
|
// 新活动运行 直接关闭旧活动
|
|
$new_id = $res['id'];
|
|
if ($runRes && $runRes['id'] != $new_id) {
|
|
$EventCalendarService->updateStatus($run_id, $this->user_id, 2);
|
|
}
|
|
$EventCalendarService->updateStatus($new_id, $this->user_id, 1, true);
|
|
} else {
|
|
// 如果不存在新活动模式,撤回默认模式
|
|
$pattern_id = EventCalendarService::getTargetModeId();
|
|
if ($pattern_id) {
|
|
// 更新车位类型
|
|
$EventCalendarService->syncUpdateSpaceType($pattern_id);
|
|
// 记录日志
|
|
//AdminNoticeService::addChangeModeNotice($pattern_id, $this->user_id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|