Files
laravel_swoole/app/Exports/DepartmentExport.php
2026-02-08 22:38:13 +08:00

68 lines
1.6 KiB
PHP

<?php
namespace App\Exports;
use App\Models\Auth\Department;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class DepartmentExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize
{
protected $departmentIds;
public function __construct(array $departmentIds = [])
{
$this->departmentIds = $departmentIds;
}
/**
* 获取数据集合
*/
public function collection()
{
$query = Department::query();
if (!empty($this->departmentIds)) {
$query->whereIn('id', $this->departmentIds);
}
return $query->get();
}
/**
* 设置表头
*/
public function headings(): array
{
return [
'ID',
'部门名称',
'上级部门',
'负责人',
'联系电话',
'排序',
'状态',
'创建时间',
];
}
/**
* 映射数据
*/
public function map($department): array
{
return [
$department->id,
$department->name,
$department->parent_id ? Department::find($department->parent_id)?->name : '',
$department->leader,
$department->phone,
$department->sort,
$department->status == 1 ? '启用' : '禁用',
$department->created_at ? $department->created_at->toDateTimeString() : '',
];
}
}