package com.student.dao.impl; import com.student.bean.LeaveRequest; import com.student.dao.LeaveDao; import com.student.util.DBUtil; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; public class LeaveDaoImpl implements LeaveDao { private static final String BASE_SQL = "SELECT l.id, l.student_id, l.start_date, l.end_date, l.reason, l.status, " + "l.approver_id, l.approve_note, l.approve_time, l.create_time, " + "u.real_name AS student_name, u.student_no, a.real_name AS approver_name " + "FROM leave_request l " + "JOIN users u ON l.student_id = u.id " + "LEFT JOIN users a ON l.approver_id = a.id "; @Override public boolean submit(LeaveRequest request) { String sql = "INSERT INTO leave_request (student_id, start_date, end_date, reason, status) " + "VALUES (?, ?, ?, ?, ?)"; Connection conn = null; PreparedStatement ps = null; try { conn = DBUtil.getConnection(); ps = conn.prepareStatement(sql); ps.setInt(1, request.getStudentId()); ps.setDate(2, new Date(request.getStartDate().getTime())); ps.setDate(3, new Date(request.getEndDate().getTime())); ps.setString(4, request.getReason()); ps.setInt(5, LeaveRequest.STATUS_PENDING); return ps.executeUpdate() > 0; } catch (SQLException e) { throw new RuntimeException("提交请假申请失败", e); } finally { DBUtil.close(conn, ps); } } @Override public boolean approve(int id, int approverId, String note) { return updateStatus(id, approverId, note, LeaveRequest.STATUS_APPROVED); } @Override public boolean reject(int id, int approverId, String note) { return updateStatus(id, approverId, note, LeaveRequest.STATUS_REJECTED); } private boolean updateStatus(int id, int approverId, String note, int status) { String sql = "UPDATE leave_request SET status=?, approver_id=?, approve_note=?, approve_time=NOW() WHERE id=?"; Connection conn = null; PreparedStatement ps = null; try { conn = DBUtil.getConnection(); ps = conn.prepareStatement(sql); ps.setInt(1, status); ps.setInt(2, approverId); ps.setString(3, note); ps.setInt(4, id); return ps.executeUpdate() > 0; } catch (SQLException e) { throw new RuntimeException("审批请假失败", e); } finally { DBUtil.close(conn, ps); } } @Override public List findPending() { return queryList(BASE_SQL + " WHERE l.status = 0 ORDER BY l.create_time ASC"); } @Override public List findByStudent(int studentId) { return queryList(BASE_SQL + " WHERE l.student_id = ? ORDER BY l.create_time DESC", studentId); } @Override public LeaveRequest findById(int id) { List list = queryList(BASE_SQL + " WHERE l.id = ?", id); return list.isEmpty() ? null : list.get(0); } private List queryList(String sql, Object... params) { List list = new ArrayList<>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = DBUtil.getConnection(); ps = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { ps.setObject(i + 1, params[i]); } rs = ps.executeQuery(); while (rs.next()) { list.add(mapRow(rs)); } } catch (SQLException e) { throw new RuntimeException("查询请假记录失败", e); } finally { DBUtil.close(conn, ps, rs); } return list; } private LeaveRequest mapRow(ResultSet rs) throws SQLException { LeaveRequest req = new LeaveRequest(); req.setId(rs.getInt("id")); req.setStudentId(rs.getInt("student_id")); Date startDate = rs.getDate("start_date"); if (startDate != null) { req.setStartDate(new java.util.Date(startDate.getTime())); } Date endDate = rs.getDate("end_date"); if (endDate != null) { req.setEndDate(new java.util.Date(endDate.getTime())); } req.setReason(rs.getString("reason")); req.setStatus(rs.getInt("status")); int approverId = rs.getInt("approver_id"); req.setApproverId(rs.wasNull() ? null : approverId); req.setApproveNote(rs.getString("approve_note")); Timestamp approveTime = rs.getTimestamp("approve_time"); if (approveTime != null) { req.setApproveTime(new java.util.Date(approveTime.getTime())); } Timestamp createTime = rs.getTimestamp("create_time"); if (createTime != null) { req.setCreateTime(new java.util.Date(createTime.getTime())); } req.setStudentName(rs.getString("student_name")); req.setStudentNo(rs.getString("student_no")); req.setApproverName(rs.getString("approver_name")); return req; } }