刘光辉
6 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<script lang="ts" setup>
import type { ActionItem, BasicColumn } from '@jnpf/ui/vxeTable';
 
import type { QuestionBankOption, QuestionEntity } from './types';
 
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
 
import { useMessage } from '@jnpf/hooks';
import { BasicVxeTable, TableAction, useVxeTable } from '@jnpf/ui/vxeTable';
 
import { Modal, Upload } from 'ant-design-vue';
 
import {
  downloadQuestionImportTemplate,
  exportQuestions,
  getQuestionBanks,
  getQuestionList,
  importQuestions,
  invalidateQuestion,
  setQuestionStatus,
} from '#/api/x/tms/question';
import { saveExportBlob } from '#/views/x/tms/shared/exportDownload';
import { TMS_DIC_FIELD_NAMES } from '#/views/x/tms/shared/dic';
import { TMS_BTN } from '#/views/x/tms/shared/ui';
 
import {
  LIFE_TYPE_OPTIONS,
  QUESTION_TYPE_OPTIONS,
  STATUS_OPTIONS,
  labelOfLifeType,
  labelOfStatus,
  labelOfType,
  loadQuestionDics,
} from './constants';
 
import '#/views/x/tms/shared/page.css';
 
defineOptions({ name: 'TmsQuestionList' });
 
const router = useRouter();
const { createMessage } = useMessage();
 
const banks = ref<QuestionBankOption[]>([]);
const exporting = ref(false);
const importVisible = ref(false);
const templateLoading = ref(false);
const importing = ref(false);
 
const columns: BasicColumn[] = [
  { title: '题库', dataIndex: 'bankName', minWidth: 220 },
  {
    title: '类型',
    dataIndex: 'questionType',
    width: 90,
    customRender: ({ record }) => labelOfType((record as QuestionEntity).questionType),
  },
  {
    title: '题干',
    dataIndex: 'stem',
    minWidth: 280,
    customRender: ({ record }) => stripHtml((record as QuestionEntity).stem),
  },
  {
    title: '时效',
    dataIndex: 'lifeType',
    width: 80,
    customRender: ({ record }) => labelOfLifeType((record as QuestionEntity).lifeType),
  },
  {
    title: '过期时间',
    dataIndex: 'expireTime',
    width: 170,
    customRender: ({ record }) => {
      const row = record as QuestionEntity;
      return row.lifeType === 'temp' ? row.expireTime || '-' : '-';
    },
  },
  { title: '创建时间', dataIndex: 'creatorTime', width: 170 },
  { title: '管理员', dataIndex: 'adminUserName', width: 100 },
  {
    title: '状态',
    dataIndex: 'bizStatus',
    width: 90,
    align: 'center',
    slots: { default: 'bizStatus' },
  },
];
 
const [registerTable, { reload, getForm, getSelectRows }] = useVxeTable({
  api: fetchList,
  columns,
  immediate: false,
  rowKey: 'id',
  useSearchForm: true,
  formConfig: {
    baseColProps: { span: 6 },
    compact: true,
    showAdvancedButton: true,
    alwaysShowLines: 1,
    autoAdvancedLine: 4,
    schemas: [
      {
        field: 'bankId',
        label: '题库',
        component: 'Select',
        componentProps: {
          allowClear: true,
          showSearch: true,
          placeholder: '请选择题库',
          options: [],
        },
      },
      {
        field: 'questionType',
        label: '题型',
        component: 'Select',
        componentProps: {
          allowClear: true,
          showSearch: true,
          placeholder: '请选择题型',
          options: QUESTION_TYPE_OPTIONS.value,
          fieldNames: TMS_DIC_FIELD_NAMES,
        },
      },
      {
        field: 'bizStatus',
        label: '状态',
        component: 'Select',
        componentProps: {
          allowClear: true,
          showSearch: true,
          placeholder: '请选择状态',
          options: STATUS_OPTIONS.value,
          fieldNames: TMS_DIC_FIELD_NAMES,
        },
      },
      {
        field: 'lifeType',
        label: '时效类型',
        component: 'Select',
        componentProps: {
          allowClear: true,
          showSearch: true,
          placeholder: '请选择时效类型',
          options: LIFE_TYPE_OPTIONS.value,
          fieldNames: TMS_DIC_FIELD_NAMES,
        },
      },
      {
        field: 'adminUserId',
        label: '管理员',
        component: 'UserSelect',
        componentProps: {
          placeholder: '请选择管理员',
        },
      },
      {
        field: 'keyword',
        label: '题干',
        component: 'Input',
        componentProps: { placeholder: '请输入题干', submitOnPressEnter: true },
      },
    ],
  },
  rowSelection: { type: 'checkbox' },
  actionColumn: {
    width: 200,
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
  },
});
 
