海工商新版后台
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.
 
 

111 lines
3.3 KiB

<?php
namespace App\Admin\Extensions\Exporter;
use App\Models\SecondaryCollege;
use App\Models\Speciality;
use Dcat\Admin\Grid\Exporters\AbstractExporter;
use Illuminate\Support\Facades\Cache;
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)
{
$value = Cache::store('file')->remember('specialityInfo', 600, function () {
//所有专业
$specialityList = Speciality::query()->get(["id", "speciality_name", "secondary_college_id"])->toArray();
if(!empty($specialityList)){
//所有学院
$secondaryCollegeList = SecondaryCollege::query()->get(["id", "name"])->toArray();
if(!empty($secondaryCollegeList)){
$secondaryCollegeList = array_column($secondaryCollegeList, null, "id");
foreach($specialityList as $key => $item){
if(array_key_exists($item["secondary_college_id"], $secondaryCollegeList)){
$specialityList[$key]["name"] = $secondaryCollegeList[$item['secondary_college_id']]['name'];
}else{
$specialityList[$key]["name"] = "";
}
}
}
return array_column($specialityList, null, "id");
}
});
return [
"name" => $value[$id]["name"],
"speciality_name" => $value[$id]["speciality_name"]
];
}
}