/** * 班级操行分管理系统 - 数据报表页JS * * 开发者: Canglan * 版权归属: Sea Network Technology Studio * * 版权所有 © Sea Network Technology Studio */ (function() { 'use strict'; let attendanceCache = null; let homeworkCache = null; function formatDateValue(date) { return date.getFullYear() + '-' + String(date.getMonth() + 1).padStart(2, '0') + '-' + String(date.getDate()).padStart(2, '0'); } async function initReportDates() { const startInput = document.getElementById('reportStartDate'); const endInput = document.getElementById('reportEndDate'); const today = new Date(); let start = new Date(today.getFullYear(), today.getMonth(), 1); try { const res = await apiGet('/api/semester/active'); if (res && res.success && res.data && res.data.start_date) { start = new Date(res.data.start_date); } } catch (e) { /* 使用默认月初 */ } startInput.value = formatDateValue(start); endInput.value = formatDateValue(today); } async function loadReports() { const startDate = document.getElementById('reportStartDate').value; const endDate = document.getElementById('reportEndDate').value; if (!startDate || !endDate) { showToast('请选择开始和结束日期', 'warning'); return; } if (startDate > endDate) { showToast('开始日期不能晚于结束日期', 'warning'); return; } document.getElementById('reportRangeHint').textContent = `统计范围:${startDate} ~ ${endDate}`; const [attendanceRes, homeworkRes] = await Promise.all([ apiGet('/api/report/attendance', { start_date: startDate, end_date: endDate }), apiGet('/api/report/homework', { start_date: startDate, end_date: endDate }) ]); if (attendanceRes && attendanceRes.success) { attendanceCache = attendanceRes.data; renderAttendance(attendanceRes.data); } else { showToast((attendanceRes && attendanceRes.message) || '考勤报表加载失败', 'error'); } if (homeworkRes && homeworkRes.success) { homeworkCache = homeworkRes.data; renderHomework(homeworkRes.data); } else { showToast((homeworkRes && homeworkRes.message) || '作业报表加载失败', 'error'); } } function rateText(rate) { return (rate === null || rate === undefined) ? '-' : rate + '%'; } function renderAttendance(data) { const rows = data.rows || []; const summary = data.summary || {}; document.getElementById('attendanceSummary').textContent = `共 ${rows.length} 人,${summary.total || 0} 条记录:出勤 ${summary.present || 0}、缺勤 ${summary.absent || 0}、迟到 ${summary.late || 0}、请假 ${summary.leave || 0},出勤率 ${rateText(summary.rate)}`; let html = ''; rows.forEach((row, index) => { html += ` ${index + 1} ${escapeHtml(row.student_no)} ${escapeHtml(row.name)} ${row.present} ${row.absent} ${row.late} ${row.leave} ${row.total} ${rateText(row.rate)} `; }); if (rows.length === 0) { html = '暂无数据'; } document.getElementById('attendanceReportList').innerHTML = html; } function renderHomework(data) { const rows = data.rows || []; const summary = data.summary || {}; document.getElementById('homeworkSummary').textContent = `共 ${rows.length} 人,${summary.total || 0} 条记录:已交 ${summary.submitted || 0}、未交 ${summary.not_submitted || 0}、迟交 ${summary.late || 0},未交率 ${rateText(summary.miss_rate)}`; let html = ''; rows.forEach((row, index) => { html += ` ${index + 1} ${escapeHtml(row.student_no)} ${escapeHtml(row.name)} ${row.submitted} ${row.not_submitted} ${row.late} ${row.total} ${rateText(row.miss_rate)} `; }); if (rows.length === 0) { html = '暂无数据'; } document.getElementById('homeworkReportList').innerHTML = html; } function exportReport(type) { const data = type === 'attendance' ? attendanceCache : homeworkCache; if (!data || !data.rows || data.rows.length === 0) { showToast('请先查询报表数据', 'warning'); return; } let csv = ''; if (type === 'attendance') { csv += '序号,学号,姓名,出勤,缺勤,迟到,请假,记录数,出勤率(%)\n'; data.rows.forEach((row, index) => { csv += [index + 1, row.student_no, row.name, row.present, row.absent, row.late, row.leave, row.total, (row.rate === null || row.rate === undefined) ? '' : row.rate].map(csvEscape).join(',') + '\n'; }); } else { csv += '序号,学号,姓名,已交,未交,迟交,记录数,未交率(%)\n'; data.rows.forEach((row, index) => { csv += [index + 1, row.student_no, row.name, row.submitted, row.not_submitted, row.late, row.total, (row.miss_rate === null || row.miss_rate === undefined) ? '' : row.miss_rate].map(csvEscape).join(',') + '\n'; }); } const label = type === 'attendance' ? '考勤统计' : '作业统计'; downloadCsv(`${label}_${data.start_date}_${data.end_date}.csv`, csv); showToast('导出成功,共' + data.rows.length + '人'); } initReportDates().then(loadReports); window.loadReports = loadReports; window.exportReport = exportReport; })();