onMounted(async () => {
  await loadQuestionDics();
  try {
    const list = await getQuestionBanks();
    banks.value = Array.isArray(list) ? list : [];
  } catch {
    banks.value = [];
  }
  getForm()?.updateSchema?.([
    {
      field: 'bankId',
      componentProps: {
        allowClear: true,
        showSearch: true,
        placeholder: '请选择题库',
        options: banks.value,
      },
    },
    {
      field: 'questionType',
      componentProps: {
        allowClear: true,
        showSearch: true,
        placeholder: '请选择题型',
        options: QUESTION_TYPE_OPTIONS.value,
        fieldNames: TMS_DIC_FIELD_NAMES,
      },
    },
    {
      field: 'bizStatus',
      componentProps: {
        allowClear: true,
        showSearch: true,
        placeholder: '请选择状态',
        options: STATUS_OPTIONS.value,
        fieldNames: TMS_DIC_FIELD_NAMES,
      },
    },
    {
      field: 'lifeType',
      componentProps: {
        allowClear: true,
        showSearch: true,
        placeholder: '请选择时效类型',
        options: LIFE_TYPE_OPTIONS.value,
        fieldNames: TMS_DIC_FIELD_NAMES,
      },
    },
  ]);
  reload();
});
 
async function fetchList(params: Record<string, any>) {
  const page = await getQuestionList(params);
  return {
    data: {
      list: Array.isArray(page?.list) ? page.list : [],
      pagination: page?.pagination || { total: 0 },
    },
  };
}
 
function stripHtml(html?: string) {
  if (!html) return '';
  const text = html.replace(/<[^>]+>/g, '').replace(/&nbsp;/g, ' ').trim();
  return text.length > 80 ? `${text.slice(0, 80)}…` : text;
}
 
function statusColor(status?: string) {
  if (status === 'open') return 'success';
  if (status === 'closed') return 'warning';
  if (status === 'invalid') return 'error';
  return 'default';
}
 
function handleCreate() {
  router.push('/tms/question/create');
}
 
function handleManage() {
  const rows = (getSelectRows?.() || []) as QuestionEntity[];
  if (!rows.length) {
    createMessage.warning('请先勾选要管理的试题');
    return;
  }
  if (rows.length === 1) {
    handleEdit(rows[0]!);
    return;
  }
  const openRows = rows.filter((r) => r.bizStatus === 'open');
  if (openRows.length) {
    createMessage.warning('勾选中含已开放试题,请先设为不开放后再批量废弃');
    return;
  }
  Modal.confirm({
    title: '批量废弃',
    content: `已勾选 ${rows.length} 道试题,确定全部废弃吗?`,
    onOk: async () => {
      for (const row of rows) {
        if (row.id) await invalidateQuestion(row.id);
      }
      createMessage.success('废弃成功');
      reload();
    },
  });
}
 
function handleDetail(record: QuestionEntity) {
  router.push(`/tms/question/detail/${record.id}`);
}
 
function handleEdit(record: QuestionEntity) {
  if (record.bizStatus === 'invalid') {
    createMessage.warning('已废弃试题不可编辑');
    return;
  }
  router.push(`/tms/question/edit/${record.id}`);
}
 
async function handleInvalidate(record: QuestionEntity) {
  await invalidateQuestion(record.id!);
  createMessage.success('废弃成功');
  reload();
}
 
async function handleEnable(record: QuestionEntity) {
  await setQuestionStatus(record.id!, 'open');
  createMessage.success('已开放');
  reload();
}
 
