Files
laravel_swoole/app/Services/Auth/ImportExportService.php
2026-02-08 22:38:13 +08:00

202 lines
5.0 KiB
PHP

<?php
namespace App\Services\Auth;
use App\Models\Auth\User;
use App\Models\Auth\Department;
use App\Models\Auth\Role;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\UserExport;
use App\Exports\DepartmentExport;
use App\Imports\UserImport;
use App\Imports\DepartmentImport;
class ImportExportService
{
/**
* 下载用户导入模板
*/
public function downloadUserTemplate(): string
{
$filename = 'user_import_template_' . date('YmdHis') . '.xlsx';
$path = storage_path('app/exports/' . $filename);
// 确保目录存在
if (!is_dir(dirname($path))) {
mkdir(dirname($path), 0755, true);
}
// 使用模板数据创建Excel
$templateData = [
[
'用户名*',
'密码*',
'真实姓名*',
'邮箱',
'手机号',
'部门名称',
'角色名称(多个用逗号分隔)',
'备注',
],
[
'test001',
'123456',
'测试用户001',
'test001@example.com',
'13800138001',
'技术部',
'管理员',
'示例数据',
],
];
Excel::store(new \App\Exports\GenericExport($templateData), 'exports/' . $filename);
return $filename;
}
/**
* 下载部门导入模板
*/
public function downloadDepartmentTemplate(): string
{
$filename = 'department_import_template_' . date('YmdHis') . '.xlsx';
// 确保目录存在
if (!is_dir(storage_path('app/exports'))) {
mkdir(storage_path('app/exports'), 0755, true);
}
$templateData = [
[
'部门名称*',
'上级部门名称',
'负责人',
'联系电话',
'排序',
'备注',
],
[
'前端开发组',
'技术部',
'张三',
'13800138001',
'1',
'示例数据',
],
];
Excel::store(new \App\Exports\GenericExport($templateData), 'exports/' . $filename);
return $filename;
}
/**
* 导出用户数据
*/
public function exportUsers(array $userIds = []): string
{
$filename = 'users_export_' . date('YmdHis') . '.xlsx';
// 确保目录存在
if (!is_dir(storage_path('app/exports'))) {
mkdir(storage_path('app/exports'), 0755, true);
}
Excel::store(new UserExport($userIds), 'exports/' . $filename);
return $filename;
}
/**
* 导出部门数据
*/
public function exportDepartments(array $departmentIds = []): string
{
$filename = 'departments_export_' . date('YmdHis') . '.xlsx';
// 确保目录存在
if (!is_dir(storage_path('app/exports'))) {
mkdir(storage_path('app/exports'), 0755, true);
}
Excel::store(new DepartmentExport($departmentIds), 'exports/' . $filename);
return $filename;
}
/**
* 导入用户数据
*/
public function importUsers(string $filePath, string $realPath): array
{
if (!file_exists($realPath)) {
throw ValidationException::withMessages([
'file' => ['文件不存在'],
]);
}
$import = new UserImport();
Excel::import($import, $realPath);
// 删除临时文件
if (file_exists($realPath)) {
unlink($realPath);
}
return [
'success_count' => $import->getSuccessCount(),
'error_count' => $import->getErrorCount(),
'errors' => $import->getErrors(),
];
}
/**
* 导入部门数据
*/
public function importDepartments(string $filePath, string $realPath): array
{
if (!file_exists($realPath)) {
throw ValidationException::withMessages([
'file' => ['文件不存在'],
]);
}
$import = new DepartmentImport();
Excel::import($import, $realPath);
// 删除临时文件
if (file_exists($realPath)) {
unlink($realPath);
}
return [
'success_count' => $import->getSuccessCount(),
'error_count' => $import->getErrorCount(),
'errors' => $import->getErrors(),
];
}
/**
* 获取导出文件路径
*/
public function getExportFilePath(string $filename): string
{
return storage_path('app/exports/' . $filename);
}
/**
* 删除导出文件
*/
public function deleteExportFile(string $filename): bool
{
$path = $this->getExportFilePath($filename);
if (file_exists($path)) {
return unlink($path);
}
return true;
}
}