usePaginate(true); $export = $model->filter()->input('_export_'); $secondary_college_id = $model->filter()->input('secondary_college_id'); $speciality_id = $model->filter()->input('speciality_id'); // 获取每页显示行数 $perPage = $model->getPerPage(); $table1 = UsersMember::query()->getModel()->getTable(); $table2 = UserFollowRecord::query()->getModel()->getTable(); $showInfo = AdminFollowShow::query()->where(['user_id' => Admin::user()['id']])->first(); // 新生数据 $studentsIds = AdmissionNewStudents::query()->where(["is_new_student" => "1"])->pluck('idCard')->toArray(); // 回访状态 $followStatus = UserFollowStatus::query()->orderBy('id', 'asc')->get()->toArray(); $collegesName = SecondaryCollege::query()->pluck('name', 'id')->toArray(); $data = Model::query() ->where(function ($query) use ($showInfo) { if ($showInfo) { if ($showInfo['type'] == '1') { $query->whereIn('secondary_college_id', $showInfo['secondary_college_id'] ?? []); } else { $query->whereIn('id', $showInfo['speciality_id'] ?? []); } } else { $query->where('id', 0); } }) ->when($secondary_college_id, function ($query) use ($secondary_college_id) { $query->where('secondary_college_id', $secondary_college_id); }) ->when($speciality_id, function ($query) use ($speciality_id) { $query->where('id', $speciality_id); }) ->orderByDesc('id') ->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; // 各专业录取人数 $enroll_all = UsersMember::query() ->where(['enroll_status' => '1']) ->whereIn("idcard", $studentsIds) ->where('speciality_id', $value['id']) ->select('sex', DB::raw('count(*) as total')) ->groupBy('sex') ->pluck('total', 'sex') ->toArray(); $all['data'][$key]["enroll_number"] = array_sum($enroll_all); $all['data'][$key]["enroll_man_student"] = $enroll_all[1] ?? 0; $all['data'][$key]["enroll_girl_student"] = $enroll_all[2] ?? 0; $all['data'][$key]["out_number"] = array_sum($enroll_all); $all['data'][$key]["out_man_student"] = $enroll_all[1] ?? 0; $all['data'][$key]["out_girl_student"] = $enroll_all[2] ?? 0; $total = DB::table("{$table2} as aa") ->leftJoin("{$table1} as bb", function ($join) { $join->on("aa.unique_number", "=", "bb.unique_number"); }) ->where(['aa.is_abandon' => '1', 'bb.speciality_id' => $value['id']]) ->whereIn('bb.idcard', $studentsIds) ->select('aa.follow_id', 'bb.sex', DB::raw('COUNT(*) as total')) ->groupBy('aa.follow_id', 'bb.sex') ->groupBy("bb.sex") ->get() ->toArray(); $totalData = []; foreach ($total as $key2 => $value2) { $totalData["{$value2->follow_id}{$value2->sex}"] = $value2->total; } foreach ($followStatus as $key3 => $value3) { $followId = $value3['id']; $man = 1; $girl = 2; $all['data'][$key]["total_number{$followId}"] = ($totalData["{$followId}{$man}"] ?? 0) + ($totalData["{$followId}{$girl}"] ?? 0); $all['data'][$key]["man_student{$followId}"] = $totalData["{$followId}{$man}"] ?? 0; $all['data'][$key]["girl_student{$followId}"] = $totalData["{$followId}{$girl}"] ?? 0; $all['data'][$key]["out_number"] -= $all['data'][$key]["total_number{$followId}"]; $all['data'][$key]["out_man_student"] -= $all['data'][$key]["man_student{$followId}"]; $all['data'][$key]["out_girl_student"] -= $all['data'][$key]["girl_student{$followId}"]; } unset($all['data'][$key]['id']); } return $model->makePaginator( $all['total'] ?? 0, // 传入总记录数 $all['data'] ?? [] // 传入数据二维数组 ); } }