Customize discipline record exports
This commit is contained in:
1 parent
bce04b768c
commit
6a1a287961
3 files changed
+28
-5
No files matched your search
@@ -81,14 +81,19 @@ public sealed class StudentDisciplinesController(AppDbContext db, ICurrentUserDa
|
||||
return Ok(new { items, total, page, pageSize });
|
||||
}
|
||||
|
||||
private static readonly string[] ExportHeaders = ["学号", "姓名", "班级", "处分类型", "状态", "决定日期", "生效日期", "文号", "决定机构"];
|
||||
[HttpGet("export-columns")]
|
||||
public IActionResult ExportColumns() => Ok(ExportHeaders);
|
||||
|
||||
[HttpGet("export")]
|
||||
public async Task<IActionResult> Export(string? keyword, StudentDisciplineState? state, string? disciplineType, DateOnly? dateFrom, DateOnly? dateTo, CancellationToken token)
|
||||
public async Task<IActionResult> Export(string? keyword, StudentDisciplineState? state, string? disciplineType, DateOnly? dateFrom, DateOnly? dateTo, CancellationToken token, string? columns = null, string? format = null)
|
||||
{
|
||||
var query = Filter(keyword, state, disciplineType, dateFrom, dateTo);
|
||||
var items = await query.OrderByDescending(x => x.CreatedAt).ThenByDescending(x => x.Id).Take(10001).Select(x => new { x.Student!.StudentNumber, x.Student.Name, ClassName = x.Student.AdministrativeClass!.Name, x.DisciplineType, x.State, x.DecisionDate, x.EffectiveDate, x.DecisionNumber, x.DecisionAuthority }).ToListAsync(token);
|
||||
if (items.Count > 10000) return BadRequest("结果超过 10000 条,请缩小查询范围。");
|
||||
var rows = items.Select(x => (IReadOnlyList<object?>)new object?[] { x.StudentNumber, x.Name, x.ClassName, x.DisciplineType, StateLabel(x.State), x.DecisionDate.ToString("yyyy-MM-dd"), x.EffectiveDate.ToString("yyyy-MM-dd"), x.DecisionNumber, x.DecisionAuthority });
|
||||
return File(ExcelWorkbookHelper.Create("处分记录", ["学号", "姓名", "班级", "处分类型", "状态", "决定日期", "生效日期", "文号", "决定机构"], rows), ExcelWorkbookHelper.ContentType, "处分记录.xlsx");
|
||||
var rows = items.Select(x => (IReadOnlyList<object?>)new object?[] { x.StudentNumber, x.Name, x.ClassName, x.DisciplineType, StateLabel(x.State), x.DecisionDate.ToString("yyyy-MM-dd"), x.EffectiveDate.ToString("yyyy-MM-dd"), x.DecisionNumber, x.DecisionAuthority }).ToList();
|
||||
if (columns is not null || format is not null) return TabularExport.Create(this, "处分记录", ExportHeaders, rows, columns, format);
|
||||
return File(ExcelWorkbookHelper.Create("处分记录", ExportHeaders, rows), ExcelWorkbookHelper.ContentType, "处分记录.xlsx");
|
||||
}
|
||||
|
||||
private static string StateLabel(StudentDisciplineState state) => state switch { StudentDisciplineState.Draft => "草稿", StudentDisciplineState.Submitted => "待院系审核", StudentDisciplineState.CollegeApproved => "待校级审核", StudentDisciplineState.Effective => "已批准", StudentDisciplineState.Rejected => "已驳回", StudentDisciplineState.Withdrawn => "已撤回", StudentDisciplineState.Lifted => "已解除", StudentDisciplineState.Revoked => "已撤销", _ => "未知" };
|
||||
|
||||
@@ -52,6 +52,12 @@ public sealed class StudentDisciplinesControllerTests
|
||||
using var stream = new MemoryStream(export.FileContents);
|
||||
using var workbook = new ClosedXML.Excel.XLWorkbook(stream);
|
||||
Assert.Equal(1, workbook.Worksheet(1).LastRowUsed()!.RowNumber());
|
||||
var custom = Assert.IsType<FileContentResult>(await school.Export(null, null, null, null, null, default, "1,0", "xlsx"));
|
||||
using var selected = new ClosedXML.Excel.XLWorkbook(new MemoryStream(custom.FileContents));
|
||||
Assert.Equal("姓名", selected.Worksheet(1).Cell(1, 1).GetString());
|
||||
Assert.Equal(student.Name, selected.Worksheet(1).Cell(2, 1).GetString());
|
||||
var restricted = Assert.IsType<FileContentResult>(await otherCollege.Export(null, null, null, null, null, default, "1,0", "txt"));
|
||||
Assert.DoesNotContain(student.StudentNumber, System.Text.Encoding.UTF8.GetString(restricted.FileContents));
|
||||
Assert.IsType<NoContentResult>(await school.SetTypes(["警告", "严重警告"], default));
|
||||
Assert.IsType<NoContentResult>(await school.SetTypes(["严重警告"], default));
|
||||
Assert.False((await db.StudentDisciplineTypes.SingleAsync(x => x.Name == "警告")).IsEnabled);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import CustomExportDialog from '../components/CustomExportDialog.vue'
|
||||
import { downloadApiFile } from '../api/excel'
|
||||
import { onMounted, reactive, ref } from 'vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import http, { apiErrorMessage } from '../api/http'
|
||||
@@ -19,7 +21,16 @@ const filters = () => ({ keyword: keyword.value || undefined, state: state.value
|
||||
async function loadTypes() { try { types.value = (await http.get('/student-disciplines/types')).data } catch (e) { ElMessage.error(apiErrorMessage(e)) } }
|
||||
function editTypes() { typesText.value = types.value.filter(x => x.isEnabled).map(x => x.name).join('\n'); typesOpen.value = true }
|
||||
async function saveTypes() { try { await http.put('/student-disciplines/types', typesText.value.split('\n').map(x => x.trim()).filter(Boolean)); typesOpen.value = false; await loadTypes(); ElMessage.success('处分类型已保存') } catch (e) { ElMessage.error(apiErrorMessage(e)) } }
|
||||
async function exportRecords() { try { const response = await http.get('/student-disciplines/export', { params: filters(), responseType: 'blob' }); const url = URL.createObjectURL(response.data); const link = document.createElement('a'); link.href = url; link.download = '处分记录.xlsx'; link.click(); setTimeout(() => URL.revokeObjectURL(url), 1000) } catch (e) { ElMessage.error(apiErrorMessage(e)) } }
|
||||
const exportDialog = ref<InstanceType<typeof CustomExportDialog>>()
|
||||
async function openExportDialog() {
|
||||
try { const { data } = await http.get<string[]>('/student-disciplines/export-columns'); exportDialog.value?.open(data, '处分记录') }
|
||||
catch (error) { ElMessage.error(apiErrorMessage(error)) }
|
||||
}
|
||||
async function exportRecords(columns: number[], filename: string, format: string) {
|
||||
try { await downloadApiFile('/student-disciplines/export', filename, { params: { ...filters(), columns: columns.join(','), format } }, filename) }
|
||||
catch (error) { ElMessage.error(apiErrorMessage(error)) }
|
||||
}
|
||||
|
||||
async function load() {
|
||||
busy.value = true
|
||||
try { const { data } = await http.get('/student-disciplines', { params: { page: page.value, pageSize: 20, ...filters() } }); rows.value = data.items; total.value = data.total }
|
||||
@@ -56,7 +67,7 @@ onMounted(() => { void load(); void loadTypes() })
|
||||
<el-input v-model="keyword" placeholder="学号或姓名" clearable style="width:220px" @keyup.enter="page = 1; load()" />
|
||||
<el-select v-model="state" clearable placeholder="状态" style="width:160px"><el-option v-for="(label, key) in labels" :key="key" :label="label" :value="key" /></el-select>
|
||||
<el-input v-model="disciplineType" clearable placeholder="处分类型" style="width:160px" /><el-date-picker v-model="dateRange" type="daterange" value-format="YYYY-MM-DD" start-placeholder="决定日期起" end-placeholder="决定日期止" />
|
||||
<el-button @click="page = 1; load()">查询</el-button><el-button @click="exportRecords">导出筛选结果</el-button><el-button v-if="has('StudentAffairsAdmin', 'AcademicAdmin', 'SuperAdmin')" @click="editTypes">维护处分类型</el-button><el-button type="primary" @click="editingId = ''; Object.assign(form, { studentId: '', disciplineType: '', facts: '', basis: '', decisionNumber: '', decisionAuthority: '', decisionDate: '', effectiveDate: '' }); createOpen = true">新增处分</el-button>
|
||||
<el-button @click="page = 1; load()">查询</el-button><el-button @click="openExportDialog">导出筛选结果</el-button><el-button v-if="has('StudentAffairsAdmin', 'AcademicAdmin', 'SuperAdmin')" @click="editTypes">维护处分类型</el-button><el-button type="primary" @click="editingId = ''; Object.assign(form, { studentId: '', disciplineType: '', facts: '', basis: '', decisionNumber: '', decisionAuthority: '', decisionDate: '', effectiveDate: '' }); createOpen = true">新增处分</el-button>
|
||||
</div>
|
||||
<el-table v-loading="busy" :data="rows"><el-table-column prop="studentNumber" label="学号" /><el-table-column prop="name" label="姓名" /><el-table-column prop="className" label="班级" /><el-table-column prop="disciplineType" label="处分类型" /><el-table-column label="状态"><template #default="{ row }">{{ labels[row.state] }}</template></el-table-column><el-table-column prop="decisionDate" label="决定日期" /><el-table-column label="操作"><template #default="{ row }"><el-button link type="primary" @click="detail(row.id)">详情</el-button></template></el-table-column></el-table>
|
||||
<el-pagination v-model:current-page="page" :page-size="20" :total="total" layout="total, prev, pager, next" @current-change="load" />
|
||||
@@ -71,4 +82,5 @@ onMounted(() => { void load(); void loadTypes() })
|
||||
</el-dialog>
|
||||
<el-dialog v-model="typesOpen" title="维护处分类型" width="500px"><p>每行填写一种类型;移除的类型将停用,历史记录保留原名称。</p><el-input v-model="typesText" type="textarea" :rows="8" /><template #footer><el-button @click="typesOpen = false">取消</el-button><el-button type="primary" @click="saveTypes">保存</el-button></template></el-dialog>
|
||||
</div>
|
||||
<CustomExportDialog ref="exportDialog" @export="exportRecords" />
|
||||
</template>
|
||||
Reference in new issue
Block a user