diff --git a/app/Http/Controllers/Admin/ParkingSpaceRepairController.php b/app/Http/Controllers/Admin/ParkingSpaceRepairController.php index d9f89b1..3ffb61d 100644 --- a/app/Http/Controllers/Admin/ParkingSpaceRepairController.php +++ b/app/Http/Controllers/Admin/ParkingSpaceRepairController.php @@ -292,13 +292,7 @@ class ParkingSpaceRepairController extends BaseController } $validator = Validator::make($data, $rules, $messages); - $start_at_times = strtotime($data['start_at']); - $end_at_times = strtotime($data['end_at']); - if ($start_at_times >= $end_at_times) { - throw new CustomException( - __validation('parking_repair_list.date_error') - ); - } + $this->validateDate($data['start_at'], $data['end_at']); if ($validator->fails()) { throw new ValidationException($validator); @@ -462,13 +456,54 @@ class ParkingSpaceRepairController extends BaseController } /** + * 维修开始时间不能大于结束时间 + * @param $start_at + * @param $end_at + * @return void + * @throws CustomException + */ + protected function validateDate($start_at, $end_at): void + { + $start_at_times = strtotime($start_at); + $end_at_times = strtotime($end_at); + if ($start_at_times >= $end_at_times) { + throw new CustomException( + __validation('parking_repair_list.date_error') + ); + } + } + + /** + * @param Request $request * @return JsonResponse * @throws CustomException * @throws ValidationException */ - public function synchronizeList(): JsonResponse + public function synchronizeList(Request $request): JsonResponse { try { + $data = $request->all(); + $rules = [ + 'start_at' => 'required', + 'end_at' => 'required' + ]; + $messages = [ + 'start_at.required' => __validation( + 'parking_repair_list.s_empty' + ), + 'end_at.required' => __validation( + 'parking_repair_list.e_empty' + ) + ]; + $validator = Validator::make($data, $rules, $messages); + + if ($validator->fails()) { + throw new ValidationException($validator); + } + + $this->validateDate($data['start_at'], $data['end_at']); + + $this->service->synchronize($data); return $this->responseService->success( null, __('admin.operation_successful') diff --git a/app/Services/ParkingSpaceRepairService.php b/app/Services/ParkingSpaceRepairService.php index b41beb7..ddd8b5f 100644 --- a/app/Services/ParkingSpaceRepairService.php +++ b/app/Services/ParkingSpaceRepairService.php @@ -209,4 +209,57 @@ class ParkingSpaceRepairService extends BaseService throw $e; } } + + /** + * @param $data + * @return bool + * @throws CustomException + */ + public function synchronize($data): bool + { + try { + DB::beginTransaction(); + + $start_at_times = strtotime($data['start_at']); + $start_at = get_datetime('datetime', $start_at_times); + $end_at_times = strtotime($data['end_at']); + $end_at = get_datetime('datetime', $end_at_times); + + $newValues = $oldValues = ParkingSpaceRepair::query()->where( + function ($query) use ($start_at, $end_at) { + $query->orWhereRaw("'{$start_at}' BETWEEN start_at AND end_at"); + $query->orWhereRaw("'{$end_at}' BETWEEN start_at AND end_at"); + } + )->where('sync_status', 0)->select()->get()->toArray(); + if (!$oldValues) { + throw new CustomException(__service($this->menuTitle . '.not_send'));//目前暂无可发送的维修车位信息 + } + + $ids = array_column($oldValues, 'id'); + ParkingSpaceRepair::query()->whereIn('id', $ids)->update([ + 'start_at' => $start_at, + 'end_at' => $end_at, + 'sync_status' => 1, + 'sync_at' => $start_at, + 'updated_at' => get_datetime() + ]); + $newValues['start_at'] = $start_at; + $newValues['end_at'] = $end_at; + $newValues['sync_status'] = 1; + $newValues['sync_at'] = $start_at; + + $this->logService->logUpdatedData( + new ParkingSpaceRepair(), + $oldValues, + $this->menuTitle . '.update', + $newValues + ); + + DB::commit(); + return true; + } catch (Exception $e) { + DB::rollBack(); + throw $e; + } + } } diff --git a/resources/lang/en/service.php b/resources/lang/en/service.php index 1d94588..4224c8d 100644 --- a/resources/lang/en/service.php +++ b/resources/lang/en/service.php @@ -171,7 +171,8 @@ return [ 'parking_repair_list' => [ 'not_synced' => 'Not synced', 'synced' => 'Synchronized', - 'space_not_exists' => 'The parking number does not exist' + 'space_not_exists' => 'The parking number does not exist', + 'not_send' => 'There is currently no information available for sending maintenance parking space information.' ], 'gate_control' => [ 'automatic' => 'Automatic entry', diff --git a/resources/lang/zh-CN/service.php b/resources/lang/zh-CN/service.php index c97475e..a9875b1 100644 --- a/resources/lang/zh-CN/service.php +++ b/resources/lang/zh-CN/service.php @@ -171,7 +171,8 @@ return [ 'parking_repair_list' => [ 'not_synced' => '未同步', 'synced' => '已同步', - 'space_not_exists' => '車位號不存在' + 'space_not_exists' => '车位号不存在', + 'not_send' => '目前暂无可发送的维修车位信息' ], 'gate_control' => [ 'automatic' => '自动入场', diff --git a/resources/lang/zh-TW/service.php b/resources/lang/zh-TW/service.php index 37f7302..50b5173 100644 --- a/resources/lang/zh-TW/service.php +++ b/resources/lang/zh-TW/service.php @@ -171,7 +171,8 @@ return [ 'parking_repair_list' => [ 'not_synced' => '未同步', 'synced' => '已同步', - 'space_not_exists' => '车位号不存在' + 'space_not_exists' => '車位號不存在', + 'not_send' => '目前暫無可發送的維修車位信息' ], 'gate_control' => [ 'automatic' => '自動入場',