From 15b6acfe18f313d02c174913f5ac71f0766cc082 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq.com> Date: Wed, 27 May 2026 17:38:15 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9D=9F=E6=A8=A1=E5=BC=8F=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E5=9B=9E=E9=BB=98=E8=AE=A4=E6=A8=A1=E5=BC=8F2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/AutomaticallyStartActivity.php | 41 +--------- app/Services/EventCalendarService.php | 75 ++++++++++++++++--- 2 files changed, 70 insertions(+), 46 deletions(-) diff --git a/app/Console/Commands/AutomaticallyStartActivity.php b/app/Console/Commands/AutomaticallyStartActivity.php index 423164b..c2daa08 100644 --- a/app/Console/Commands/AutomaticallyStartActivity.php +++ b/app/Console/Commands/AutomaticallyStartActivity.php @@ -77,57 +77,24 @@ class AutomaticallyStartActivity extends Command ); })->orderBy('start_time') ->first(['id']); + $EventCalendarService = (new EventCalendarService($this->logService)); if ($res) { // 新活动运行 直接关闭旧活动 $new_id = $res['id']; if ($runRes && $runRes['id'] != $new_id) { - $this->updateStatus($run_id, 2); + $EventCalendarService->updateStatus($run_id, $this->user_id, 2); } - $this->updateStatus($new_id, 1, true); + $EventCalendarService->updateStatus($new_id, $this->user_id, 1, true); } else { // 如果不存在新活动模式,撤回默认模式 $pattern_id = EventCalendarService::getTargetModeId(); if ($pattern_id) { // 更新车位类型 - (new EventCalendarService($this->logService))->syncUpdateSpaceType($pattern_id); + $EventCalendarService->syncUpdateSpaceType($pattern_id); // 记录日志 AdminNoticeService::addChangeModeNotice($pattern_id, $this->user_id); } } } } - - /** - * @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, - 'admin_user_id' => $this->user_id, - 'updated_at' => date("Y-m-d H:i:s", time()) - ]); - // 记录日志 - $this->logService->logUpdated( - $model, - $oldValue, - 'event_calendar.update' - ); - // 记录警报信息, 切换任务, 同步更新指定模式车位类型 - if ($is_notice) { - $pattern_id = $oldValue['pattern_id']; - // 更新车位类型 - (new EventCalendarService($this->logService))->syncUpdateSpaceType($pattern_id); - // 记录日志 - AdminNoticeService::addChangeModeNotice($pattern_id, $this->user_id); - } - } } diff --git a/app/Services/EventCalendarService.php b/app/Services/EventCalendarService.php index ed827af..36a0a46 100644 --- a/app/Services/EventCalendarService.php +++ b/app/Services/EventCalendarService.php @@ -212,15 +212,8 @@ class EventCalendarService extends BaseService 'event_calendar.update' ); - // 切换回默认模式 - $pattern_id = EventCalendarService::getTargetModeId(); - if ($pattern_id) { - // 更新车位类型 - $this->syncUpdateSpaceType($pattern_id); - // 记录日志 - AdminNoticeService::addChangeModeNotice($pattern_id, $user_id); - } - + // 切换回默认模式 或者 需要运行的模式 + $this->autoChangeMode($user_id); DB::commit(); return true; } catch (Exception $e) { @@ -350,4 +343,68 @@ class EventCalendarService extends BaseService ); } } + + // 自动切换模式 + public function autoChangeMode($user_id) + { + $now_time = date("Y-m-d H:i:s", time()); + $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) { + $this->updateStatus($res['id'], $user_id,1, true); + } else { + // 如果不存在新活动模式,撤回默认模式 + $pattern_id = EventCalendarService::getTargetModeId(); + if ($pattern_id) { + // 更新车位类型 + $this->syncUpdateSpaceType($pattern_id); + // 记录日志 + AdminNoticeService::addChangeModeNotice($pattern_id, $user_id); + } + } + } + + /** + * 切换模式 更改状态 + * @param $id + * @param $user_id + * @param int $status // 1运行 、 2结束 + * @param bool $is_notice + * @return void + */ + public function updateStatus( + $id, + $user_id, + int $status = 1, + bool $is_notice = false + ): void { + $model = EventCalendar::query()->findOrFail($id); + $oldValue = $model->toArray(); + $model->update([ + 'status' => $status, + 'admin_user_id' => $user_id, + 'updated_at' => date("Y-m-d H:i:s", time()) + ]); + // 记录日志 + $this->logService->logUpdated( + $model, + $oldValue, + 'event_calendar.update' + ); + // 记录警报信息, 切换任务, 同步更新指定模式车位类型 + if ($is_notice) { + $pattern_id = $oldValue['pattern_id']; + // 更新车位类型 + $this->syncUpdateSpaceType($pattern_id); + // 记录日志 + AdminNoticeService::addChangeModeNotice($pattern_id, $user_id); + } + } }