<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, java.util.Map, java.util.HashMap" %>
<%
    String studentId = "12345";
    String teacherId = request.getParameter("teacher");
    String courseId = request.getParameter("course");

    if (teacherId != null && courseId != null) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "SchoolST", "123456");

            String sql = "SELECT d.dimension_id, d.weight, COUNT(q.question_id) AS question_count " +
                    "FROM dimensions d " +
                    "LEFT JOIN questions q ON d.dimension_id = q.dimension_id " +
                    "GROUP BY d.dimension_id";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            Map<Integer, Float> dimensionWeights = new HashMap<>();
            Map<Integer, Integer> dimensionQuestionCounts = new HashMap<>();
            while (rs.next()) {
                int dimensionId = rs.getInt("dimension_id");
                dimensionWeights.put(dimensionId, rs.getFloat("weight"));
                dimensionQuestionCounts.put(dimensionId, rs.getInt("question_count"));
            }
            rs.close();
            stmt.close();

            Map<Integer, Integer> questionToDimension = new HashMap<>();
            sql = "SELECT question_id, dimension_id FROM questions";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                questionToDimension.put(rs.getInt("question_id"), rs.getInt("dimension_id"));
            }
            rs.close();
            stmt.close();

            Map<Integer, Float> dimensionScores = new HashMap<>();
            for (String paramName : request.getParameterMap().keySet()) {
                if (paramName.startsWith("question_")) {
                    String questionId = paramName.replace("question_", "");
                    int qId = Integer.parseInt(questionId);
                    int dimensionId = questionToDimension.get(qId);
                    int answer = Integer.parseInt(request.getParameter(paramName));

                    dimensionScores.put(dimensionId, dimensionScores.getOrDefault(dimensionId, 0f) + answer);
                }
            }

            float totalScore = 0;
            for (Map.Entry<Integer, Float> entry : dimensionScores.entrySet()) {
                int dimensionId = entry.getKey();
                float scoreSum = entry.getValue();
                float weight = dimensionWeights.get(dimensionId);
                int questionCount = dimensionQuestionCounts.get(dimensionId);

                float dimensionScore = (scoreSum / questionCount) * (weight / 100) * 100;
                totalScore += dimensionScore;
            }

            sql = "INSERT INTO evaluations (student_id, teacher_id, course_id, total_score) VALUES (?, ?, ?, ?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, studentId);
            pstmt.setString(2, teacherId);
            pstmt.setString(3, courseId);
            pstmt.setFloat(4, totalScore);
            pstmt.executeUpdate();
            pstmt.close();

            conn.close();
            out.println("提交成功！");
        } catch (Exception e) {
            e.printStackTrace();
            out.println("评价提交失败！错误信息：" + e.getMessage());
        }
    } else {
        out.println("请填写完整的评价信息！");
    }
%>