async function handleDisable(record: QuestionEntity) {
  await setQuestionStatus(record.id!, 'closed');
  createMessage.success('已不开放');
  reload();
}
 
async function handleExport() {
  if (exporting.value) return;
  const selected = (getSelectRows?.() || []) as QuestionEntity[];
  const filters = (getForm()?.getFieldsValue?.() || {}) as Record<string, any>;
  exporting.value = true;
  try {
    const payload =
      selected.length > 0
        ? { ids: selected.map((x) => x.id!).filter(Boolean) }
        : {
            bankId: filters.bankId,
            questionType: filters.questionType,
            bizStatus: filters.bizStatus,
            lifeType: filters.lifeType,
            keyword: filters.keyword,
          };
    const res = await exportQuestions(payload);
    await saveExportBlob(res, `试题导出.xls`);
    createMessage.success(selected.length ? '导出成功' : '已按当前筛选条件导出');
  } catch (e: any) {
    createMessage.error(e?.message || '导出失败');
  } finally {
    exporting.value = false;
  }
}
 
function openImportModal() {
  importVisible.value = true;
}
 
async function handleDownloadTemplate() {
  if (templateLoading.value) return;
  templateLoading.value = true;
  try {
    const res = await downloadQuestionImportTemplate();
    await saveExportBlob(res, '试题导入模板.xls');
    createMessage.success('模板已下载');
  } catch (e: any) {
    createMessage.error(e?.message || '下载模板失败');
  } finally {
    templateLoading.value = false;
  }
}
 
async function handleImport(file: File) {
  if (importing.value) return false;
  importing.value = true;
  try {
    const result = await importQuestions(file);
    const ok = result?.successCount || 0;
    const fail = result?.failCount || 0;
    if (fail > 0) {
      const detail = (result?.errors || []).slice(0, 8).join('\n');
      Modal.warning({
        title: `导入完成:成功 ${ok},失败 ${fail}`,
        content: detail || '部分行导入失败,请检查题库名称与题型',
        width: 520,
      });
    } else {
      createMessage.success(`成功导入 ${ok} 道试题`);
    }
    if (ok > 0) {
      importVisible.value = false;
      reload();
    }
  } catch (e: any) {
    createMessage.error(e?.message || '导入失败');
  } finally {
    importing.value = false;
  }
  return false;
}
 
function getTableActions(record: QuestionEntity): ActionItem[] {
  const title = stripHtml(record.stem) || record.questionNo || '';
  if (record.bizStatus === 'invalid') {
    return [{ label: '详情', onClick: handleDetail.bind(null, record) }];
  }
  const actions: ActionItem[] = [
    { label: '编辑', onClick: handleEdit.bind(null, record) },
    { label: '详情', onClick: handleDetail.bind(null, record) },
  ];
  if (record.bizStatus === 'open') {
    actions.unshift({
      label: '不开放',
      modelConfirm: {
        content: `确定将试题「${title}」设为不开放吗?不开放后学生不可模拟抽题。`,
        onOk: handleDisable.bind(null, record),
      },
    });
  } else {
    actions.unshift({
      label: '开放',
      modelConfirm: {
        content: `确定开放试题「${title}」吗?开放后学生可模拟抽题。`,
        onOk: handleEnable.bind(null, record),
      },
    });
    actions.push({
      label: '废弃',
      color: 'error',
      modelConfirm: {
        content: `确定废弃试题「${title}」吗?废弃后仍可在列表按状态查看,但不可再组卷/模拟。`,
        onOk: handleInvalidate.bind(null, record),
      },
    });
  }
  return actions;
}
</script>
 
