初始化项目
This commit is contained in:
67
app/Exports/DepartmentExport.php
Normal file
67
app/Exports/DepartmentExport.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?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() : '',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user