-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.cpp
More file actions
215 lines (177 loc) · 7.12 KB
/
Copy pathFileManager.cpp
File metadata and controls
215 lines (177 loc) · 7.12 KB
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
// FileManager.cpp
#include "FileManager.h"
#include "qdebug.h"
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QFileInfo>
#include <QDir>
FileManager::FileManager() {}
Computer_type FileManager::stringToComputerType(const QString& str) {
if (str == "IDLE") return IDLE;
if (str == "IN_USE") return IN_USE;
if (str == "MAINTENANCE") return MAINTENANCE;
if (str == "FAULT") return FAULT;
return IDLE; // 默认返回空闲状态
}
QString FileManager::escapeCsvField(const QString& field) {
// 处理包含引号或逗号的字段
if (field.contains('"') || field.contains(',') || field.contains('\n')) {
QString escaped = field;
escaped.replace('"', "\"\""); // 双引号转义
return "\"" + escaped + "\""; // 用引号包裹字段
}
return field;
}
QString FileManager::computerTypeToString(Computer_type type) {
switch (type) {
case IDLE: return "IDLE";
case IN_USE: return "IN_USE";
case MAINTENANCE: return "MAINTENANCE";
case FAULT: return "FAULT";
default: return "UNKNOWN";
}
}
void FileManager::importAccountsFromCSV(LoginManager* loginManager, const QString& filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return;
}
QTextStream in(&file);
// 跳过表头
in.readLine();
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
if (line.isEmpty()) continue;
QStringList fields = line.split(',');
if (fields.size() < 3) continue;
QString userType = fields[0];
QString username = fields[1];
QString password = fields[2];
if (userType == "Admin") {
loginManager->addAdminAccount(username, password);
} else if (userType == "Student") {
loginManager->addStudentAccount(username, password);
}
}
file.close();
}
void FileManager::importDataFromCSV(ComputerClassroom& classroom, const QString& filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "打开文件失败:" << filePath;
return;
}
QTextStream in(&file);
// 跳过表头
in.readLine();
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
// 跳过空行
if (line.isEmpty()) continue;
QStringList fields = line.split(',');
// 检查基本字段数量
if (fields.isEmpty()) continue;
if (fields[0] == "Computer") {
if (fields.size() < 5) {
qWarning() << "无效的Computer记录:" << line;
continue;
}
std::string pcid = fields[1].toStdString();
bool ok;
int hor = fields[2].toInt(&ok);
if (!ok || hor <= 0) {
qWarning() << "无效的水平位置:" << fields[2];
continue;
}
int ver = fields[3].toInt(&ok);
if (!ok || ver <= 0) {
qWarning() << "无效的垂直位置:" << fields[3];
continue;
}
Computer_type type = stringToComputerType(fields[4]);
std::list<UsageRecord> usageRecords;
int i = 5;
while (i + 3 < fields.size()) { // 确保至少有 name, id, login, logout 四个字段
std::string name = fields[i].toStdString();
std::string id = fields[i + 1].toStdString();
std::string login = fields[i + 2].toStdString();
std::string logout = fields[i + 3].toStdString();
UsageRecord record(name, id, login, logout);
usageRecords.push_back(record);
i += 4; // 移动到下一组使用记录
}
qDebug() << "导入计算机:" << QString::fromStdString(pcid) << "位置:" << hor << "," << ver<<type;
// 检查坐标是否在教室范围内
if (hor > 5 || ver >10) {
qWarning() << "计算机位置超出教室范围:" << hor << "," << ver;
continue;
}
Computer computer(pcid, hor, ver, type, usageRecords);
//qDebug()<<computer.getComputerType();
classroom.setcomputer(computer, hor-1, ver-1);
} else if (fields[0] == "ClassSchedule") {
if (fields.size() < 5) {
qWarning() << "无效的ClassSchedule记录:" << line;
continue;
}
bool ok;
int week = fields[1].toInt(&ok);
if (!ok || week < 1 || week > 5) {
qWarning() << "无效的星期值:" << fields[1];
continue;
}
int time = fields[2].toInt(&ok);
if (!ok || time < 1 || time > 4) {
qWarning() << "无效的时间段值:" << fields[2];
continue;
}
QString classname = fields[3];
QString teacher = fields[4];
qDebug() << "导入课程:" << classname << "星期:" << week << "时段:" << time;
classroom.handleSaveCourseData(week, time, classname, teacher);
} else {
qWarning() << "未知记录类型:" << fields[0];
}
}
file.close();
}
void FileManager::exportDataToCSV(const ComputerClassroom& classroom, const QString& filePath) {
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "无法打开文件进行写入:" << filePath;
return;
}
QTextStream out(&file);
// 写入表头
out << "Type,PCID,Horizontal,Vertical,Status,UsageName,UsageID,LoginTime,LogoutTime,..." << "\n";
// 写入计算机信息
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 10; ++j) {
const Computer& computer = classroom.getcomputer(i + 1, j + 1);
out << "Computer,";
out << escapeCsvField(QString::fromStdString(computer.getPcid())) << ",";
out << computer.getLocation().get_hor() << ",";
out << computer.getLocation().get_ver() << ",";
out << computerTypeToString(computer.getComputerType());
const std::list<UsageRecord>& usageRecords = computer.getUsagerecord();
for (const auto& record : usageRecords) {
out << "," << escapeCsvField(QString::fromStdString(record.getUserName()));
out << "," << escapeCsvField(QString::fromStdString(record.getStudentId()));
out << "," << escapeCsvField(QString::fromStdString(record.getLoginTimeString()));
out << "," << escapeCsvField(QString::fromStdString(record.getLogoutTimeString()));
}
out << "\n";
}
}
// 写入课程信息
std::list<ClassSchInf> classSchedules = classroom.getCSI();
for ( auto& schedule : classSchedules) {
out << "ClassSchedule,";
out << schedule.getweek() << ",";
out << schedule.gettime() << ",";
out << escapeCsvField(QString::fromStdString(schedule.getclassname())) << ",";
out << escapeCsvField(QString::fromStdString(schedule.getteacher())) << "\n";
}
file.close();
}