From 955389dd2d1eddb19f6b0944211bef9de78905f9 Mon Sep 17 00:00:00 2001 From: cmz Date: Wed, 4 Sep 2024 20:17:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E8=B6=85=E6=97=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/Exporter/StudentExporter.php | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/app/Admin/Extensions/Exporter/StudentExporter.php b/app/Admin/Extensions/Exporter/StudentExporter.php index 932d83d..72410ab 100644 --- a/app/Admin/Extensions/Exporter/StudentExporter.php +++ b/app/Admin/Extensions/Exporter/StudentExporter.php @@ -4,8 +4,10 @@ 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; @@ -75,16 +77,35 @@ class StudentExporter extends AbstractExporter implements WithMapping, WithHeadi 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" => "" - ]; - } + $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"] + ]; - return $nameInfo->toArray(); } }