first commit
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
const db = require('../db');
|
||||
const config = require('../config');
|
||||
const logger = require('../utils/logger');
|
||||
|
||||
/**
|
||||
* 记录操作日志
|
||||
*/
|
||||
function createLog({ approvalCode, instanceId, workflowType, applicantName, applicantPinyin, action, detail }) {
|
||||
const stmt = db.prepare(`
|
||||
INSERT INTO operation_logs
|
||||
(approval_code, approval_instance_id, workflow_type, applicant_name, applicant_pinyin, action, detail, status)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, 'running')
|
||||
`);
|
||||
const result = stmt.run(approvalCode, instanceId, workflowType, applicantName, applicantPinyin, action, detail);
|
||||
return result.lastInsertRowid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新日志状态
|
||||
*/
|
||||
function updateLog(logId, status, errorMessage = null) {
|
||||
const stmt = db.prepare(`
|
||||
UPDATE operation_logs
|
||||
SET status = ?, error_message = ?, finished_at = datetime('now', 'localtime')
|
||||
WHERE id = ?
|
||||
`);
|
||||
stmt.run(status, errorMessage, logId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询日志(带分页和过滤)
|
||||
*/
|
||||
function queryLogs({ page = 1, pageSize = 20, workflowType, status, startDate, endDate, keyword } = {}) {
|
||||
let where = 'WHERE 1=1';
|
||||
const params = [];
|
||||
|
||||
if (workflowType) {
|
||||
where += ' AND workflow_type = ?';
|
||||
params.push(workflowType);
|
||||
}
|
||||
if (status) {
|
||||
where += ' AND status = ?';
|
||||
params.push(status);
|
||||
}
|
||||
if (startDate) {
|
||||
where += ' AND created_at >= ?';
|
||||
params.push(startDate);
|
||||
}
|
||||
if (endDate) {
|
||||
where += ' AND created_at <= ?';
|
||||
params.push(endDate + ' 23:59:59');
|
||||
}
|
||||
if (keyword) {
|
||||
where += ' AND (applicant_name LIKE ? OR detail LIKE ? OR approval_instance_id LIKE ?)';
|
||||
params.push(`%${keyword}%`, `%${keyword}%`, `%${keyword}%`);
|
||||
}
|
||||
|
||||
const countStmt = db.prepare(`SELECT COUNT(*) as total FROM operation_logs ${where}`);
|
||||
const { total } = countStmt.get(...params);
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
const dataStmt = db.prepare(`
|
||||
SELECT * FROM operation_logs ${where}
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`);
|
||||
const rows = dataStmt.all(...params, pageSize, offset);
|
||||
|
||||
return { total, page, pageSize, rows };
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理过期日志(保留 N 天)
|
||||
*/
|
||||
function cleanupOldLogs() {
|
||||
const retentionDays = config.security.logRetentionDays;
|
||||
const stmt = db.prepare(`
|
||||
DELETE FROM operation_logs
|
||||
WHERE created_at < datetime('now', 'localtime', '-' || ? || ' days')
|
||||
`);
|
||||
const result = stmt.run(retentionDays);
|
||||
if (result.changes > 0) {
|
||||
logger.info(`已清理 ${result.changes} 条过期日志(保留${retentionDays}天)`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查审批是否已处理(防重复)
|
||||
*/
|
||||
function isApprovalProcessed(instanceId) {
|
||||
const stmt = db.prepare(`
|
||||
SELECT COUNT(*) as count FROM operation_logs
|
||||
WHERE approval_instance_id = ? AND status = 'success'
|
||||
`);
|
||||
const { count } = stmt.get(instanceId);
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createLog,
|
||||
updateLog,
|
||||
queryLogs,
|
||||
cleanupOldLogs,
|
||||
isApprovalProcessed,
|
||||
};
|
||||
Reference in New Issue
Block a user