Browse Source

活动行事历导入错误返回

master
wanghongjun 2 days ago
parent
commit
8e68fc8e12
  1. 14
      app/Http/Controllers/Admin/EventCalendarController.php
  2. 43
      app/Imports/EventCalendarImport.php
  3. 18
      app/common.php
  4. 18
      resources/lang/en/imports.php
  5. 18
      resources/lang/zh-CN/imports.php
  6. 18
      resources/lang/zh-TW/imports.php

14
app/Http/Controllers/Admin/EventCalendarController.php

@ -376,18 +376,20 @@ class EventCalendarController extends BaseController
// 4. 执行导入(使用存储后的绝对路径) // 4. 执行导入(使用存储后的绝对路径)
// storage_path('app') 获取 storage/app 的绝对路径 // storage_path('app') 获取 storage/app 的绝对路径
Excel::import( $model = new EventCalendarImport($this->adminUserId);
new EventCalendarImport($this->adminUserId), Excel::import($model, storage_path('app/' . $path));
storage_path('app/' . $path) $error_arr = $model->errorArr();
);
// 5. (可选)导入完成后删除临时文件 // 5. (可选)导入完成后删除临时文件
Storage::delete($path); Storage::delete($path);
// 6. 返回错误行
if ($error_arr) {
throw new CustomException(implode('\\n', $error_arr));
}
return $this->responseService->success( return $this->responseService->success(
__('controller.import.success') __('controller.import.success')
); );
} catch (ValidationException $e) { } catch (ValidationException|CustomException $e) {
throw $e; throw $e;
} catch (Exception $e) { } catch (Exception $e) {
return $this->responseService->systemError( return $this->responseService->systemError(

43
app/Imports/EventCalendarImport.php

@ -13,6 +13,7 @@ class EventCalendarImport implements ToModel
protected string $user_id; protected string $user_id;
protected OperationLogService $logService; protected OperationLogService $logService;
protected int $index = 1; protected int $index = 1;
protected array $error = [];
public function __construct(string $user_id) public function __construct(string $user_id)
{ {
@ -37,37 +38,51 @@ class EventCalendarImport implements ToModel
$end_date = $row[4]; $end_date = $row[4];
$end_time = $row[5]; $end_time = $row[5];
if (empty($pattern_name)) { if (empty($pattern_name)) {
return ; $this->error[] = imports_error($this->index, 'import3');
return;
} }
$pattern_id = ParkingPattern::getId($pattern_name); $pattern_id = ParkingPattern::getId($pattern_name);
if (!$pattern_id) { if (!$pattern_id) {
$pattern_id = AdminTranslationService::getTypeId(10, $pattern_name); $pattern_id = AdminTranslationService::getTypeId(10, $pattern_name);
if (!$pattern_id) { if (!$pattern_id) {
$this->error[] = imports_error($this->index, 'import4');
return; return;
} }
} }
if (EventCalendar::query()->whereIn('status', [0, 1])->where( if (empty($start_date)) {
'pattern_id', $this->error[] = imports_error($this->index, 'import5');
$pattern_id
)
->exists()
) {
return ; return ;
} }
$start_date_times = strtotime($start_date); $start_date_times = strtotime($start_date);
$start_times = strtotime($start_time);
$end_date_times = strtotime($end_date);
$end_times = strtotime($end_time);
if (!$start_date_times) { if (!$start_date_times) {
$this->error[] = imports_error($this->index, 'import6');
return ; return ;
} }
if (empty($start_time)) {
$this->error[] = imports_error($this->index, 'import7');
return ;
}
$start_times = strtotime($start_time);
if (!$start_times) { if (!$start_times) {
$this->error[] = imports_error($this->index, 'import8');
return ; return ;
} }
if (empty($end_date)) {
$this->error[] = imports_error($this->index, 'import9');
return ;
}
$end_date_times = strtotime($end_date);
if (!$end_date_times) { if (!$end_date_times) {
$this->error[] = imports_error($this->index, 'import10');
return ;
}
if (empty($end_time)) {
$this->error[] = imports_error($this->index, 'import11');
return ; return ;
} }
$end_times = strtotime($end_time);
if (!$end_times) { if (!$end_times) {
$this->error[] = imports_error($this->index, 'import12');
return ; return ;
} }
$start_date = date("Y-m-d", $start_date_times); $start_date = date("Y-m-d", $start_date_times);
@ -79,12 +94,14 @@ class EventCalendarImport implements ToModel
// 开始时间不能大于结束时间 // 开始时间不能大于结束时间
if (strtotime($save_start_time) > strtotime($save_end_time)) { if (strtotime($save_start_time) > strtotime($save_end_time)) {
$this->error[] = imports_error($this->index, 'import13');
return; return;
} }
// 开始时间 和 结束时间段 存在活动不允许上传 // 开始时间 和 结束时间段 存在活动不允许上传
$start_time_exists = $this->validateDate($save_start_time); $start_time_exists = $this->validateDate($save_start_time);
$end_time_exists = $this->validateDate($save_end_time); $end_time_exists = $this->validateDate($save_end_time);
if ($start_time_exists || $end_time_exists) { if ($start_time_exists || $end_time_exists) {
$this->error[] = imports_error($this->index, 'import14');
return; return;
} }
@ -100,6 +117,7 @@ class EventCalendarImport implements ToModel
$model = EventCalendar::query()->create($create); $model = EventCalendar::query()->create($create);
$this->logService->logCreated($model, 'event_calendar.import'); $this->logService->logCreated($model, 'event_calendar.import');
$this->index += 1;
} }
protected function validateDate($datetime) protected function validateDate($datetime)
@ -113,4 +131,9 @@ class EventCalendarImport implements ToModel
}) })
->exists(); ->exists();
} }
public function errorArr(): array
{
return $this->error;
}
} }

