/**
* 班级操行分管理系统 - 个人记录查询页JS
*
* 开发者: Canglan
* 版权归属: Sea Network Technology Studio
*
* 版权所有 © Sea Network Technology Studio
*/
(function() {
'use strict';
const canRevokeAny = hasPermission('revoke_any');
const canRevokeOwn = hasPermission('revoke_own');
const canRestore = hasPermission('restore_records');
const canManage = canRevokeAny || canRevokeOwn;
let currentStudentId = null;
let currentPage = 1;
function typeMap(relatedType) {
var map = { manual: '手动', homework: '作业', attendance: '考勤' };
return map[relatedType] || '手动';
}
function monthLabel(month) {
if (!month || month.indexOf('-') < 0) return month || '-';
var parts = month.split('-');
return parts[0] + '年' + parseInt(parts[1], 10) + '月';
}
async function loadStudents() {
const res = await apiGet('/api/admin/students', { page_size: 1000 });
if (res && res.success) {
let html = '';
res.data.students.forEach(s => {
html += '';
});
document.getElementById('queryStudentId').innerHTML = html;
}
}
async function queryStudentRecords(page) {
const studentId = document.getElementById('queryStudentId').value;
if (!studentId) {
showToast('请先选择学生', 'warning');
return;
}
currentStudentId = studentId;
currentPage = page || 1;
const semesterId = document.getElementById('querySemesterId').value;
document.getElementById('queryPlaceholder').style.display = 'none';
document.getElementById('queryResult').style.display = 'block';
await Promise.all([
loadSummary(studentId, semesterId),
loadRecords(studentId, semesterId, currentPage)
]);
}
function onQueryStudentChange() {
if (!document.getElementById('queryStudentId').value) {
document.getElementById('queryResult').style.display = 'none';
document.getElementById('queryPlaceholder').style.display = 'block';
return;
}
queryStudentRecords(1);
}
function onQuerySemesterChange() {
if (document.getElementById('queryStudentId').value) {
queryStudentRecords(1);
}
}
async function loadSummary(studentId, semesterId) {
const params = { student_id: studentId };
if (semesterId) params.semester_id = semesterId;
const res = await apiGet('/api/admin/conduct/student-summary', params);
if (!res || !res.success) {
showToast((res && res.message) || '获取汇总失败', 'error');
return;
}
const data = res.data;
const semesterName = data.semester ? data.semester.semester_name : '全部学期';
document.getElementById('queryStudentTitle').textContent =
data.student.name + '(' + data.student.student_no + ') - ' + semesterName;
const s = data.summary;
document.getElementById('querySummaryCards').innerHTML =
'
基础分
' + data.base_points + '
' +
'加分合计
+' + s.bonus_total + '
' +
'减分合计
' + s.deduction_total + '
' +
'得分(基础分+加减分)
' + s.computed_total + '
' +
'当前实时总分
' + s.current_total_points + '
';
document.getElementById('querySummaryHint').textContent =
'共 ' + s.record_count + ' 条有效记录(不含已撤销)。' +
'月度分为当月加分与减分的净得分,与基础分无关;总分 = 基础分 + 全部加分 - 全部减分。';
renderMonthly(data.months);
renderReasonSummary(data.months);
}
function renderMonthly(months) {
let html = '';
if (!months || months.length === 0) {
html = '| 暂无记录 |
';
} else {
months.forEach(m => {
const monthScore = (typeof m.month_score === 'number') ? m.month_score : ((m.bonus || 0) + (m.deduction || 0));
html += '' +
'| ' + escapeHtml(monthLabel(m.month)) + ' | ' +
'+' + m.bonus + ' | ' +
'' + m.deduction + ' | ' +
'' + (monthScore > 0 ? '+' : '') + monthScore + ' | ' +
'' + m.record_count + ' | ' +
'
';
});
}
document.getElementById('queryMonthlyList').innerHTML = html;
}
function renderReasonTable(reasons, emptyText) {
if (!reasons || reasons.length === 0) {
return '' + emptyText + '
';
}
let html = '| 理由 | 次数 | 合计 |
';
reasons.forEach(r => {
html += '' +
'| ' + escapeHtml(r.reason) + ' | ' +
'' + r.count + ' | ' +
'' + (r.points > 0 ? '+' : '') + r.points + ' | ' +
'
';
});
html += '
';
return html;
}
function renderReasonSummary(months) {
const container = document.getElementById('queryReasonSummary');
if (!months || months.length === 0) {
container.innerHTML = '暂无记录
';
return;
}
let html = '';
months.forEach(m => {
html += '' +
'
' + escapeHtml(monthLabel(m.month)) +
' ' +
'加分 +' + m.bonus + ' / 减分 ' + m.deduction + '
' +
'
' +
'
加分理由
' + renderReasonTable(m.bonus_reasons, '本月无加分') + '
' +
'
减分理由
' + renderReasonTable(m.deduction_reasons, '本月无减分') + '
' +
'
';
});
container.innerHTML = html;
}
async function loadRecords(studentId, semesterId, page) {
const params = { student_id: studentId, page: page || 1, page_size: 20 };
if (semesterId) params.semester_id = semesterId;
const res = await apiGet('/api/admin/conduct/history', params);
if (!res || !res.success) {
showToast('获取明细记录失败', 'error');
return;
}
const records = res.data.records || [];
let html = '';
records.forEach(record => {
const pointsClass = record.points_change > 0 ? 'plus' : 'minus';
const revokedStyle = record.is_revoked == 1 ? ' style="opacity:0.5;text-decoration:line-through;"' : '';
html += '' +
'| ' + typeMap(record.related_type) + ' | ' +
'' + (record.points_change > 0 ? '+' : '') + record.points_change + ' | ' +
'' + escapeHtml(record.reason) + ' | ' +
'' + escapeHtml(record.recorder_name || '') + ' | ' +
'' + formatDateTime(record.created_at) + ' | ';
if (canManage) {
if (record.is_revoked == 1) {
if (canRestore) {
html += '已撤销 | ';
} else {
html += '已撤销 | ';
}
} else if (canRevokeAny || (canRevokeOwn && record.recorder_id == window.PAGE_CONFIG.userId)) {
html += ' | ';
} else {
html += '- | ';
}
}
html += '
';
});
if (records.length === 0) {
html = '| 暂无记录 |
';
}
document.getElementById('queryRecordList').innerHTML = html;
const totalPages = res.data.total_pages || 1;
renderSmartPagination('queryPagination', page || 1, totalPages, function(p) {
currentPage = p;
loadRecords(currentStudentId, document.getElementById('querySemesterId').value, p);
});
}
async function revokeQueryRecord(recordId) {
if (!confirm('确定要撤销这条记录吗?撤销后学生分数将恢复。')) return;
const res = await apiPost('/api/admin/conduct/revoke', { record_id: recordId });
if (res && res.success) {
showToast('撤销成功');
queryStudentRecords(currentPage);
} else {
showToast((res && res.message) || '撤销失败', 'error');
}
}
async function restoreQueryRecord(recordId) {
if (!confirm('确定要反撤销这条记录吗?分数变动将重新生效。')) return;
const res = await apiPost('/api/admin/conduct/restore', { record_id: recordId });
if (res && res.success) {
showToast('反撤销成功');
queryStudentRecords(currentPage);
} else {
showToast((res && res.message) || '反撤销失败', 'error');
}
}
async function exportStudentRecords() {
const studentId = document.getElementById('queryStudentId').value;
if (!studentId) {
showToast('请先选择学生', 'warning');
return;
}
const semesterSelect = document.getElementById('querySemesterId');
const semesterId = semesterSelect.value;
const semesterName = (semesterSelect.selectedIndex >= 0)
? semesterSelect.options[semesterSelect.selectedIndex].textContent.replace(/((当前|已归档))$/, '')
: '全部学期';
const studentName = document.getElementById('queryStudentId').selectedOptions[0].textContent.trim();
showToast('正在导出个人记录...', 'info');
try {
const params = { student_id: studentId, is_revoked: 0 };
if (semesterId) params.semester_id = semesterId;
const records = await apiGetAll('/api/admin/conduct/history', params);
let summaryLines = [];
const summaryParams = { student_id: studentId };
if (semesterId) summaryParams.semester_id = semesterId;
const summaryRes = await apiGet('/api/admin/conduct/student-summary', summaryParams);
if (summaryRes && summaryRes.success) {
const d = summaryRes.data;
summaryLines = [
['学生', studentName],
['学期', semesterName],
['基础分', d.base_points],
['加分合计', d.summary.bonus_total],
['减分合计', d.summary.deduction_total],
['得分(基础分+加减分)', d.summary.computed_total],
['当前实时总分', d.summary.current_total_points]
];
}
let csv = '';
summaryLines.forEach(line => {
csv += [line[0], line[1]].map(csvEscape).join(',') + '\n';
});
csv += '\n时间,类型,分值,原因,操作人,状态\n';
records.forEach(r => {
csv += [
r.created_at || '',
typeMap(r.related_type),
(r.points_change > 0 ? '+' : '') + r.points_change,
r.reason || '',
r.recorder_name || '',
r.is_revoked == 1 ? '已撤销' : '正常'
].map(csvEscape).join(',') + '\n';
});
const safeLabel = (semesterName || '全部学期').replace(/[\\/:*?"<>|]/g, '_');
downloadCsv('个人记录_' + studentName + '_' + safeLabel + '_' + new Date().toISOString().slice(0,10) + '.csv', csv);
showToast('导出成功,共' + records.length + '条明细记录');
} catch (err) {
showToast('导出失败:' + err.message, 'error');
}
}
(async function init() {
await Promise.all([
loadStudents(),
loadSemesterOptions('querySemesterId', { includeAll: true })
]);
const urlParams = new URLSearchParams(window.location.search);
const preStudentId = urlParams.get('student_id');
if (preStudentId) {
document.getElementById('queryStudentId').value = preStudentId;
queryStudentRecords(1);
}
})();
window.queryStudentRecords = queryStudentRecords;
window.onQueryStudentChange = onQueryStudentChange;
window.onQuerySemesterChange = onQuerySemesterChange;
window.exportStudentRecords = exportStudentRecords;
window.revokeQueryRecord = revokeQueryRecord;
window.restoreQueryRecord = restoreQueryRecord;
})();