Browse Source

模式切换车位属性切换,车位属性同步更新

master
wanghongjun 4 weeks ago
parent
commit
6c38607f08
  1. 30
      app/Console/Commands/AutomaticallyStartActivity.php
  2. 47
      app/Services/EventCalendarService.php
  3. 22
      app/Services/ParkingPatternService.php
  4. 42
      app/Services/ParkingPatternSpaceService.php
  5. 36
      app/Services/ParkingSpaceService.php

30
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);
}
}
}

47
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
);
}
}
}

22
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();

42
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 {

36
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');
}
}

Loading…
Cancel
Save