18
app/common.php

@ -246,3 +246,21 @@ if (!function_exists('__exports')) {
return __('exports.' . $str); return __('exports.' . $str);
} }
} }
if (!function_exists('__imports')) {
function __imports($str): string
{
return __('imports.' . $str);
}
}
if (!function_exists('imports_error')) {
function imports_error($index, $str): string
{
$str1 = __imports('import1');
$str2 = __imports('import2');
$str3 = __imports($str);
return $str1 . $index . $str2 . ' : ' . $str3;
}
}

18
resources/lang/en/imports.php

@ -0,0 +1,18 @@
<?php
return [
'import1' => 'Line',
'import2' => '',
'import3' => 'The mode name cannot be empty.',
'import4' => 'The mode name does not exist.',
'import5' => 'The start date cannot be empty.',
'import6' => 'Start date data error',
'import7' => 'The start time cannot be empty.',
'import8' => 'Start time data error',
'import9' => 'The end date cannot be empty.',
'import10' => 'End date data error',
'import11' => 'The end time cannot be empty.',
'import12' => 'End time data error',
'import13' => 'The start time cannot be greater than the end time.',
'import14' => 'There is already an activity running in the current time period, so uploading is not allowed.'
];

18
resources/lang/zh-CN/imports.php

@ -0,0 +1,18 @@
<?php
return [
'import1' => '第',
'import2' => '行',
'import3' => '模式名称不能为空',
'import4' => '模式名称不存在',
'import5' => '开始日期不能为空',
'import6' => '开始日期数据错误',
'import7' => '开始时间不能为空',
'import8' => '开始时间数据错误',
'import9' => '结束日期不能为空',
'import10' => '结束日期数据错误',
'import11' => '结束时间不能为空',
'import12' => '结束时间数据错误',
'import13' => '开始时间不能大于结束时间',
'import14' => '当前时间段已有活动,不可上传'
];

18
resources/lang/zh-TW/imports.php

@ -0,0 +1,18 @@
<?php
return [
'import1' => '第',
'import2' => '行',
'import3' => '模式名稱不能為空',
'import4' => '模式名稱不存在',
'import5' => '開始日期不能為空',
'import6' => '開始日期資料錯誤',
'import7' => '開始時間不能為空',
'import8' => '開始時間資料錯誤',
'import9' => '結束日期不能為空',
'import10' => '結束日期資料錯誤',
'import11' => '結束時間不能為空',
'import12' => '結束時間資料錯誤',
'import13' => '開始時間不能大於結束時間',
'import14' => '目前時段已有活動,不可上傳'
];
Loading…
Cancel
Save