From b67fa267aed96997174613001cad49aba4de0d46 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq.com> Date: Tue, 19 May 2026 10:20:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=BC=80=E5=A7=8B=E6=B4=BB?= =?UTF-8?q?=E5=8A=A8=E6=A8=A1=E5=BC=8F=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/AutomaticallyStartActivity.php | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 app/Console/Commands/AutomaticallyStartActivity.php diff --git a/app/Console/Commands/AutomaticallyStartActivity.php b/app/Console/Commands/AutomaticallyStartActivity.php new file mode 100644 index 0000000..c1a6439 --- /dev/null +++ b/app/Console/Commands/AutomaticallyStartActivity.php @@ -0,0 +1,111 @@ +logService = $log; + $this->logService->menuTitle = 'event_calendar'; + } + + /** + * 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; + // 数据存在,且结束时间到了,结束时间 + if ($runRes) { + $end_time = $runRes['end_time']; + $run_id = $runRes['id']; + $is_manual = $runRes['is_manual']; + if ($end_time && strtotime($end_time) <= $now_times) { + $this->updateStatus($run_id, 2); + } elseif ($is_manual == 1) { + // 手动开始的不切换模式 + $start = false; + } + } + + // 启动 新模式 + if ($start) { + $where = [ + ['status', '=', 0], + ['start_time', '>=', $now_time], + ['end_time', '<=', $now_time] + ]; + $res = EventCalendar::query()->where($where)->orderBy('start_time') + ->first(['id']); + if ($res) { + // 新活动运行 直接关闭就活动 + $new_id = $res['id']; + if ($runRes && $runRes['id'] != $new_id) { + $this->updateStatus($run_id, 2); + } + $this->updateStatus($new_id, true); + } + } + } + + /** + * @param $id + * @param int $status // 1运行 、 2结束 + * @param bool $is_notice + * @return void + */ + protected function updateStatus( + $id, + int $status = 1, + bool $is_notice = false + ): void { + $model = EventCalendar::query()->findOrFail($id); + $oldValue = $model->toArray(); + $model->update([ + 'status' => $status, + 'updated_at' => get_datetime() + ]); + // 记录日志 + $this->logService->logUpdated($model, $oldValue); + // 记录警报信息, 切换任务 + if ($is_notice) { + $data = [ + 'pattern_id' => $oldValue['pattern_id'] + ]; + $user_id = DB::table('admin_users')->where('username', 'Admin') + ->value('id'); + AdminNoticeService::createData(2, 5, $data, $user_id); + } + } +}