海工商新版后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

90 lines
2.4 KiB

<?php
namespace App\Admin\Extensions\Exporter;
use App\Models\Speciality;
use Dcat\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
class StudentExporter extends AbstractExporter implements WithMapping, WithHeadings, FromCollection
{
use Exportable;
protected $fileName = '注册用户';
protected $titles = [];
public function __construct()
{
$this->fileName = $this->fileName.'_'.time().'.xlsx';//拼接下载文件名称
$this->titles = [
'mobile' => '手机号' ,
'name'=>'姓名',
'idcard'=>'身份证',
'sex' => "性别",
"college" => "二级学院",
"speciality" => "专业",
"create_time" => "注册时间",
"is_test" => "是否为测试号"
];
parent::__construct();
}
public function export()
{
// TODO: Implement export() method.
$this->download($this->fileName)->prepare(request())->send();
exit;
}
public function collection()
{
// TODO: Implement collection() method.
return collect($this->buildData());
}
public function headings(): array
{
// TODO: Implement headings() method.
return $this->titles();
}
public function map($row): array
{
$nameInfo = $this->getSpecialityAndCollegeNameById($row["speciality_id"]);
$sex = $row['sex'] == 1 ? "" : "";
$test = $row['is_test'] == 1 ? "" : "";
// TODO: Implement map() method.
return [
$row['mobile'],
$row['name'],
"'".$row['idcard'].'',
$sex,
$nameInfo["name"],
$nameInfo["speciality_name"],
date("Y-m-d H:i:s", $row['create_time']),
$test
];
}
public function getSpecialityAndCollegeNameById($id)
{
$nameInfo = Speciality::query()->where("speciality.id", $id)->leftJoin("secondary_college as b", "speciality.secondary_college_id", "=", "b.id")->first();
if(empty($nameInfo)){
return [
"name" => "",
"speciality_name" => ""
];
}
return $nameInfo->toArray();
}
}