feat: model undergraduate schools terms and exam queries
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'school_identity.dart';
|
||||
import 'school_term.dart';
|
||||
|
||||
class ExamRecord {
|
||||
const ExamRecord({
|
||||
required this.code,
|
||||
@@ -8,8 +12,12 @@ class ExamRecord {
|
||||
this.room = '',
|
||||
this.seat = '',
|
||||
this.time = '',
|
||||
this.date = '', this.status = '', this.notes = '', this.examTypeId,
|
||||
});
|
||||
final String code, name, assessment, candidate, sequence, room, seat, time;
|
||||
final String date, status, notes;
|
||||
final int? examTypeId;
|
||||
String get undergraduateKey => jsonEncode([examTypeId,code,date,time,room,seat]);
|
||||
Map<String, dynamic> toJson() => {
|
||||
'code': code,
|
||||
'name': name,
|
||||
@@ -19,10 +27,11 @@ class ExamRecord {
|
||||
'room': room,
|
||||
'seat': seat,
|
||||
'time': time,
|
||||
'date':date, 'status':status, 'notes':notes, 'examTypeId':examTypeId,
|
||||
};
|
||||
factory ExamRecord.fromJson(Map<String, dynamic> json) {
|
||||
String field(String key) {
|
||||
final value = json[key];
|
||||
String field(String key, {bool optional=false}) {
|
||||
final value = optional && !json.containsKey(key) ? '' : json[key];
|
||||
if (value is! String || value.length > 1000) {
|
||||
throw const FormatException('考试信息无效');
|
||||
}
|
||||
@@ -38,6 +47,8 @@ class ExamRecord {
|
||||
room: field('room'),
|
||||
seat: field('seat'),
|
||||
time: field('time'),
|
||||
date:field('date',optional:true), status:field('status',optional:true), notes:field('notes',optional:true),
|
||||
examTypeId:json['examTypeId'] as int?,
|
||||
);
|
||||
if (record.code.isEmpty || record.name.isEmpty) {
|
||||
throw const FormatException('考试课程无效');
|
||||
@@ -51,15 +62,21 @@ class ExamSchedule {
|
||||
required this.semesterLabel,
|
||||
required this.termId,
|
||||
required List<ExamRecord> records,
|
||||
}) : records = List.unmodifiable(records);
|
||||
this.school = SupportedSchool.uestcGraduate,
|
||||
Set<int> requestedExamTypes = const {},
|
||||
}) : records = List.unmodifiable(records), requestedExamTypes=Set.unmodifiable(requestedExamTypes);
|
||||
final String semesterLabel, termId;
|
||||
final List<ExamRecord> records;
|
||||
final SupportedSchool school;
|
||||
final Set<int> requestedExamTypes;
|
||||
Map<String, dynamic> toJson() => {
|
||||
'semesterLabel': semesterLabel,
|
||||
'termId': termId,
|
||||
'records': records.map((r) => r.toJson()).toList(),
|
||||
'schoolId':school.id, 'requestedExamTypes':requestedExamTypes.toList()..sort(),
|
||||
};
|
||||
factory ExamSchedule.fromJson(Map<String, dynamic> json) {
|
||||
try {
|
||||
final label = json['semesterLabel'],
|
||||
id = json['termId'],
|
||||
rows = json['records'];
|
||||
@@ -74,9 +91,18 @@ class ExamSchedule {
|
||||
final records = rows
|
||||
.map((r) => ExamRecord.fromJson(Map<String, dynamic>.from(r as Map)))
|
||||
.toList();
|
||||
if (records.map((r) => r.code).toSet().length != records.length) {
|
||||
final school=json['schoolId']==null ? SupportedSchool.uestcGraduate : SupportedSchool.fromId(json['schoolId'] as String);
|
||||
final rawTypes=json['requestedExamTypes'] ?? <int>[];
|
||||
if(rawTypes is! List || rawTypes.any((t)=>t is! int || t<1 || t>3)) throw const FormatException('考试类型无效');
|
||||
final types=rawTypes.cast<int>().toSet();
|
||||
SchoolTerm(int.parse(id),school:school);
|
||||
if(school==SupportedSchool.uestcUndergraduate && (types.isEmpty || records.any((r)=>!types.contains(r.examTypeId)))) {
|
||||
throw const FormatException('本科考试类型无效');
|
||||
}
|
||||
if (records.map((r) => school==SupportedSchool.uestcGraduate ? r.code : r.undergraduateKey).toSet().length != records.length) {
|
||||
throw const FormatException('考试课程重复');
|
||||
}
|
||||
return ExamSchedule(semesterLabel: label, termId: id, records: records);
|
||||
return ExamSchedule(semesterLabel: label, termId: id, records: records,school:school,requestedExamTypes:types);
|
||||
} catch (_) { throw const FormatException('考试列表无效'); }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user