fuliqi
2024-06-06 711555c3d5c36a0b2ca34eb9dc8b31a8101d9e81
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
<!-- 试卷管理 -->
<template>
  <div class="c">
    <div class="bg">
      <div class="main">
        <!-- 带返回的标题 -->
        <TitleIndex title='试卷管理'/>
        <div class="content">
          <div
            style="padding-bottom:20px; border-bottom: 3px solid #409EFF;margin-bottom: 20px;display: flex; align-items: center;">
            <el-page-header @back="goBack()">
            </el-page-header>
            <el-button
              type="primary"
              @click="addTemplate"
            >新增模板
            </el-button>
          </div>
          <!-- 搜索 -->
          <div>
            <el-form
              :inline="true"
              :model="queryParam"
              label-width="80px"
            >
              <el-form-item>
                <el-input v-model="queryParam.name" placeholder="模板名" clearable></el-input>
              </el-form-item>
              <el-form-item>
                <el-select
                  v-model="queryParam.subjectId"
                  placeholder="全部科目"
                  clearable
                >
                  <el-option v-for="item in subjects" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
              </el-form-item>
              <el-form-item label="">
                <el-button
                  style="width:100px;"
                  type="primary"
                  size="small"
                  @click="getPage"
                >查询
                </el-button>
              </el-form-item>
            </el-form>
          </div>
          <!-- 表格 -->
          <el-table
            :header-cell-style="getRowClass"
            :data="tableData"
            border
            style="width: 100%;"
            :row-style="{height:'40px'}"
            :cell-style="{padding: '0'}"
          >
            <el-table-column
              align="center"
              prop="name"
              label="模板名"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="subjectId"
              label="科目"
            >
              <template slot-scope="scope">
                {{ translate(scope.row.subjectId) }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="suggestTime"
              label="建议时长(分钟)"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="score"
              label="分值"
            >
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
            >
              <template slot-scope="scope">
                <el-button type="text">预览</el-button>
                <el-button type="text">编辑</el-button>
                <el-button type="text" @click="deletePaper(scope.row)" class="link-left">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
 
          <div
            class="flex"
            style="justify-content:center;margin-top:20px;"
          >
            <pagination v-show="total>0" :total="total" :page.sync="queryParam.currentPage"
                        :limit.sync="queryParam.pageSize"
                        @pagination="getPage"/>
          </div>
        </div>
 
        <!-- 添加试卷模板对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="模板名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入模板名称" style="width: 300px"/>
            </el-form-item>
            <el-form-item label="科目" prop="subjectId">
              <el-select v-model="form.subjectId" placeholder="请选择科目" style="width: 300px">
                <el-option
                  v-for="item in subjects"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="考试时长(分钟)" prop="suggestTime">
              <el-input-number v-model="form.suggestTime" placeholder="请输入考试时长"/>
            </el-form-item>
            <el-form-item label="权限" prop="visibility">
              <el-radio v-model="form.visibility" :label="1">私有</el-radio>
              <el-radio v-model="form.visibility" :label="2">公开</el-radio>
            </el-form-item>
            <el-form-item label="多选题得分类型" prop="deductType" v-show="addedQuestionTypes.includes(2)"><!-- 有多选才出现 -->
              <div>
                <el-select v-model="form.deductType" placeholder="请选择多选题得分类型" style="width: 200px;margin-right: 30px">
                  <el-option
                    v-for="item in deductTypeList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
                <el-input-number v-model="form.score" placeholder="请输入多选得分分数"
                                 v-show="form.deductType === 2 || form.deductType === 3"/>
              </div>
            </el-form-item>
            <el-form-item prop="questionList" label-width="0">
              <el-table
                :summary-method="getSummaries"
                show-summary
                :data="questionList"
                style="width: 100%;">
                <el-table-column
                  prop="questionType"
                  label="题目类型"
                  width="180"
                >
                  <template slot-scope="scope">
                    <div> {{ translateQuestionType(scope.row.questionType) }}</div>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="num"
                  label="题目数量"
                  width="180">
                </el-table-column>
                <el-table-column
                  prop="score"
                  label="每题分数">
                </el-table-column>
                <el-table-column
                  fixed="right"
                  width="100">
                  <template slot-scope="scope">
                    <el-button
                      type="text"
                      size="small"
                      @click="editQuestion(scope.row)">
                      编辑
                    </el-button>
                    <el-button
                      type="text"
                      size="small"
                      @click="removeQuestion(scope.row)">
                      移除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <el-button type="success" @click="nextAdd()" size="mini" plain>添加</el-button>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
 
        <!-- 添加题目对话框 -->
        <el-dialog :title="questionTitle" :visible.sync="questionOpen" width="400px" append-to-body>
          <el-form ref="questionForm" :model="questionForm" :rules="questionRules">
            <el-form-item label="题目类型" prop="questionType">
              <el-select v-model="questionForm.questionType" placeholder="请选择题目类型">
                <el-option
                  v-for="item in questionTypeList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                  :disabled="addedQuestionTypes.includes(item.value)"/><!-- 禁用已添加的选项 -->
              </el-select>
            </el-form-item>
            <el-form-item label="题目数量" prop="num" style="margin-left: 10px">
              <el-input-number v-model="questionForm.num" placeholder="请输入题目数量" :precision="0"/>
            </el-form-item>
            <el-form-item label="每题分数" prop="score" style="margin-left: 10px">
              <el-input-number v-model="questionForm.score" placeholder="请输入每题分数" :precision="1"/>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="addQuestion">确 定</el-button>
            <el-button @click="questionCancel">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
 
</template>
<script>
import {
  getExamTemplates,
  getExamTemplateById,
  deleteExamTemplateById,
  editExamTemplate,
  addExamTemplate
} from '@/api/examTemplate'
import subjectApi from '@/api/subject'
import Pagination from "@/components/Pagination"
 
export default {
  components: {Pagination},
  data() {
    var validateDeductType = (rule, value, callback) => {
      const index = this.addedQuestionTypes.findIndex(item => item.questionType === 2)
      if (index !== -1 && (!value || value === '')) {
        callback(new Error('请选择多选题得分方式'));
      } else {
        callback();
      }
    }
    return {
      // 弹出层标题
      title: "",
      questionTitle: "",
      // 是否显示弹出层
      open: false,
      questionOpen: false,
      // 总条数
      total: 0,
      queryParam: {
        name: null,
        subjectId: null,
        currentPage: 1,
        pageSize: 10
      },
      form: {
        visibility: 1
      },
      questionForm: {},
      tableData: [],
      subjects: [],
      deductTypeList: [
        {name: '答错不得分', value: 1},
        {name: '漏选得固定分值,包含错误选项不得分', value: 2},
        {name: '每对一题得相应分值,包含错误选项不得分', value: 3},
      ],
      questionTypeList: [
        {name: '单选题', value: 1},
        {name: '多选题', value: 2},
        {name: '填空题', value: 3},
        {name: '判断题', value: 4},
        {name: '简答题', value: 5},
        {name: '计算题', value: 6},
      ],
      questionList: [],
      addedQuestionTypes: [],// 已添加的题目类型
      // 表单校验
      rules: {
        name: [
          {required: true, message: "试卷名称不能为空", trigger: "blur"}
        ],
        subjectId: [
          {required: true, message: "科目不能为空", trigger: "change"}
        ],
        paperType: [
          {required: true, message: "试卷类型不能为空", trigger: "change"}
        ],
        suggestTime: [
          {required: true, message: "考试时长不能为空", trigger: "blur"}
        ],
        deductType: [
          {validator: validateDeductType, trigger: 'change'}
        ],
      },
      questionRules: {
        questionType: [
          {required: true, message: "题目类型不能为空", trigger: "change"}
        ],
      }
    };
 
  },
  created() {
    this.getSubjects();
    this.getPage()
  },
  methods: {
    //移除题目
    removeQuestion(row) {
      let index = this.questionList.findIndex(item => item.questionType === row.questionType && item.num === row.num && item.score === row.score);
      this.questionList.splice(index, 1);
      index = this.addedQuestionTypes.findIndex(item => item.questionType === row.questionType)
      this.addedQuestionTypes.splice(index, 1);
    },
    //编辑题目
    editQuestion(row) {
      this.questionReset();
      this.questionForm.questionType = row.questionType
      this.questionForm.num = row.num
      this.questionForm.score = row.score
      this.questionTitle = "编辑题目"
      this.questionOpen = true;
    },
    //题目列表合计方法
    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        if (index === 3) {
          sums[index] = '';
          return;
        }
        if (index === 2) { // 第三列是分数列
          const values = data.map(item => Number(item.score) * Number(item.num) || 0);
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] = sums[index].toFixed(2); // 格式化合计值,保留两位小数
            sums[index] += ' 分';
          }
        } else if (index === 1) { // 第二列是题目数量列,在这里展示总数
          const values = data.map(item => Number(item.num) || 0);
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] += ' 题';
          } else {
            sums[index] = 'N/A';
          }
        } else {
          sums[index] = 'N/A';
        }
      });
      return sums;
    },
    translate(subjectId) {
      const subject = this.subjects.find(subject => subject.id == subjectId);
      return subject ? subject.name : '未知';
    },
    translateQuestionType(questionTypeId) {
      const questionType = this.questionTypeList.find(questionType => questionType.value == questionTypeId);
      return questionType ? questionType.name : '未知';
    },
    // 返回上一个页面
    goBack() {
      this.$router.back();
    },
    cancel() {
      this.open = false;
    },
    questionCancel() {
      this.questionOpen = false;
    },
    // 模板表单重置
    reset() {
      this.form = {
        name: null,
        subjectId: null,
        suggestTime: null,
        visibility: 1
      };
      this.addedQuestionTypes = []
      this.questionList = []
    },
    // 问题表单重置
    questionReset() {
      this.questionForm = {
        questionType: null,
        num: null,
        score: null,
      };
    },
    // 修改表单头部的颜色
    getRowClass() {
      return "background:#d2d3d6";
    },
    //新增模板
    addTemplate() {
      this.reset();
      this.title = "新增模板"
      this.open = true;
    },
    //新增题目
    nextAdd() {
      this.questionReset();
      this.questionTitle = "添加题目"
      this.questionOpen = true;
    },
    deletePaper(row) {
      deleteExamTemplateById(row.id).then(re => {
        if (re.code === 1) {
          this.getPage()
          this.$message.success("删除成功")
        } else {
          this.$message.error(re.message)
        }
      })
    },
    getPage() {
      this.listLoading = true
      getExamTemplates(this.queryParam).then(re => {
        const data = re.data
        this.tableData = data.data
        this.total = data.total
        this.listLoading = false
      })
    },
    // 获取科目
    getSubjects() {
      subjectApi.list().then(re => {
        this.subjects = re.data
      })
    },
    //添加或修改题目
    addQuestion() {
      this.$refs['questionForm'].validate(valid => {
        if (valid) {
          //新增
          if (this.questionTitle === "添加题目") {
            // 将题目类型添加到已添加列表中
            if (!this.addedQuestionTypes.includes(this.questionForm.questionType)) {
              this.addedQuestionTypes.push(this.questionForm.questionType);
            }
            this.questionList.push({
              "questionType": this.questionForm.questionType,
              "num": this.questionForm.num,
              "score": this.questionForm.score
            });
            this.questionOpen = false;
          } else if (this.questionTitle === "编辑题目") {
            let index = this.questionList.findIndex(item => item.questionType === this.questionForm.questionType)
            this.questionList.splice(index, 1, this.questionForm);
            this.questionOpen = false;
          }
        }
      })
    },
    //添加或修改模板
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let temp = {
            ...this.form,
          };
          temp.questionList = this.questionList
          if (temp.id != null) {
            editExamTemplate(temp).then(response => {
              this.$message("修改成功");
              this.open = false;
              this.getPage();
            });
          } else {
            addExamTemplate(temp).then(response => {
              this.$message("新增成功");
              this.open = false;
              this.reset()
              this.getPage();
            });
          }
        } else {
          console.log("error")
        }
      })
    },
  },
};
</script>
<style scoped lang="scss">
.flex {
  display: flex;
}
 
// 内容
.content {
  width: 1262px;
  margin-bottom: 80px;
  background-color: #fff;
  padding: 20px 40px;
  border-radius: 10px;
}
 
</style>