You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
6.8 KiB
185 lines
6.8 KiB
|
1 week ago
|
package com.student.dao.impl;
|
||
|
|
|
||
|
|
import com.student.bean.LoginLog;
|
||
|
|
import com.student.bean.OperationLog;
|
||
|
|
import com.student.bean.PageBean;
|
||
|
|
import com.student.dao.LogDao;
|
||
|
|
import com.student.util.DBUtil;
|
||
|
|
|
||
|
|
import java.sql.Connection;
|
||
|
|
import java.sql.PreparedStatement;
|
||
|
|
import java.sql.ResultSet;
|
||
|
|
import java.sql.SQLException;
|
||
|
|
import java.sql.Timestamp;
|
||
|
|
import java.sql.Types;
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
|
||
|
|
public class LogDaoImpl implements LogDao {
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public boolean saveLoginLog(LoginLog log) {
|
||
|
|
String sql = "INSERT INTO login_log (user_id, username, ip, result, message) VALUES (?, ?, ?, ?, ?)";
|
||
|
|
Connection conn = null;
|
||
|
|
PreparedStatement ps = null;
|
||
|
|
try {
|
||
|
|
conn = DBUtil.getConnection();
|
||
|
|
ps = conn.prepareStatement(sql);
|
||
|
|
if (log.getUserId() != null) {
|
||
|
|
ps.setInt(1, log.getUserId());
|
||
|
|
} else {
|
||
|
|
ps.setNull(1, Types.INTEGER);
|
||
|
|
}
|
||
|
|
ps.setString(2, log.getUsername());
|
||
|
|
ps.setString(3, log.getIp());
|
||
|
|
ps.setInt(4, log.getResult());
|
||
|
|
ps.setString(5, log.getMessage());
|
||
|
|
return ps.executeUpdate() > 0;
|
||
|
|
} catch (SQLException e) {
|
||
|
|
throw new RuntimeException("保存登录日志失败", e);
|
||
|
|
} finally {
|
||
|
|
DBUtil.close(conn, ps);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public boolean saveOperationLog(OperationLog log) {
|
||
|
|
String sql = "INSERT INTO operation_log (user_id, username, module, action, ip) VALUES (?, ?, ?, ?, ?)";
|
||
|
|
Connection conn = null;
|
||
|
|
PreparedStatement ps = null;
|
||
|
|
try {
|
||
|
|
conn = DBUtil.getConnection();
|
||
|
|
ps = conn.prepareStatement(sql);
|
||
|
|
if (log.getUserId() != null) {
|
||
|
|
ps.setInt(1, log.getUserId());
|
||
|
|
} else {
|
||
|
|
ps.setNull(1, Types.INTEGER);
|
||
|
|
}
|
||
|
|
ps.setString(2, log.getUsername());
|
||
|
|
ps.setString(3, log.getModule());
|
||
|
|
ps.setString(4, log.getAction());
|
||
|
|
ps.setString(5, log.getIp());
|
||
|
|
return ps.executeUpdate() > 0;
|
||
|
|
} catch (SQLException e) {
|
||
|
|
throw new RuntimeException("保存操作日志失败", e);
|
||
|
|
} finally {
|
||
|
|
DBUtil.close(conn, ps);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public PageBean<LoginLog> findLoginLogs(int currentPage, int pageSize) {
|
||
|
|
PageBean<LoginLog> page = new PageBean<>(currentPage, pageSize);
|
||
|
|
page.setTotalCount(countLoginLogs());
|
||
|
|
page.setData(queryLoginLogs(page.getStartIndex(), page.getPageSize()));
|
||
|
|
return page;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public PageBean<OperationLog> findOperationLogs(int currentPage, int pageSize) {
|
||
|
|
PageBean<OperationLog> page = new PageBean<>(currentPage, pageSize);
|
||
|
|
page.setTotalCount(countOperationLogs());
|
||
|
|
page.setData(queryOperationLogs(page.getStartIndex(), page.getPageSize()));
|
||
|
|
return page;
|
||
|
|
}
|
||
|
|
|
||
|
|
private int countLoginLogs() {
|
||
|
|
return count("SELECT COUNT(*) FROM login_log");
|
||
|
|
}
|
||
|
|
|
||
|
|
private int countOperationLogs() {
|
||
|
|
return count("SELECT COUNT(*) FROM operation_log");
|
||
|
|
}
|
||
|
|
|
||
|
|
private int count(String sql) {
|
||
|
|
Connection conn = null;
|
||
|
|
PreparedStatement ps = null;
|
||
|
|
ResultSet rs = null;
|
||
|
|
try {
|
||
|
|
conn = DBUtil.getConnection();
|
||
|
|
ps = conn.prepareStatement(sql);
|
||
|
|
rs = ps.executeQuery();
|
||
|
|
if (rs.next()) {
|
||
|
|
return rs.getInt(1);
|
||
|
|
}
|
||
|
|
} catch (SQLException e) {
|
||
|
|
throw new RuntimeException("统计日志数量失败", e);
|
||
|
|
} finally {
|
||
|
|
DBUtil.close(conn, ps, rs);
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
private List<LoginLog> queryLoginLogs(int startIndex, int pageSize) {
|
||
|
|
String sql = "SELECT id, user_id, username, ip, result, message, create_time "
|
||
|
|
+ "FROM login_log ORDER BY create_time DESC LIMIT ?, ?";
|
||
|
|
List<LoginLog> list = new ArrayList<>();
|
||
|
|
Connection conn = null;
|
||
|
|
PreparedStatement ps = null;
|
||
|
|
ResultSet rs = null;
|
||
|
|
try {
|
||
|
|
conn = DBUtil.getConnection();
|
||
|
|
ps = conn.prepareStatement(sql);
|
||
|
|
ps.setInt(1, startIndex);
|
||
|
|
ps.setInt(2, pageSize);
|
||
|
|
rs = ps.executeQuery();
|
||
|
|
while (rs.next()) {
|
||
|
|
LoginLog log = new LoginLog();
|
||
|
|
log.setId(rs.getInt("id"));
|
||
|
|
int userId = rs.getInt("user_id");
|
||
|
|
log.setUserId(rs.wasNull() ? null : userId);
|
||
|
|
log.setUsername(rs.getString("username"));
|
||
|
|
log.setIp(rs.getString("ip"));
|
||
|
|
log.setResult(rs.getInt("result"));
|
||
|
|
log.setMessage(rs.getString("message"));
|
||
|
|
Timestamp createTime = rs.getTimestamp("create_time");
|
||
|
|
if (createTime != null) {
|
||
|
|
log.setCreateTime(new java.util.Date(createTime.getTime()));
|
||
|
|
}
|
||
|
|
list.add(log);
|
||
|
|
}
|
||
|
|
} catch (SQLException e) {
|
||
|
|
throw new RuntimeException("查询登录日志失败", e);
|
||
|
|
} finally {
|
||
|
|
DBUtil.close(conn, ps, rs);
|
||
|
|
}
|
||
|
|
return list;
|
||
|
|
}
|
||
|
|
|
||
|
|
private List<OperationLog> queryOperationLogs(int startIndex, int pageSize) {
|
||
|
|
String sql = "SELECT id, user_id, username, module, action, ip, create_time "
|
||
|
|
+ "FROM operation_log ORDER BY create_time DESC LIMIT ?, ?";
|
||
|
|
List<OperationLog> list = new ArrayList<>();
|
||
|
|
Connection conn = null;
|
||
|
|
PreparedStatement ps = null;
|
||
|
|
ResultSet rs = null;
|
||
|
|
try {
|
||
|
|
conn = DBUtil.getConnection();
|
||
|
|
ps = conn.prepareStatement(sql);
|
||
|
|
ps.setInt(1, startIndex);
|
||
|
|
ps.setInt(2, pageSize);
|
||
|
|
rs = ps.executeQuery();
|
||
|
|
while (rs.next()) {
|
||
|
|
OperationLog log = new OperationLog();
|
||
|
|
log.setId(rs.getInt("id"));
|
||
|
|
int userId = rs.getInt("user_id");
|
||
|
|
log.setUserId(rs.wasNull() ? null : userId);
|
||
|
|
log.setUsername(rs.getString("username"));
|
||
|
|
log.setModule(rs.getString("module"));
|
||
|
|
log.setAction(rs.getString("action"));
|
||
|
|
log.setIp(rs.getString("ip"));
|
||
|
|
Timestamp createTime = rs.getTimestamp("create_time");
|
||
|
|
if (createTime != null) {
|
||
|
|
log.setCreateTime(new java.util.Date(createTime.getTime()));
|
||
|
|
}
|
||
|
|
list.add(log);
|
||
|
|
}
|
||
|
|
} catch (SQLException e) {
|
||
|
|
throw new RuntimeException("查询操作日志失败", e);
|
||
|
|
} finally {
|
||
|
|
DBUtil.close(conn, ps, rs);
|
||
|
|
}
|
||
|
|
return list;
|
||
|
|
}
|
||
|
|
}
|