You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.5 KiB
133 lines
4.5 KiB
import 'package:timemanagerapp/controller/NetWorkController.dart';
|
|
import 'package:timemanagerapp/database/dao/CourseDao.dart';
|
|
import 'package:timemanagerapp/entity/Course.dart';
|
|
import 'package:timemanagerapp/entity/CourseForm.dart';
|
|
import 'package:timemanagerapp/setting/Setting.dart';
|
|
import 'package:timemanagerapp/util/GetCourseByLogin.dart';
|
|
|
|
import '../util/dataUtil.dart';
|
|
|
|
class CourseController {
|
|
static CourseController getInstance() {
|
|
return new CourseController();
|
|
}
|
|
|
|
GetCourseByLogin getCourseByLogin = GetCourseByLogin();
|
|
IdGenerator idGenerator = IdGenerator();
|
|
List<Course> courseList = []; //实时维护的courseList缓存
|
|
NetWorkController netWorkController = NetWorkController();
|
|
|
|
DateTime termstartdate = Setting.startdate; //Setting.getStartDate();
|
|
|
|
// 课表的时间范围
|
|
final List<List<String>> raspiyane = [
|
|
["8:00", "8:45"], //1
|
|
["8:50", "9:35"], //2
|
|
["10:05", "10:50"], //3
|
|
["10:55", "11:40"], //4
|
|
|
|
["13:30", "14:15"], //5
|
|
["14:20", "15:05"], //6
|
|
["15:35", "16:20"], //7
|
|
["16:25", "17:10"], //8
|
|
|
|
["18:30", "19:15"],
|
|
["19:20", "20:05"],
|
|
["20:10", "20:55"],
|
|
["21:10", "21:50"],
|
|
["22:05", "22:35"],
|
|
];
|
|
|
|
Future<void> addCourseForm(CourseForm courseForm) async {
|
|
List<Course> courseListToInsert = [];
|
|
for (int week = courseForm.getStartWeek(); week <= courseForm.getEndWeek(); week++) {
|
|
for(int day in courseForm.selectedDays){
|
|
// 计算具体的日期和时间
|
|
final startDate = termstartdate.add(Duration(
|
|
days: (7 * (week - 1) + day! - 1),
|
|
hours: int.parse(raspiyane[courseForm.getStartTime() - 1][0].split(':')[0]),
|
|
minutes: int.parse(raspiyane[courseForm.getStartTime() - 1][0].split(':')[1]),
|
|
));
|
|
|
|
final endDate = termstartdate.add(Duration(
|
|
days: (7 * (week - 1) + day! - 1),
|
|
hours: int.parse(raspiyane[courseForm.getEndTime() - 1][1].split(':')[0]),
|
|
minutes: int.parse(raspiyane[courseForm.getEndTime() - 1][1].split(':')[1]),
|
|
));
|
|
Course course = Course(
|
|
id:await idGenerator.generateId(),
|
|
name: courseForm.getCourse(),
|
|
userId: Setting.user!.getId!,
|
|
courseId: await idGenerator.generateId(),
|
|
teacher: courseForm.getTeacher(),
|
|
location: courseForm.getLocation(),
|
|
start: startDate,
|
|
end: endDate,
|
|
credit: courseForm.getCredit(),
|
|
remark: courseForm.getNote()
|
|
);
|
|
courseListToInsert.add(course);
|
|
}
|
|
}
|
|
await insertCourseList(courseListToInsert);
|
|
}
|
|
|
|
Future<List<Course>> getCourses() async {
|
|
List<Map<String, dynamic>> courseMaps = await CourseDao().getCourses();
|
|
List<Course> courses = []; // 用于存储转换后的Course对象列表
|
|
|
|
for (var courseMap in courseMaps) {
|
|
// 使用Course类的构造函数从Map创建Course对象
|
|
Course course = Course(
|
|
id: courseMap['id'],
|
|
userId: courseMap['userId'],
|
|
courseId: courseMap['courseId'],
|
|
name: courseMap['name'],
|
|
credit: courseMap['credit'],
|
|
teacher: courseMap['teacher'],
|
|
location: courseMap['location'],
|
|
remark: courseMap['remark'],
|
|
start: DateTime.parse(courseMap['start']),
|
|
end: DateTime.parse(courseMap['end']),
|
|
);
|
|
courses.add(course);
|
|
}
|
|
courseList = courses; // 将Course对象添加到列表中
|
|
return courseList;
|
|
}
|
|
|
|
Future<int> insertCourse(Course course) async {
|
|
return await CourseDao().insertCourse(course);
|
|
}
|
|
|
|
Future<int> insertCourseList(List<Course> courseList) async {
|
|
int result = 0;
|
|
for(Course course in courseList){
|
|
result += await CourseDao().insertCourse(course);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Future<int> autoImportCours(int stuid,String passwd,int year, int term) async {
|
|
String jsonstr = await netWorkController.getUserCoursejson(stuid, passwd, year, term);
|
|
List<Course> courseList = await getCourseByLogin.dealRawString(jsonstr);
|
|
return await insertCourseList(courseList);
|
|
}
|
|
|
|
//test_autoImportCours
|
|
Future<int> test_autoImportCours(String jsonstr) async {
|
|
List<Course> courseList = await GetCourseByLogin().dealRawString(jsonstr);
|
|
return await insertCourseList(courseList);
|
|
}
|
|
|
|
|
|
Future<int> deleteAllCourses() async {
|
|
return await CourseDao().deleteAllCourses();
|
|
}
|
|
|
|
Future<int> deleteCourse(int id) async {
|
|
return await CourseDao().deleteCourseById(id);
|
|
}
|
|
|
|
}
|