From 90489936fe6ca0e2af3a21217c051a47da1e0dda Mon Sep 17 00:00:00 2001 From: Hjj <126586545@qq.com> Date: Wed, 2 Jul 2025 17:00:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=A2=84=E6=8A=A5=E5=88=B0?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UserFollowOverviewController.php | 11 +-- .../Exporter/FollowStatisticsExporter.php | 73 +++++++++++++++++++ app/Admin/Repositories/UserFollowOverview.php | 21 ++++-- 3 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 app/Admin/Extensions/Exporter/FollowStatisticsExporter.php diff --git a/app/Admin/Controllers/UserFollowOverviewController.php b/app/Admin/Controllers/UserFollowOverviewController.php index 6192d5b..8a8b2d0 100644 --- a/app/Admin/Controllers/UserFollowOverviewController.php +++ b/app/Admin/Controllers/UserFollowOverviewController.php @@ -2,6 +2,7 @@ namespace App\Admin\Controllers; +use App\Admin\Extensions\Exporter\FollowStatisticsExporter; use App\Admin\Metrics\Examples\Follow\Ajax\AllCollegesFollowChartsAjax; use App\Admin\Metrics\Examples\Follow\AllColleges; use App\Admin\Metrics\Examples\Follow\AllCollegesCharts; @@ -70,18 +71,18 @@ class UserFollowOverviewController extends AdminController $grid->disableActions(); $grid->disableRefreshButton(); $grid->disableRowSelector(); - $grid->disableToolbar(); $grid->paginate(10); + $grid->export(new FollowStatisticsExporter()); - $followStatus = UserFollowStatus::query()->get()->toArray(); + $followStatus = UserFollowStatus::query()->orderBy('id', 'asc')->get()->toArray(); $showInfo = AdminFollowShow::query()->where(['user_id' => Admin::user()['id']])->first(); $collegeAll = SecondaryCollege::query()->whereIn('id', $showInfo['secondary_college_id'] ?? [])->pluck("name", "id")->toArray(); - $grid->combine('总录取', ['enroll_number', 'enroll_man_student', 'enroll_girl_student']); - $grid->combine('未联系', ['out_number', 'out_man_student', 'out_girl_student']); + $grid->addTableClass(['table-text-center'])->combine('总录取', ['enroll_number', 'enroll_man_student', 'enroll_girl_student']); + $grid->addTableClass(['table-text-center'])->combine('未联系', ['out_number', 'out_man_student', 'out_girl_student']); foreach ($followStatus as $k => $v) { - $grid->combine($v['name'], ["total_number{$v['id']}", "man_student{$v['id']}", "girl_student{$v['id']}"]); + $grid->addTableClass(['table-text-center'])->combine($v['name'], ["total_number{$v['id']}", "man_student{$v['id']}", "girl_student{$v['id']}"]); } $grid->column('secondary_college_id', '二级学院'); diff --git a/app/Admin/Extensions/Exporter/FollowStatisticsExporter.php b/app/Admin/Extensions/Exporter/FollowStatisticsExporter.php new file mode 100644 index 0000000..73e6e1b --- /dev/null +++ b/app/Admin/Extensions/Exporter/FollowStatisticsExporter.php @@ -0,0 +1,73 @@ +fileName = '新生回访统计数据-' . date('Y-m-d') . '.xlsx'; //拼接下载文件名称 + $this->titles = ['学院基础信息', '', '总录取', '', '', '未联系', '', '']; + foreach (UserFollowStatus::query()->orderBy('id', 'asc')->get('name')->toArray() as $key => $item) { + $this->titles[] = $item['name']; + $this->titles[] = ''; + $this->titles[] = ''; + } + parent::__construct(); + } + + + public function export() + { + // TODO: Implement export() method. + $this->download($this->fileName)->prepare(request())->send(); + exit; + } + + public function collection() + { + $data = $this->buildData(); + $newData[2] = ['二级学院', '专业', '总人数', '男生', '女生', '总人数', '男生', '女生']; + foreach (UserFollowStatus::query()->get('id')->toArray() as $key => $item) { + $newData[2][] = '总人数'; + $newData[2][] = '男生'; + $newData[2][] = '女生'; + } + foreach ($data as $key => $item) { + $newData[] = $item; + } + // TODO: Implement collection() method. + return collect($newData); + } + + public function headings(): array + { + // TODO: Implement headings() method. + return $this->titles; + } + + public function map($row): array + { + // 直接返回row 导出全部字段 dd($row); + // return $row; + foreach ($row as $k => $v) { + $row[$k] = (string)$v; + } + + // TODO: Implement map() method. + return $row; + } +} diff --git a/app/Admin/Repositories/UserFollowOverview.php b/app/Admin/Repositories/UserFollowOverview.php index 78b1394..08b8563 100644 --- a/app/Admin/Repositories/UserFollowOverview.php +++ b/app/Admin/Repositories/UserFollowOverview.php @@ -13,6 +13,7 @@ use Dcat\Admin\Admin; use Dcat\Admin\Grid; use Dcat\Admin\Repositories\EloquentRepository; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; class UserFollowOverview extends EloquentRepository { @@ -25,6 +26,8 @@ class UserFollowOverview extends EloquentRepository public function get(Grid\Model $model) { + $model->usePaginate(true); + $export = $model->filter()->input('_export_'); $secondary_college_id = $model->filter()->input('secondary_college_id'); $speciality_id = $model->filter()->input('speciality_id'); @@ -37,9 +40,9 @@ class UserFollowOverview extends EloquentRepository // 新生数据 $studentsIds = AdmissionNewStudents::query()->where(["is_new_student" => "1"])->pluck('idCard')->toArray(); // 回访状态 - $followStatus = UserFollowStatus::query()->get()->toArray(); + $followStatus = UserFollowStatus::query()->orderBy('id', 'asc')->get()->toArray(); $collegesName = SecondaryCollege::query()->pluck('name', 'id')->toArray(); - $all = Model::query() + $data = Model::query() ->where(function ($query) use ($showInfo) { if ($showInfo) { if ($showInfo['type'] == '1') { @@ -58,9 +61,15 @@ class UserFollowOverview extends EloquentRepository $query->where('id', $speciality_id); }) ->orderByDesc('id') - ->paginate($perPage) - ->toArray(); - + ->select(['id', 'secondary_college_id', 'speciality_name']); + + if ($export == 'all') { + $data = $data->get()->toArray(); + $all['total'] = count($data); + $all['data'] = $data; + } else { + $all = $data->paginate($perPage)->toArray(); + } foreach ($all['data'] as $key => $value) { $all['data'][$key]["secondary_college_id"] = $collegesName[$value['secondary_college_id']] ?? null; @@ -112,6 +121,8 @@ class UserFollowOverview extends EloquentRepository $all['data'][$key]["out_girl_student"] -= $all['data'][$key]["girl_student{$followId}"]; } + + unset($all['data'][$key]['id']); } return $model->makePaginator(