<template>
  <div class="jnpf-content-wrapper">
    <div class="jnpf-content-wrapper-center">
      <div class="jnpf-content-wrapper-content">
        <BasicVxeTable @register="registerTable">
          <template #tableTitle>
            <a-space>
              <a-button type="primary" pre-icon="icon-ym icon-ym-btn-add" @click="handleCreate">创建试题</a-button>
              <a-button @click="handleManage">管理试题</a-button>
              <a-button @click="openImportModal">{{ TMS_BTN.import }}</a-button>
              <a-button :loading="exporting" @click="handleExport">{{ TMS_BTN.export }}</a-button>
              <span class="tms-toolbar-hint">导出优先勾选,未勾选则按筛选条件</span>
            </a-space>
          </template>
          <template #bizStatus="{ record }">
            <a-tag :color="statusColor(record.bizStatus)">{{ labelOfStatus(record.bizStatus) }}</a-tag>
          </template>
          <template #action="{ record }">
            <TableAction :actions="getTableActions(record)" />
          </template>
        </BasicVxeTable>
      </div>
    </div>
 
    <Modal
      v-model:open="importVisible"
      title="导入试题"
      :mask-closable="!importing"
      :destroy-on-close="true"
      :confirm-loading="importing"
      centered
      width="560"
      wrap-class-name="tms-import-dialog"
    >
      <div class="tms-import-modal">
        <a-alert
          class="tms-import-modal__alert"
          type="info"
          show-icon
          message="请先下载模板并按示例填写;题库名称须与系统已有题库完全一致。支持 xls / xlsx / csv。"
        />
 
        <div class="tms-import-step">
          <div class="tms-import-step__index">1</div>
          <div class="tms-import-step__body">
            <div class="tms-import-step__title">下载导入模板</div>
            <div class="tms-import-step__desc">按模板列填写题库、题型、题干与答案</div>
            <a-button
              class="tms-import-step__action"
              :loading="templateLoading"
              pre-icon="icon-ym icon-ym-btn-download"
              @click="handleDownloadTemplate"
            >
              {{ TMS_BTN.downloadTemplate }}
            </a-button>
          </div>
        </div>
 
        <div class="tms-import-step">
          <div class="tms-import-step__index">2</div>
          <div class="tms-import-step__body">
            <div class="tms-import-step__title">上传填写后的文件</div>
            <div class="tms-import-step__desc">选择或拖拽文件后将立即开始导入</div>
            <Upload.Dragger
              class="tms-import-modal__dragger"
              :show-upload-list="false"
              :disabled="importing"
              accept=".xls,.xlsx,.csv"
              :before-upload="handleImport"
            >
              <p class="tms-import-modal__drag-icon">
                <i class="icon-ym icon-ym-generator-upload"></i>
              </p>
              <p class="tms-import-modal__drag-title">
                {{ importing ? '正在导入…' : '点击或拖拽文件到此处' }}
              </p>
              <p class="tms-import-modal__drag-sub">支持 .xls / .xlsx / .csv</p>
            </Upload.Dragger>
          </div>
        </div>
      </div>
      <template #footer>
        <a-button @click="importVisible = false">{{ TMS_BTN.close }}</a-button>
      </template>
    </Modal>
  </div>
</template>
 
<style scoped>
.tms-import-modal {
  padding: 8px 8px 4px;
}
 
.tms-import-modal__alert {
  margin-bottom: 20px;
}
 
.tms-import-step {
  display: flex;
  gap: 14px;
  margin-bottom: 20px;
}
 
.tms-import-step:last-child {
  margin-bottom: 0;
}
 
.tms-import-step__index {
  flex: 0 0 28px;
  width: 28px;
  height: 28px;
  margin-top: 2px;
  border-radius: 50%;
  background: #1677ff;
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  line-height: 28px;
  text-align: center;
}
 
.tms-import-step__body {
  flex: 1;
  min-width: 0;
}
 
.tms-import-step__title {
  font-size: 14px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.88);
  line-height: 22px;
}
 
.tms-import-step__desc {
  margin-top: 2px;
  margin-bottom: 12px;
  font-size: 13px;
  color: rgba(0, 0, 0, 0.45);
  line-height: 20px;
}
 
.tms-import-step__action {
  min-width: 120px;
}
 
.tms-import-modal__dragger {
  display: block;
}
 
.tms-import-modal__dragger :deep(.ant-upload-drag) {
  padding: 28px 16px;
  background: #fafafa;
  border-radius: 8px;
}
 
.tms-import-modal__drag-icon {
  margin: 0 0 8px;
  font-size: 36px;
  line-height: 1;
  color: #1677ff;
}
 
.tms-import-modal__drag-title {
  margin: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.88);
}
 
.tms-import-modal__drag-sub {
  margin: 6px 0 0;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
}
</style>