From 6c38607f0805c530bcc978a29125c24822e454d8 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq.com> Date: Wed, 20 May 2026 18:05:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E5=88=87=E6=8D=A2=E8=BD=A6?= =?UTF-8?q?=E4=BD=8D=E5=B1=9E=E6=80=A7=E5=88=87=E6=8D=A2=EF=BC=8C=E8=BD=A6?= =?UTF-8?q?=E4=BD=8D=E5=B1=9E=E6=80=A7=E5=90=8C=E6=AD=A5=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/AutomaticallyStartActivity.php | 30 +++++++++--- app/Services/EventCalendarService.php | 47 +++++++++++++++---- app/Services/ParkingPatternService.php | 22 ++++----- app/Services/ParkingPatternSpaceService.php | 42 ++++++++++++----- app/Services/ParkingSpaceService.php | 36 ++++++++++++++ 5 files changed, 140 insertions(+), 37 deletions(-) diff --git a/app/Console/Commands/AutomaticallyStartActivity.php b/app/Console/Commands/AutomaticallyStartActivity.php index 71d0df1..badc80d 100644 --- a/app/Console/Commands/AutomaticallyStartActivity.php +++ b/app/Console/Commands/AutomaticallyStartActivity.php @@ -4,6 +4,7 @@ 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; @@ -24,11 +25,17 @@ class AutomaticallyStartActivity extends Command 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'); } /** @@ -69,12 +76,21 @@ class AutomaticallyStartActivity extends Command $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, 1, true); + } else { + // 如果不存在新活动模式,撤回默认模式 + $pattern_id = EventCalendarService::getTargetModeId(); + if ($pattern_id) { + // 更新车位类型 + (new EventCalendarService($this->logService))->syncUpdateSpaceType($pattern_id); + // 记录日志 + AdminNoticeService::addChangeModeNotice($pattern_id, $this->user_id); + } } } } @@ -92,12 +108,9 @@ class AutomaticallyStartActivity extends Command ): void { $model = EventCalendar::query()->findOrFail($id); $oldValue = $model->toArray(); - // 默认超级管理员修改 - $user_id = DB::table('admin_users')->where('username', 'Admin') - ->value('id'); $model->update([ 'status' => $status, - 'admin_user_id' => $user_id, + 'admin_user_id' => $this->user_id, 'updated_at' => date("Y-m-d H:i:s", time()) ]); // 记录日志 @@ -106,10 +119,13 @@ class AutomaticallyStartActivity extends Command $oldValue, 'event_calendar.update' ); - // 记录警报信息, 切换任务 + // 记录警报信息, 切换任务, 同步更新指定模式车位类型 if ($is_notice) { $pattern_id = $oldValue['pattern_id']; - AdminNoticeService::addChangeModeNotice($pattern_id, $user_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 e2ac6f2..69ca5ad 100644 --- a/app/Services/EventCalendarService.php +++ b/app/Services/EventCalendarService.php @@ -2,9 +2,9 @@ namespace App\Services; -use App\Exceptions\CustomException; use App\Models\EventCalendar; use App\Models\ParkingPattern; +use App\Models\ParkingPatternSpace; use Exception; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; @@ -101,10 +101,13 @@ class EventCalendarService extends BaseService DB::beginTransaction(); $existsWhere = [ ['pattern_id', '=', $data['pattern_id']], - ['status', 'in', [0, 1]], ['id', '<>', $id] ]; - if (EventCalendar::query()->where($existsWhere)->exists()) { + if (EventCalendar::query()->where($existsWhere)->whereIn( + 'status', + [0, 1] + )->exists() + ) { throw new Exception( __('service.event_calendar.pattern_exists') ); @@ -236,11 +239,8 @@ class EventCalendarService extends BaseService ); } - $where = [ - ['pattern_id', '=', $pattern_id], - ['status', 'in', [0,1]] - ]; - $query = EventCalendar::query()->where($where)->first(); + $query = EventCalendar::query()->where('pattern_id', $pattern_id) + ->whereIn('status', [0, 1])->first(); $end_time = get_datetime('datetime', strtotime($end_time)); $save = [ @@ -268,6 +268,8 @@ class EventCalendarService extends BaseService $this->logService->logCreated($model, 'event_calendar.create'); } + // 切换活动模式,将模式所有车位类型 全部更新当前车位 + $this->syncUpdateSpaceType($pattern_id); // 生成警报信息消息 AdminNoticeService::addChangeModeNotice($pattern_id, $user_id); @@ -279,6 +281,7 @@ class EventCalendarService extends BaseService } } + // 返回当前模式的模式信息 public static function targetModel() { $pattern_id = EventCalendar::query()->where('status', 1)->value('pattern_id'); @@ -290,4 +293,32 @@ class EventCalendarService extends BaseService } return $data; } + + // 返回当前模式的模式id + public static function getTargetModeId() + { + $data = self::targetModel(); + return $data['pattern_id'] ?? ''; + } + + // 切换模式后 - 同步更新所有车位类型 + public function syncUpdateSpaceType($pattern_id) + { + // 查询当前模式是否 + $data = ParkingPatternSpace::query()->where('pattern_id', $pattern_id) + ->select() + ->get()->toArray(); + foreach ($data as $item) { + $space_id = $item['space_id']; + $space_type_id = $item['space_type_id']; + (new ParkingSpaceService( + $this->logService + ))->syncUpdateSpaceType( + $space_id, + $space_type_id, + $pattern_id, + false + ); + } + } } diff --git a/app/Services/ParkingPatternService.php b/app/Services/ParkingPatternService.php index a0c5077..70beafc 100644 --- a/app/Services/ParkingPatternService.php +++ b/app/Services/ParkingPatternService.php @@ -180,27 +180,27 @@ class ParkingPatternService extends BaseService throw new Exception(''); } + // 更新车位id $pattern_space_id = ParkingPatternSpace::query()->where( 'space_id', $parking_space_id )->value('id'); $save = [ - 'pattern_id' => $pattern_id, - 'parking_space_id' => $parking_space_id, - 'parking_space_type' => $parking_space_type_id + 'pattern_id' => $pattern_id, + 'space_id' => $parking_space_id, + 'space_type_id' => $parking_space_type_id ]; + $ParkingPatternSpaceService = (new ParkingPatternSpaceService( + $this->logService + )); if ($pattern_space_id) { - $ParkingPatternSpace = ParkingPatternSpace::query()->findOrFail($pattern_space_id); - $oldValues = $ParkingPatternSpace->toArray(); - $ParkingPatternSpace->update($save); - $this->logService->logUpdated( - $ParkingPatternSpace, - $oldValues, - 'parking_pattern_space.update' + $ParkingPatternSpaceService->updateData( + $pattern_space_id, + $save['space_type_id'] ); } else { - (new ParkingPatternSpaceService($this->logService))->createData($save); + $ParkingPatternSpaceService->createData($save); } DB::commit(); diff --git a/app/Services/ParkingPatternSpaceService.php b/app/Services/ParkingPatternSpaceService.php index 9c4c098..77cf30f 100644 --- a/app/Services/ParkingPatternSpaceService.php +++ b/app/Services/ParkingPatternSpaceService.php @@ -65,6 +65,12 @@ class ParkingPatternSpaceService extends BaseService $ParkingPatternSpace, 'parking_pattern_space.create' ); + // 同步更新车位类型 + (new ParkingSpaceService($this->logService))->syncUpdateSpaceType( + $save['space_id'], + $save['space_type_id'], + $data['pattern_id'] + ); return $ParkingPatternSpace; } @@ -78,17 +84,7 @@ class ParkingPatternSpaceService extends BaseService { try { DB::beginTransaction(); - $update = [ - 'space_type_id' => $data['parking_space_type'] - ]; - $model = ParkingPatternSpace::query()->findOrFail($id); - $oldValues = $model->toArray(); - $model->update($update); - $this->logService->logUpdated( - $model, - $oldValues, - 'parking_pattern_space.update' - ); + $model = $this->updateData($id, $data['parking_space_type']); DB::commit(); return $model; } catch (Exception $e) { @@ -97,6 +93,30 @@ class ParkingPatternSpaceService extends BaseService } } + // 更新车位类型 + public function updateData($id, $parking_space_type) + { + $update = [ + 'space_type_id' => $parking_space_type, + 'updated_at' => get_datetime() + ]; + $model = ParkingPatternSpace::query()->findOrFail($id); + $oldValues = $model->toArray(); + $model->update($update); + $this->logService->logUpdated( + $model, + $oldValues, + 'parking_pattern_space.update' + ); + // 同步更新车位类型 + (new ParkingSpaceService($this->logService))->syncUpdateSpaceType( + $oldValues['space_id'], + $update['space_type_id'], + $oldValues['pattern_id'] + ); + return $model; + } + public function deleteModel(int $id): bool { try { diff --git a/app/Services/ParkingSpaceService.php b/app/Services/ParkingSpaceService.php index 48bc022..aea190d 100644 --- a/app/Services/ParkingSpaceService.php +++ b/app/Services/ParkingSpaceService.php @@ -268,4 +268,40 @@ class ParkingSpaceService extends BaseService throw $e; } } + + /** + * 根据不同模式 更新车位类型 + * @param $id + * @param $type_id + * @param $pattern_id + * @param bool $is_target_mode // 是否判断是不是当前模式 + * @return void + */ + public function syncUpdateSpaceType( + $id, + $type_id, + $pattern_id, + bool $is_target_mode = true + ) { + // 跳过判断 + if ($is_target_mode) { + // 非当前模式不更新 + $targetPatternId = EventCalendarService::getTargetModeId(); + if ($pattern_id != $targetPatternId) { + return; + } + } + $model = ParkingSpace::query()->findOrFail($id); + $oldValues = $model->toArray(); + // 相同不更新 + if ($oldValues['space_type_id'] == $type_id) { + return; + } + $updateData = [ + 'space_type_id' => $type_id, + 'updated_at' => get_datetime() + ]; + $model->update($updateData); + $this->logService->logUpdated($model, $oldValues, 'space_type.update'); + } }