fix: 教师无法审批错误

This commit is contained in:
seahi 2024-12-16 21:40:20 +08:00
parent be0b49b3d1
commit 7fde34d238
4 changed files with 212 additions and 73 deletions

View File

@ -98,7 +98,9 @@ public class LeaveRequestDAOImpl implements LeaveRequestDAO {
@Override @Override
public LeaveRequest findById(int id) { public LeaveRequest findById(int id) {
String sql = "SELECT lr.*, s.* FROM leave_requests lr " + String sql = "SELECT lr.*, s.id as student_id, s.student_id as student_number, " +
"s.name, s.class_name, s.contact, s.college, s.major, s.is_graduating " +
"FROM leave_requests lr " +
"JOIN students s ON lr.student_id = s.id " + "JOIN students s ON lr.student_id = s.id " +
"WHERE lr.id = ?"; "WHERE lr.id = ?";
try (Connection conn = DatabaseUtil.getConnection(); try (Connection conn = DatabaseUtil.getConnection();
@ -119,7 +121,9 @@ public class LeaveRequestDAOImpl implements LeaveRequestDAO {
@Override @Override
public List<LeaveRequest> findAll() { public List<LeaveRequest> findAll() {
List<LeaveRequest> requests = new ArrayList<>(); List<LeaveRequest> requests = new ArrayList<>();
String sql = "SELECT lr.*, s.* FROM leave_requests lr " + String sql = "SELECT lr.*, s.id as student_id, s.student_id as student_number, " +
"s.name, s.class_name, s.contact, s.college, s.major, s.is_graduating " +
"FROM leave_requests lr " +
"JOIN students s ON lr.student_id = s.id"; "JOIN students s ON lr.student_id = s.id";
try (Connection conn = DatabaseUtil.getConnection(); try (Connection conn = DatabaseUtil.getConnection();
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
@ -137,7 +141,9 @@ public class LeaveRequestDAOImpl implements LeaveRequestDAO {
@Override @Override
public List<LeaveRequest> findByStudentId(int studentId) { public List<LeaveRequest> findByStudentId(int studentId) {
List<LeaveRequest> requests = new ArrayList<>(); List<LeaveRequest> requests = new ArrayList<>();
String sql = "SELECT lr.*, s.* FROM leave_requests lr " + String sql = "SELECT lr.*, s.id as student_id, s.student_id as student_number, " +
"s.name, s.class_name, s.contact, s.college, s.major, s.is_graduating " +
"FROM leave_requests lr " +
"JOIN students s ON lr.student_id = s.id " + "JOIN students s ON lr.student_id = s.id " +
"WHERE lr.student_id = ? ORDER BY lr.request_time DESC"; "WHERE lr.student_id = ? ORDER BY lr.request_time DESC";
try (Connection conn = DatabaseUtil.getConnection(); try (Connection conn = DatabaseUtil.getConnection();
@ -158,7 +164,9 @@ public class LeaveRequestDAOImpl implements LeaveRequestDAO {
@Override @Override
public List<LeaveRequest> findByStatus(ApprovalStatus status) { public List<LeaveRequest> findByStatus(ApprovalStatus status) {
List<LeaveRequest> requests = new ArrayList<>(); List<LeaveRequest> requests = new ArrayList<>();
String sql = "SELECT lr.*, s.* FROM leave_requests lr " + String sql = "SELECT lr.*, s.id as student_id, s.student_id as student_number, " +
"s.name, s.class_name, s.contact, s.college, s.major, s.is_graduating " +
"FROM leave_requests lr " +
"JOIN students s ON lr.student_id = s.id " + "JOIN students s ON lr.student_id = s.id " +
"WHERE lr.status = ? ORDER BY lr.request_time DESC"; "WHERE lr.status = ? ORDER BY lr.request_time DESC";
try (Connection conn = DatabaseUtil.getConnection(); try (Connection conn = DatabaseUtil.getConnection();
@ -211,10 +219,13 @@ public class LeaveRequestDAOImpl implements LeaveRequestDAO {
// 设置学生信息 // 设置学生信息
Student student = new Student(); Student student = new Student();
student.setId(rs.getInt("student_id")); student.setId(rs.getInt("student_id"));
student.setStudentId(rs.getString("student_id")); student.setStudentId(rs.getString("student_number"));
student.setName(rs.getString("name")); student.setName(rs.getString("name"));
student.setClassName(rs.getString("class_name")); student.setClassName(rs.getString("class_name"));
student.setContact(rs.getString("contact")); student.setContact(rs.getString("contact"));
student.setCollege(rs.getString("college"));
student.setMajor(rs.getString("major"));
student.setGraduating(rs.getBoolean("is_graduating"));
request.setStudent(student); request.setStudent(student);
Timestamp approvalTime = rs.getTimestamp("approval_time"); Timestamp approvalTime = rs.getTimestamp("approval_time");

View File

@ -13,17 +13,15 @@ import java.util.List;
public class RequestDetailDialog extends JDialog { public class RequestDetailDialog extends JDialog {
private final LeaveRequestService leaveRequestService; private final LeaveRequestService leaveRequestService;
private LeaveRequest currentRequest; private LeaveRequest currentRequest;
private Runnable onApprovalComplete; // 添加回调接口
public RequestDetailDialog(JFrame parent, String studentName) { public RequestDetailDialog(JFrame parent, int requestId, Runnable onApprovalComplete) {
super(parent, "请假详情", true); super(parent, "请假详情", true);
this.leaveRequestService = ServiceFactory.getLeaveRequestService(); this.leaveRequestService = ServiceFactory.getLeaveRequestService();
this.onApprovalComplete = onApprovalComplete;
// 根据学生姓名获取请假信息 // 根据ID获取请假信息
List<LeaveRequest> requests = leaveRequestService.getAllLeaveRequests(); this.currentRequest = leaveRequestService.getLeaveRequestById(requestId);
this.currentRequest = requests.stream()
.filter(r -> r.getStudent().getName().equals(studentName))
.findFirst()
.orElse(null);
if (currentRequest == null) { if (currentRequest == null) {
JOptionPane.showMessageDialog(parent, "未找到请假信息!", "错误", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(parent, "未找到请假信息!", "错误", JOptionPane.ERROR_MESSAGE);
@ -31,7 +29,7 @@ public class RequestDetailDialog extends JDialog {
return; return;
} }
setSize(500, 700); setSize(350, 550);
setLocationRelativeTo(parent); setLocationRelativeTo(parent);
setResizable(false); setResizable(false);
@ -49,13 +47,11 @@ public class RequestDetailDialog extends JDialog {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 学生基本信息 // 学生基本信息
addInfoField(mainPanel, gbc, "审核进度:", "班主任");
addInfoField(mainPanel, gbc, "学院:", currentRequest.getStudent().getCollege()); addInfoField(mainPanel, gbc, "学院:", currentRequest.getStudent().getCollege());
addInfoField(mainPanel, gbc, "专业:", currentRequest.getStudent().getMajor()); addInfoField(mainPanel, gbc, "专业:", currentRequest.getStudent().getMajor());
addInfoField(mainPanel, gbc, "班级:", currentRequest.getStudent().getClassName()); addInfoField(mainPanel, gbc, "班级:", currentRequest.getStudent().getClassName());
addInfoField(mainPanel, gbc, "姓名:", currentRequest.getStudent().getName()); addInfoField(mainPanel, gbc, "姓名:", currentRequest.getStudent().getName());
addInfoField(mainPanel, gbc, "学号:", currentRequest.getStudent().getStudentId()); addInfoField(mainPanel, gbc, "学号:", currentRequest.getStudent().getStudentId());
addInfoField(mainPanel, gbc, "手机号:", currentRequest.getStudent().getPhone());
// 请假信息 // 请假信息
addInfoField(mainPanel, gbc, "申请外出时间:", dateFormat.format(currentRequest.getStartTime())); addInfoField(mainPanel, gbc, "申请外出时间:", dateFormat.format(currentRequest.getStartTime()));
@ -68,10 +64,10 @@ public class RequestDetailDialog extends JDialog {
addInfoField(mainPanel, gbc, "其他特殊情况:", addInfoField(mainPanel, gbc, "其他特殊情况:",
currentRequest.getSpecialSituation() == null ? "" : currentRequest.getSpecialSituation()); currentRequest.getSpecialSituation() == null ? "" : currentRequest.getSpecialSituation());
addInfoField(mainPanel, gbc, "发起时间:", dateFormat.format(currentRequest.getRequestTime())); addInfoField(mainPanel, gbc, "发起时间:", dateFormat.format(currentRequest.getRequestTime()));
addInfoField(mainPanel, gbc, "审批状态:", currentRequest.getStatus().getDescription());
// 如果已经审批过显示审批信息 // 如果已经审批过显示审批信息
if (currentRequest.getStatus() != ApprovalStatus.PENDING) { if (currentRequest.getStatus() != ApprovalStatus.PENDING) {
addInfoField(mainPanel, gbc, "审批状态:", currentRequest.getStatus().getDescription());
if (currentRequest.getApprovalComment() != null) { if (currentRequest.getApprovalComment() != null) {
addInfoField(mainPanel, gbc, "审批意见:", currentRequest.getApprovalComment()); addInfoField(mainPanel, gbc, "审批意见:", currentRequest.getApprovalComment());
} }
@ -81,7 +77,7 @@ public class RequestDetailDialog extends JDialog {
} }
// 审批按钮 // 审批按钮
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10)); JPanel actionButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
JButton approveButton = new JButton("通过"); JButton approveButton = new JButton("通过");
JButton rejectButton = new JButton("驳回"); JButton rejectButton = new JButton("驳回");
@ -94,52 +90,116 @@ public class RequestDetailDialog extends JDialog {
approveButton.setPreferredSize(new Dimension(100, 30)); approveButton.setPreferredSize(new Dimension(100, 30));
rejectButton.setPreferredSize(new Dimension(100, 30)); rejectButton.setPreferredSize(new Dimension(100, 30));
// 设置按钮颜色 // 设置按钮样式
approveButton.setBackground(new Color(39, 174, 96)); approveButton.setBackground(new Color(39, 174, 96));
approveButton.setForeground(Color.WHITE); // approveButton.setForeground(Color.WHITE);
rejectButton.setBackground(new Color(231, 76, 60));
rejectButton.setForeground(Color.WHITE);
buttonPanel.add(approveButton); rejectButton.setBackground(new Color(231, 76, 60));
buttonPanel.add(rejectButton); // rejectButton.setForeground(Color.WHITE);
actionButtonPanel.add(approveButton);
actionButtonPanel.add(rejectButton);
// 添加按钮面板 // 添加按钮面板
gbc.insets = new Insets(20, 10, 10, 10); gbc.insets = new Insets(20, 10, 10, 10);
mainPanel.add(buttonPanel, gbc); mainPanel.add(actionButtonPanel, gbc);
// 添加事件监听 // 添加事件监听
approveButton.addActionListener(e -> { approveButton.addActionListener(e -> {
int result = JOptionPane.showConfirmDialog( // 创建一个自定义对话框
this, JDialog approveDialog = new JDialog(this, "审批意见", true);
"确定通过该请假申请吗?", approveDialog.setLayout(new BorderLayout());
"确认", approveDialog.setSize(300, 200);
JOptionPane.YES_NO_OPTION approveDialog.setLocationRelativeTo(this);
);
if (result == JOptionPane.YES_OPTION) { JPanel inputPanel = new JPanel(new BorderLayout(10, 10));
leaveRequestService.approveLeaveRequest( inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
currentRequest.getId(),
ApprovalStatus.APPROVED, JLabel label = new JLabel("请输入审批意见:");
"已通过" JTextArea commentArea = new JTextArea(4, 20);
); commentArea.setLineWrap(true);
dispose(); commentArea.setWrapStyleWord(true);
} commentArea.setText("同意");
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton confirmButton = new JButton("确定");
JButton cancelButton = new JButton("取消");
confirmButton.addActionListener(event -> {
String comment = commentArea.getText().trim();
if (!comment.isEmpty()) {
leaveRequestService.approveLeaveRequest(
currentRequest.getId(),
ApprovalStatus.APPROVED,
comment
);
approveDialog.dispose();
dispose();
if (onApprovalComplete != null) {
onApprovalComplete.run();
}
}
});
cancelButton.addActionListener(event -> approveDialog.dispose());
buttonPanel.add(confirmButton);
buttonPanel.add(cancelButton);
inputPanel.add(label, BorderLayout.NORTH);
inputPanel.add(new JScrollPane(commentArea), BorderLayout.CENTER);
inputPanel.add(buttonPanel, BorderLayout.SOUTH);
approveDialog.add(inputPanel);
approveDialog.setVisible(true);
}); });
rejectButton.addActionListener(e -> { rejectButton.addActionListener(e -> {
String reason = JOptionPane.showInputDialog( // 创建一个自定义对话框
this, JDialog rejectDialog = new JDialog(this, "驳回原因", true);
"请输入驳回原因:", rejectDialog.setLayout(new BorderLayout());
"驳回原因", rejectDialog.setSize(300, 200);
JOptionPane.QUESTION_MESSAGE rejectDialog.setLocationRelativeTo(this);
);
if (reason != null && !reason.trim().isEmpty()) { JPanel inputPanel = new JPanel(new BorderLayout(10, 10));
leaveRequestService.approveLeaveRequest( inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
currentRequest.getId(),
ApprovalStatus.REJECTED, JLabel label = new JLabel("请输入驳回原因:");
reason JTextArea commentArea = new JTextArea(4, 20);
); commentArea.setLineWrap(true);
dispose(); commentArea.setWrapStyleWord(true);
}
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton confirmButton = new JButton("确定");
JButton cancelButton = new JButton("取消");
confirmButton.addActionListener(event -> {
String comment = commentArea.getText().trim();
if (!comment.isEmpty()) {
leaveRequestService.approveLeaveRequest(
currentRequest.getId(),
ApprovalStatus.REJECTED,
comment
);
rejectDialog.dispose();
dispose();
if (onApprovalComplete != null) {
onApprovalComplete.run();
}
}
});
cancelButton.addActionListener(event -> rejectDialog.dispose());
buttonPanel.add(confirmButton);
buttonPanel.add(cancelButton);
inputPanel.add(label, BorderLayout.NORTH);
inputPanel.add(new JScrollPane(commentArea), BorderLayout.CENTER);
inputPanel.add(buttonPanel, BorderLayout.SOUTH);
rejectDialog.add(inputPanel);
rejectDialog.setVisible(true);
}); });
// 添加滚动面板 // 添加滚动面板

View File

@ -11,6 +11,8 @@ import javax.swing.table.TableColumnModel;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.List; import java.util.List;
@ -46,7 +48,9 @@ public class TeacherMainFrame extends JFrame {
// 状态过滤器 // 状态过滤器
JPanel filterPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); JPanel filterPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
String[] statuses = {"全部", "待审批", "已通过", "已驳回"}; String[] statuses = {"全部", ApprovalStatus.PENDING.getDescription(),
ApprovalStatus.APPROVED.getDescription(),
ApprovalStatus.REJECTED.getDescription()};
statusFilter = new JComboBox<>(statuses); statusFilter = new JComboBox<>(statuses);
statusFilter.addActionListener(e -> loadLeaveRequests()); statusFilter.addActionListener(e -> loadLeaveRequests());
filterPanel.add(statusFilter); filterPanel.add(statusFilter);
@ -62,7 +66,7 @@ public class TeacherMainFrame extends JFrame {
topPanel.add(filterPanel, BorderLayout.SOUTH); topPanel.add(filterPanel, BorderLayout.SOUTH);
// 表格 // 表格
String[] columnNames = {"姓名", "班级", "状态", "申请时间", "外出事由"}; String[] columnNames = {"ID", "姓名", "班级", "状态", "申请时间", "外出事由"};
tableModel = new DefaultTableModel(columnNames, 0) { tableModel = new DefaultTableModel(columnNames, 0) {
@Override @Override
public boolean isCellEditable(int row, int column) { public boolean isCellEditable(int row, int column) {
@ -79,11 +83,12 @@ public class TeacherMainFrame extends JFrame {
// 设置列宽 // 设置列宽
TableColumnModel columnModel = requestTable.getColumnModel(); TableColumnModel columnModel = requestTable.getColumnModel();
columnModel.getColumn(0).setPreferredWidth(50); // 姓名 columnModel.getColumn(0).setPreferredWidth(40); // ID
columnModel.getColumn(1).setPreferredWidth(90); // 班级 columnModel.getColumn(1).setPreferredWidth(50); // 姓名
columnModel.getColumn(2).setPreferredWidth(50); // 状态 columnModel.getColumn(2).setPreferredWidth(90); // 班级
columnModel.getColumn(3).setPreferredWidth(100); // 申请时间 columnModel.getColumn(3).setPreferredWidth(50); // 状态
columnModel.getColumn(4).setPreferredWidth(60); // 外出事由 columnModel.getColumn(4).setPreferredWidth(100); // 申请时间
columnModel.getColumn(5).setPreferredWidth(60); // 外出事由
// 分页控制 // 分页控制
JPanel pagePanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); JPanel pagePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
@ -94,18 +99,73 @@ public class TeacherMainFrame extends JFrame {
pagePanel.add(pageLabel); pagePanel.add(pageLabel);
pagePanel.add(nextButton); pagePanel.add(nextButton);
// 操作按钮
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton approveButton = new JButton("通过");
JButton rejectButton = new JButton("驳回");
approveButton.setPreferredSize(new Dimension(80, 30));
rejectButton.setPreferredSize(new Dimension(80, 30));
// 设置按钮颜色
approveButton.setBackground(new Color(39, 174, 96));
approveButton.setForeground(Color.WHITE);
rejectButton.setBackground(new Color(231, 76, 60));
rejectButton.setForeground(Color.WHITE);
buttonPanel.add(approveButton);
buttonPanel.add(rejectButton);
// 默认禁用按钮
approveButton.setEnabled(false);
rejectButton.setEnabled(false);
// 表格选择监听器
requestTable.getSelectionModel().addListSelectionListener(e -> {
if (!e.getValueIsAdjusting() && requestTable.getSelectedRow() != -1) {
String statusText = (String) tableModel.getValueAt(requestTable.getSelectedRow(), 3);
boolean isPending = false;
for (ApprovalStatus s : ApprovalStatus.values()) {
if (s.getDescription().equals(statusText)) {
isPending = (s == ApprovalStatus.PENDING);
break;
}
}
approveButton.setEnabled(isPending);
rejectButton.setEnabled(isPending);
}
});
// 按钮点击事件
approveButton.addActionListener(e -> {
if (requestTable.getSelectedRow() != -1) {
showRequestDetails(requestTable.getSelectedRow());
}
});
rejectButton.addActionListener(e -> {
if (requestTable.getSelectedRow() != -1) {
showRequestDetails(requestTable.getSelectedRow());
}
});
// 添加到主面板 // 添加到主面板
JPanel mainPanel = new JPanel(new BorderLayout()); JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(topPanel, BorderLayout.NORTH); mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(new JScrollPane(requestTable), BorderLayout.CENTER); mainPanel.add(new JScrollPane(requestTable), BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(pagePanel, BorderLayout.SOUTH); mainPanel.add(pagePanel, BorderLayout.SOUTH);
// 添加事件监听 // 添加事件监听
searchButton.addActionListener(e -> loadLeaveRequests()); searchButton.addActionListener(e -> loadLeaveRequests());
requestTable.getSelectionModel().addListSelectionListener(e -> { // 使用鼠标监听器替代选择监听器实现双击打开详情
if (!e.getValueIsAdjusting() && requestTable.getSelectedRow() != -1) { requestTable.addMouseListener(new MouseAdapter() {
showRequestDetails(requestTable.getSelectedRow()); @Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 && requestTable.getSelectedRow() != -1) {
showRequestDetails(requestTable.getSelectedRow());
}
} }
}); });
@ -134,16 +194,11 @@ public class TeacherMainFrame extends JFrame {
requests = leaveRequestService.getAllLeaveRequests(); requests = leaveRequestService.getAllLeaveRequests();
} else { } else {
ApprovalStatus status = null; ApprovalStatus status = null;
switch (statusText) { for (ApprovalStatus s : ApprovalStatus.values()) {
case "待审批": if (s.getDescription().equals(statusText)) {
status = ApprovalStatus.PENDING; status = s;
break;
case "已通过":
status = ApprovalStatus.APPROVED;
break;
case "已驳回":
status = ApprovalStatus.REJECTED;
break; break;
}
} }
requests = leaveRequestService.getLeaveRequestsByStatus(status); requests = leaveRequestService.getLeaveRequestsByStatus(status);
} }
@ -158,6 +213,7 @@ public class TeacherMainFrame extends JFrame {
// 添加到表格 // 添加到表格
for (LeaveRequest request : requests) { for (LeaveRequest request : requests) {
Object[] row = { Object[] row = {
request.getId(),
request.getStudent().getName(), request.getStudent().getName(),
request.getStudent().getClassName(), request.getStudent().getClassName(),
request.getStatus().getDescription(), request.getStatus().getDescription(),
@ -170,7 +226,11 @@ public class TeacherMainFrame extends JFrame {
private void showRequestDetails(int row) { private void showRequestDetails(int row) {
// 创建详情对话框 // 创建详情对话框
RequestDetailDialog dialog = new RequestDetailDialog(this, tableModel.getValueAt(row, 0).toString()); RequestDetailDialog dialog = new RequestDetailDialog(
this,
((Number)tableModel.getValueAt(row, 0)).intValue(), // 获取ID
() -> loadLeaveRequests() // 添加刷新列表的回调
);
dialog.setVisible(true); dialog.setVisible(true);
} }

View File

@ -79,7 +79,6 @@ public class LeaveRequestService {
return leaveRequestDAO.findByStatus(status); return leaveRequestDAO.findByStatus(status);
} }
/** /**
* 获取所有请假申请 * 获取所有请假申请
* @return 请假申请列表 * @return 请假申请列表
@ -88,6 +87,15 @@ public class LeaveRequestService {
return leaveRequestDAO.findAll(); return leaveRequestDAO.findAll();
} }
/**
* 根据ID获取请假申请
* @param requestId 请假申请ID
* @return 请假申请信息
*/
public LeaveRequest getLeaveRequestById(int requestId) {
return leaveRequestDAO.findById(requestId);
}
/** /**
* 检查请假时间是否合理 * 检查请假时间是否合理
* @param startTime 开始时间 * @param startTime 开始时间