package com.example; import com.example.entity.Course; import java.util.List; import java.util.Map; import java.util.HashMap; import java.util.ArrayList; import java.util.stream.Collectors; /** * 课程分析类 * 不依赖Spring Boot,使用普通Java类实现 */ public class CourseAnalysis { // 无参构造方法 public CourseAnalysis() { } // 获取课程类型分布 public Map getCourseTypeDistribution() { return DatabaseUtil.getCourseTypeDistribution(); } // 获取院系课程分布 public Map getDepartmentDistribution() { return DatabaseUtil.getDepartmentDistribution(); } // 获取学分分布 public Map getCreditDistribution() { Map distribution = new HashMap<>(); List courses = DatabaseUtil.getAllCourses(); for (Course course : courses) { double credit = course.getCredit(); distribution.put(credit, distribution.getOrDefault(credit, 0) + 1); } return distribution; } // 获取热门课程 public List> getTopCourses() { return DatabaseUtil.getTopCourses(); } // 获取课程容量使用率 public List> getCourseUsageRate() { List> usageRates = new ArrayList<>(); List courses = DatabaseUtil.getAllCourses(); for (Course course : courses) { Map usageRate = new HashMap<>(); usageRate.put("courseName", course.getCourseName()); usageRate.put("capacity", course.getCapacity()); usageRate.put("enrolled", course.getEnrolled()); double rate = course.getCapacity() > 0 ? (double) course.getEnrolled() / course.getCapacity() * 100 : 0; usageRate.put("usageRate", rate); usageRates.add(usageRate); } // 按使用率排序 usageRates.sort((a, b) -> Double.compare((Double) b.get("usageRate"), (Double) a.get("usageRate"))); return usageRates; } // 获取整体统计信息 public Map getOverallStatistics() { Map statistics = new HashMap<>(); try { List allCourses = DatabaseUtil.getAllCourses(); // 总课程数 statistics.put("totalCourses", allCourses.size()); // 总学分 double totalCredits = allCourses.stream() .mapToDouble(Course::getCredit) .sum(); statistics.put("totalCredits", totalCredits); // 平均学分 double avgCredit = allCourses.isEmpty() ? 0 : totalCredits / allCourses.size(); statistics.put("averageCredit", avgCredit); // 总容量 int totalCapacity = allCourses.stream() .mapToInt(Course::getCapacity) .sum(); statistics.put("totalCapacity", totalCapacity); // 总已选人数 int totalEnrolled = allCourses.stream() .mapToInt(Course::getEnrolled) .sum(); statistics.put("totalEnrolled", totalEnrolled); // 总体使用率 double overallUsageRate = totalCapacity > 0 ? (double) totalEnrolled / totalCapacity * 100 : 0; statistics.put("overallUsageRate", overallUsageRate); // 课程类型数量 long requiredCourses = allCourses.stream() .filter(course -> "必修课".equals(course.getCourseType())) .count(); statistics.put("requiredCourses", requiredCourses); long electiveCourses = allCourses.stream() .filter(course -> "选修课".equals(course.getCourseType())) .count(); statistics.put("electiveCourses", electiveCourses); // 院系数量 long departmentCount = allCourses.stream() .map(Course::getDepartment) .distinct() .count(); statistics.put("departmentCount", departmentCount); } catch (Exception e) { System.err.println("获取整体统计信息失败:" + e.getMessage()); // 返回默认值 statistics.put("totalCourses", 0); statistics.put("totalCredits", 0.0); statistics.put("averageCredit", 0.0); statistics.put("totalCapacity", 0); statistics.put("totalEnrolled", 0); statistics.put("overallUsageRate", 0.0); statistics.put("requiredCourses", 0); statistics.put("electiveCourses", 0); statistics.put("departmentCount", 0); } return statistics; } // 获取按院系分组的课程统计 public Map> getDepartmentStatistics() { Map> deptStats = new HashMap<>(); try { List allCourses = DatabaseUtil.getAllCourses(); // 按院系分组 Map> coursesByDept = allCourses.stream() .collect(Collectors.groupingBy(Course::getDepartment)); for (Map.Entry> entry : coursesByDept.entrySet()) { String department = entry.getKey(); List deptCourses = entry.getValue(); Map stats = new HashMap<>(); stats.put("courseCount", deptCourses.size()); double deptCredits = deptCourses.stream() .mapToDouble(Course::getCredit) .sum(); stats.put("totalCredits", deptCredits); int deptCapacity = deptCourses.stream() .mapToInt(Course::getCapacity) .sum(); stats.put("totalCapacity", deptCapacity); int deptEnrolled = deptCourses.stream() .mapToInt(Course::getEnrolled) .sum(); stats.put("totalEnrolled", deptEnrolled); double deptUsageRate = deptCapacity > 0 ? (double) deptEnrolled / deptCapacity * 100 : 0; stats.put("usageRate", deptUsageRate); deptStats.put(department, stats); } } catch (Exception e) { System.err.println("获取院系统计信息失败:" + e.getMessage()); } return deptStats; } // 获取教师课程统计 public Map> getTeacherStatistics() { Map> teacherStats = new HashMap<>(); try { List allCourses = DatabaseUtil.getAllCourses(); // 按教师分组 Map> coursesByTeacher = allCourses.stream() .collect(Collectors.groupingBy(Course::getTeacher)); for (Map.Entry> entry : coursesByTeacher.entrySet()) { String teacher = entry.getKey(); List teacherCourses = entry.getValue(); Map stats = new HashMap<>(); stats.put("courseCount", teacherCourses.size()); double totalCredits = teacherCourses.stream() .mapToDouble(Course::getCredit) .sum(); stats.put("totalCredits", totalCredits); int totalEnrolled = teacherCourses.stream() .mapToInt(Course::getEnrolled) .sum(); stats.put("totalEnrolled", totalEnrolled); teacherStats.put(teacher, stats); } } catch (Exception e) { System.err.println("获取教师统计信息失败:" + e.getMessage()); } return teacherStats; } // 获取课程容量利用率分析 public Map getCapacityAnalysis() { Map analysis = new HashMap<>(); try { List allCourses = DatabaseUtil.getAllCourses(); int totalCapacity = allCourses.stream() .mapToInt(Course::getCapacity) .sum(); int totalEnrolled = allCourses.stream() .mapToInt(Course::getEnrolled) .sum(); int availableCapacity = totalCapacity - totalEnrolled; // 计算不同使用率区间的课程数量 long highUsage = allCourses.stream() .filter(course -> course.getCapacity() > 0 && (double) course.getEnrolled() / course.getCapacity() >= 0.9) .count(); long mediumUsage = allCourses.stream() .filter(course -> course.getCapacity() > 0 && (double) course.getEnrolled() / course.getCapacity() >= 0.5 && (double) course.getEnrolled() / course.getCapacity() < 0.9) .count(); long lowUsage = allCourses.stream() .filter(course -> course.getCapacity() > 0 && (double) course.getEnrolled() / course.getCapacity() < 0.5) .count(); analysis.put("totalCapacity", totalCapacity); analysis.put("totalEnrolled", totalEnrolled); analysis.put("availableCapacity", availableCapacity); analysis.put("highUsageCourses", highUsage); analysis.put("mediumUsageCourses", mediumUsage); analysis.put("lowUsageCourses", lowUsage); } catch (Exception e) { System.err.println("获取容量分析失败:" + e.getMessage()); } return analysis; } }