{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBLayer.cpp
More file actions
973 lines (855 loc) · 31 KB
/
Copy pathDBLayer.cpp
File metadata and controls
973 lines (855 loc) · 31 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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
#include "DBLayer.h"
#include "Utility.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <utility>
DBLayer::DBLayer(std::string db_file_name) : db_file_name_(std::move(db_file_name))
{
// 初始化 SQLite 数据库连接
db_ = QSqlDatabase::addDatabase("QSQLITE");
db_.setDatabaseName(QString::fromStdString(db_file_name_));
// 检查数据库文件是否存在
if (!QFile::exists(QString::fromStdString(db_file_name_)))
{
qDebug() << "数据库文件不存在,将创建新文件:" << QString::fromStdString(db_file_name_);
}
// 打开数据库
if (!db_.open())
{
qDebug() << "数据库打开失败:" << db_.lastError().text();
return;
}
// 初始化表
QSqlQuery query(db_);
// 创建 HabitTable
QString habitSql = "CREATE TABLE IF NOT EXISTS HabitTable ("
"habitId INTEGER PRIMARY KEY AUTOINCREMENT, "
"userId INTEGER, "
"name TEXT, "
"targetCount INTEGER, "
"startDate TEXT, "
"endDate TEXT, "
"isActive INTEGER, "
"isDeleted INTEGER)";
if (!query.exec(habitSql))
{
qDebug() << "HabitTable 创建失败:" << query.lastError().text();
}
else
{
qDebug() << "HabitTable 创建成功";
}
// 创建 EventTable
QString eventSql = "CREATE TABLE IF NOT EXISTS EventTable ("
"eventId INTEGER PRIMARY KEY AUTOINCREMENT, "
"userId INTEGER, "
"title TEXT, "
"eventDate TEXT, "
"eventTime TEXT, "
"remindFlag INTEGER, "
"remindTime TEXT, "
"isExpiredFlag INTEGER, "
"isDeleted INTEGER)";
if (!query.exec(eventSql))
{
qDebug() << "EventTable 创建失败:" << query.lastError().text();
}
else
{
qDebug() << "EventTable 创建成功";
}
// 创建 PomodoroTable
QString pomoSql = "CREATE TABLE IF NOT EXISTS PomodoroTable ("
"pomoId INTEGER PRIMARY KEY AUTOINCREMENT, "
"userId INTEGER, "
"recordDate TEXT, "
"recordTime TEXT, "
"recordDuration TEXT, "
"recordMark TEXT)";
if (!query.exec(pomoSql))
{
qDebug() << "PomodoroTable 创建失败:" << query.lastError().text();
}
else
{
qDebug() << "PomodoroTable 创建成功";
}
// 创建 DateRecordTable 用于存储习惯打卡记录
QString dateRecordSql = "CREATE TABLE IF NOT EXISTS DateRecordTable ("
"recordId INTEGER PRIMARY KEY AUTOINCREMENT, "
"habitId INTEGER, "
"userId INTEGER, "
"recordDate TEXT, "
"recordTime TEXT, "
"FOREIGN KEY(habitId) REFERENCES HabitTable(habitId))";
if (!query.exec(dateRecordSql))
{
qDebug() << "DateRecordTable 创建失败:" << query.lastError().text();
}
else
{
qDebug() << "DateRecordTable 创建成功";
}
// 创建 UserSettingsTable 用于存储用户设置
QString userSettingsSql = "CREATE TABLE IF NOT EXISTS UserSettingsTable ("
"userId INTEGER PRIMARY KEY, "
"nickname TEXT, "
"currentSkin TEXT, "
"ddlReminderEnabled INTEGER, "
"ddlReminderEmail TEXT, "
"lastLoginTime TEXT, "
"isLoggedIn INTEGER)";
if (!query.exec(userSettingsSql))
{
qDebug() << "UserSettingsTable 创建失败:" << query.lastError().text();
}
else
{
qDebug() << "UserSettingsTable 创建成功";
}
// 创建 PomodoroStateTable 用于存储番茄钟状态
QString pomodoroStateSql = "CREATE TABLE IF NOT EXISTS PomodoroStateTable ("
"userId INTEGER PRIMARY KEY, "
"state INTEGER, "
"totalSeconds INTEGER, "
"remainingSeconds INTEGER, "
"remark TEXT, "
"startTime TEXT)";
if (!query.exec(pomodoroStateSql))
{
qDebug() << "PomodoroStateTable 创建失败:" << query.lastError().text();
}
else
{
qDebug() << "PomodoroStateTable 创建成功";
}
// 构造函数中初始化完后关闭数据库
closeDatabase();
}
DBLayer::~DBLayer()
{
closeDatabase();
}
bool DBLayer::openDatabase() const
{
if (db_.isOpen())
{
return true;
}
return db_.open();
}
void DBLayer::closeDatabase() const
{
if (db_.isOpen())
{
db_.close();
}
}
std::vector<Habit> DBLayer::getHabitLists() const
{
std::vector<Habit> habits;
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法获取习惯列表";
return habits;
}
QSqlQuery query(db_);
QString sql = "SELECT * FROM HabitTable WHERE isDeleted = 0";
if (!query.exec(sql))
{
qDebug() << "查询习惯列表失败:" << query.lastError().text();
closeDatabase();
return habits;
}
while (query.next())
{
Habit habit{
query.value("habitId").toULongLong(),
query.value("userId").toULongLong(),
query.value("name").toString().toStdString(),
query.value("targetCount").toULongLong(),
dateFromString(query.value("startDate").toString().toStdString()),
dateFromString(query.value("endDate").toString().toStdString()),
query.value("isActive").toBool(),
query.value("isDeleted").toBool()};
habits.push_back(habit);
}
closeDatabase();
return habits;
}
bool DBLayer::insertHabit(const Habit &habit) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法插入习惯";
return false;
}
QSqlQuery query(db_);
query.prepare("INSERT INTO HabitTable (userId, name, targetCount, startDate, endDate, isActive, isDeleted) "
"VALUES (:userId, :name, :targetCount, :startDate, :endDate, :isActive, :isDeleted)");
query.bindValue(":userId", habit.user_id);
query.bindValue(":name", QString::fromStdString(habit.name));
query.bindValue(":targetCount", habit.target_count);
query.bindValue(":startDate", QString::fromStdString(toString(habit.start_date)));
query.bindValue(":endDate", QString::fromStdString(toString(habit.end_date)));
query.bindValue(":isActive", habit.is_active ? 1 : 0);
query.bindValue(":isDeleted", habit.is_deleted ? 1 : 0);
if (!query.exec())
{
qDebug() << "插入习惯失败:" << query.lastError().text();
closeDatabase();
return false;
}
closeDatabase();
return true;
}
bool DBLayer::updateHabit(const Habit &habit) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法更新习惯";
return false;
}
QSqlQuery query(db_);
query.prepare("UPDATE HabitTable SET userId = :userId, name = :name, targetCount = :targetCount, "
"startDate = :startDate, endDate = :endDate, isActive = :isActive, isDeleted = :isDeleted "
"WHERE habitId = :habitId");
query.bindValue(":habitId", habit.habit_id);
query.bindValue(":userId", habit.user_id);
query.bindValue(":name", QString::fromStdString(habit.name));
query.bindValue(":targetCount", habit.target_count);
query.bindValue(":startDate", QString::fromStdString(toString(habit.start_date)));
query.bindValue(":endDate", QString::fromStdString(toString(habit.end_date)));
query.bindValue(":isActive", habit.is_active ? 1 : 0);
query.bindValue(":isDeleted", habit.is_deleted ? 1 : 0);
if (!query.exec())
{
qDebug() << "更新习惯失败:" << query.lastError().text();
closeDatabase();
return false;
}
closeDatabase();
return true;
}
bool DBLayer::deleteHabit(std::size_t habit_id) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法删除习惯";
return false;
}
QSqlQuery query(db_);
query.prepare("UPDATE HabitTable SET isDeleted = 1 WHERE habitId = :habitId");
query.bindValue(":habitId", static_cast<int>(habit_id));
if (!query.exec())
{
qDebug() << "删除习惯失败:" << query.lastError().text();
closeDatabase();
return false;
}
closeDatabase();
return true;
}
bool DBLayer::insertHabitRecord(const Habit &habit) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法插入习惯打卡记录";
return false;
}
QSqlQuery query(db_);
// 使用 QDateTime 获取当前日期和时间
QDateTime currentDateTime = QDateTime::currentDateTime();
QString dateStr = currentDateTime.toString("yyyy-MM-dd"); // 格式化为日期字符串
QString timeStr = currentDateTime.toString("HH:mm:ss"); // 格式化为时间字符串
query.prepare("INSERT INTO DateRecordTable (habitId, userId, recordDate, recordTime) "
"VALUES (:habitId, :userId, :recordDate, :recordTime)");
query.bindValue(":habitId", habit.habit_id);
query.bindValue(":userId", habit.user_id);
query.bindValue(":recordDate", dateStr);
query.bindValue(":recordTime", timeStr);
if (!query.exec())
{
qDebug() << "插入习惯打卡记录失败:" << query.lastError().text();
closeDatabase();
return false;
}
closeDatabase();
return true;
}
std::vector<Event> DBLayer::getEventLists() const
{
std::vector<Event> events;
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法获取事项列表";
return events;
}
QSqlQuery query(db_);
QString sql = "SELECT * FROM EventTable WHERE isDeleted = 0";
if (!query.exec(sql))
{
qDebug() << "查询事项列表失败:" << query.lastError().text();
closeDatabase();
return events;
}
while (query.next())
{
Event event{
query.value("eventId").toULongLong(),
query.value("userId").toULongLong(),
query.value("title").toString().toStdString(),
dateFromString(query.value("eventDate").toString().toStdString()),
timeFromString(query.value("eventTime").toString().toStdString()),
query.value("remindFlag").toBool(),
timeFromString(query.value("remindTime").toString().toStdString()),
query.value("isExpiredFlag").toBool(),
query.value("isDeleted").toBool()};
events.push_back(event);
}
closeDatabase();
return events;
}
bool DBLayer::insertEvent(const Event &event) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法插入事项";
return false;
}
QSqlQuery query(db_);
query.prepare("INSERT INTO EventTable (userId, title, eventDate, eventTime, remindFlag, remindTime, isExpiredFlag, isDeleted) "
"VALUES (:userId, :title, :eventDate, :eventTime, :remindFlag, :remindTime, :isExpiredFlag, :isDeleted)");
query.bindValue(":userId", event.user_id);
query.bindValue(":title", QString::fromStdString(event.title));
query.bindValue(":eventDate", QString::fromStdString(toString(event.event_date)));
query.bindValue(":eventTime", QString::fromStdString(toString(event.event_time)));
query.bindValue(":remindFlag", event.remind_flag);
query.bindValue(":remindTime", QString::fromStdString(toString(event.remind_time)));
query.bindValue(":isExpiredFlag", event.is_expired_flag);
query.bindValue(":isDeleted", event.is_deleted);
if (!query.exec())
{
qDebug() << "插入事项失败:" << query.lastError().text();
closeDatabase();
return false;
}
closeDatabase();
return true;
}
bool DBLayer::updateEvent(const Event &event) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法更新事项";
return false;
}
QSqlQuery query(db_);
query.prepare("UPDATE EventTable SET userId = :userId, title = :title, eventDate = :eventDate, "
"eventTime = :eventTime, remindFlag = :remindFlag, remindTime = :remindTime, "
"isExpiredFlag = :isExpiredFlag, isDeleted = :isDeleted WHERE eventId = :eventId");
query.bindValue(":eventId", event.event_id);
query.bindValue(":userId", event.user_id);
query.bindValue(":title", QString::fromStdString(event.title));
query.bindValue(":eventDate", QString::fromStdString(toString(event.event_date)));
query.bindValue(":eventTime", QString::fromStdString(toString(event.event_time)));
query.bindValue(":remindFlag", event.remind_flag);
query.bindValue(":remindTime", QString::fromStdString(toString(event.remind_time)));
query.bindValue(":isExpiredFlag", event.is_expired_flag);
query.bindValue(":isDeleted", event.is_deleted);
if (!query.exec())
{
qDebug() << "更新事项失败:" << query.lastError().text();
closeDatabase();
return false;
}
closeDatabase();
return true;
}
bool DBLayer::deleteEvent(std::size_t event_id) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法删除事项";
return false;
}
QSqlQuery query(db_);
query.prepare("UPDATE EventTable SET isDeleted = 1 WHERE eventId = :eventId");
query.bindValue(":eventId", static_cast<int>(event_id));
if (!query.exec())
{
qDebug() << "删除事项失败:" << query.lastError().text();
closeDatabase();
return false;
}
closeDatabase();
return true;
}
void DBLayer::insertPomoRecord(const Pomodoro& pomo) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败:" << db_.lastError().text();
return;
}
QDateTime currentDateTime = QDateTime::currentDateTime();
QString dateStr = currentDateTime.toString("yyyy-MM-dd");
QString timeStr = currentDateTime.toString("HH:mm:ss");
QSqlQuery query(db_);
query.prepare("INSERT INTO PomodoroTable (userId, recordDate, recordTime, recordDuration, recordMark) "
"VALUES (:userId, :recordDate, :recordTime, :recordDuration, :recordMark)");
// 确保所有参数都正确绑定
query.bindValue(":userId", getCurrentUserID());
query.bindValue(":recordDate", dateStr);
query.bindValue(":recordTime", timeStr);
query.bindValue(":recordDuration", QString::fromStdString(toString(pomo.pomodoro_duration)));
query.bindValue(":recordMark", QString::fromStdString(pomo.record));
if (!query.exec())
{
qDebug() << "插入番茄钟记录失败:" << query.lastError().text();
qDebug() << "执行的SQL:" << query.lastQuery();
qDebug() << "绑定的值:" << query.boundValues();
}
closeDatabase();
}
DateRecord DBLayer::getRecordByDate(Date date) const
{
std::vector<std::pair<Time, Habit>> habit_records;
std::vector<std::pair<Time, Pomodoro>> pomodoro_records;
std::vector<std::pair<Time, Event>> event_records;
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法获取指定日期的记录";
return DateRecord(habit_records, pomodoro_records, event_records);
}
// 查询指定日期的习惯打卡记录
QString dateStr = QString::fromStdString(toString(date));
QSqlQuery habitQuery(db_);
habitQuery.prepare("SELECT dr.*, ht.name, ht.targetCount, ht.startDate, ht.endDate, ht.isActive, ht.isDeleted "
"FROM DateRecordTable dr "
"JOIN HabitTable ht ON dr.habitId = ht.habitId "
"WHERE dr.recordDate = :date");
habitQuery.bindValue(":date", dateStr);
if (!habitQuery.exec())
{
qDebug() << "查询指定日期的习惯打卡记录失败:" << habitQuery.lastError().text();
}
else
{
while (habitQuery.next())
{
Habit habit{
habitQuery.value("habitId").toULongLong(),
habitQuery.value("userId").toULongLong(),
habitQuery.value("name").toString().toStdString(),
habitQuery.value("targetCount").toULongLong(),
dateFromString(habitQuery.value("startDate").toString().toStdString()),
dateFromString(habitQuery.value("endDate").toString().toStdString()),
habitQuery.value("isActive").toBool(),
habitQuery.value("isDeleted").toBool()};
Time time = timeFromString(habitQuery.value("recordTime").toString().toStdString());
habit_records.emplace_back(time, habit);
}
}
// 查询指定日期的事项记录
QSqlQuery event_query(db_);
event_query.prepare("SELECT * FROM EventTable WHERE eventDate = :date AND isDeleted = 0");
event_query.bindValue(":date", dateStr);
if (!event_query.exec())
{
qDebug() << "查询指定日期的事项记录失败:" << event_query.lastError().text();
}
else
{
while (event_query.next())
{
Event event{
event_query.value("eventId").toULongLong(),
event_query.value("userId").toULongLong(),
event_query.value("title").toString().toStdString(),
dateFromString(event_query.value("eventDate").toString().toStdString()),
timeFromString(event_query.value("eventTime").toString().toStdString()),
event_query.value("remindFlag").toBool(),
timeFromString(event_query.value("remindTime").toString().toStdString()),
event_query.value("isExpiredFlag").toBool(),
event_query.value("isDeleted").toBool()};
// 使用事项的时间作为记录时间
Time time = event.event_time;
event_records.emplace_back(time, event);
}
}
// 查询指定日期的番茄钟使用记录
QSqlQuery pomodoro_query(db_);
pomodoro_query.prepare("SELECT * FROM PomodoroTable WHERE recordDate = :date");
pomodoro_query.bindValue(":date", dateStr);
if (!pomodoro_query.exec())
{
qDebug() << "查询指定日期的番茄钟使用记录失败:" << pomodoro_query.lastError().text();
}
else
{
while (pomodoro_query.next())
{
// 提取数据库中的字段
std::size_t id = pomodoro_query.value("pomoId").toULongLong();
QString record_end_time = pomodoro_query.value("recordTime").toString();
QString record_duration_str = pomodoro_query.value("recordDuration").toString();
std::string record = pomodoro_query.value("recordMark").toString().toStdString();
// 将时间字符串转换为 Time 对象
Time duration = timeFromString(record_duration_str.toStdString());
Time end_time = timeFromString(record_end_time.toStdString());
// 构造 Pomodoro 对象
Pomodoro pomodoro(id, duration, record);
pomodoro_records.emplace_back(end_time, pomodoro);
}
}
closeDatabase();
return DateRecord(habit_records, pomodoro_records, event_records);
}
int DBLayer::getHabitIDMax() const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法获取最大习惯ID";
return 0;
}
QSqlQuery query(db_);
QString sql = "SELECT MAX(habitId) as maxId FROM HabitTable";
if (!query.exec(sql))
{
qDebug() << "查询最大习惯ID失败:" << query.lastError().text();
closeDatabase();
return 0;
}
int maxId = 0;
if (query.next())
{
maxId = query.value("maxId").toInt();
}
closeDatabase();
return maxId;
}
int DBLayer::getEventIDMax() const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法获取最大事项ID";
return 0;
}
QSqlQuery query(db_);
QString sql = "SELECT MAX(eventId) as maxId FROM EventTable";
if (!query.exec(sql))
{
qDebug() << "查询最大事项ID失败:" << query.lastError().text();
closeDatabase();
return 0;
}
int maxId = 0;
if (query.next())
{
maxId = query.value("maxId").toInt();
}
closeDatabase();
return maxId;
}
int DBLayer::getPomoIDMax() const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法获取最大番茄钟ID";
return 0;
}
QSqlQuery query(db_);
QString sql = "SELECT MAX(pomoId) as maxId FROM PomodoroTable";
if (!query.exec(sql))
{
qDebug() << "查询最大番茄钟ID失败:" << query.lastError().text();
closeDatabase();
return 0;
}
int maxId = 0;
if (query.next())
{
maxId = query.value("maxId").toInt();
}
closeDatabase();
return maxId;
}
UserSettings DBLayer::getUserSettings(std::size_t user_id) const
{
UserSettings settings;
settings.user_id = user_id;
settings.nickname = "";
settings.current_skin = "default";
settings.ddl_reminder_enabled = false;
settings.ddl_reminder_email = "";
settings.last_login_time = "";
settings.is_logged_in = false;
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法获取用户设置";
return settings;
}
QSqlQuery query(db_);
query.prepare("SELECT * FROM UserSettingsTable WHERE userId = :userId");
query.bindValue(":userId", static_cast<int>(user_id));
if (!query.exec())
{
qDebug() << "查询用户设置失败:" << query.lastError().text();
closeDatabase();
return settings;
}
if (query.next())
{
settings.user_id = query.value("userId").toULongLong();
settings.nickname = query.value("nickname").toString().toStdString();
settings.current_skin = query.value("currentSkin").toString().toStdString();
settings.ddl_reminder_enabled = query.value("ddlReminderEnabled").toBool();
settings.ddl_reminder_email = query.value("ddlReminderEmail").toString().toStdString();
settings.last_login_time = query.value("lastLoginTime").toString().toStdString();
settings.is_logged_in = query.value("isLoggedIn").toBool();
}
closeDatabase();
return settings;
}
bool DBLayer::updateUserSettings(const UserSettings &settings)
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法更新用户设置";
return false;
}
QSqlQuery checkQuery(db_);
checkQuery.prepare("SELECT COUNT(*) FROM UserSettingsTable WHERE userId = :userId");
checkQuery.bindValue(":userId", static_cast<int>(settings.user_id));
if (!checkQuery.exec())
{
qDebug() << "检查用户设置记录失败:" << checkQuery.lastError().text();
closeDatabase();
return false;
}
bool recordExists = false;
if (checkQuery.next())
{
recordExists = checkQuery.value(0).toInt() > 0;
}
QSqlQuery query(db_);
if (recordExists)
{
// 更新现有记录
query.prepare("UPDATE UserSettingsTable SET "
"nickname = :nickname, "
"currentSkin = :currentSkin, "
"ddlReminderEnabled = :ddlReminderEnabled, "
"ddlReminderEmail = :ddlReminderEmail, "
"lastLoginTime = :lastLoginTime, "
"isLoggedIn = :isLoggedIn "
"WHERE userId = :userId");
}
else
{
// 插入新记录
query.prepare("INSERT INTO UserSettingsTable "
"(userId, nickname, currentSkin, ddlReminderEnabled, ddlReminderEmail, lastLoginTime, isLoggedIn) "
"VALUES "
"(:userId, :nickname, :currentSkin, :ddlReminderEnabled, :ddlReminderEmail, :lastLoginTime, :isLoggedIn)");
}
query.bindValue(":userId", static_cast<int>(settings.user_id));
query.bindValue(":nickname", QString::fromStdString(settings.nickname));
query.bindValue(":currentSkin", QString::fromStdString(settings.current_skin));
query.bindValue(":ddlReminderEnabled", settings.ddl_reminder_enabled ? 1 : 0);
query.bindValue(":ddlReminderEmail", QString::fromStdString(settings.ddl_reminder_email));
query.bindValue(":lastLoginTime", QString::fromStdString(settings.last_login_time));
query.bindValue(":isLoggedIn", settings.is_logged_in ? 1 : 0);
if (!query.exec())
{
qDebug() << "更新用户设置失败:" << query.lastError().text();
closeDatabase();
return false;
}
closeDatabase();
return true;
}
std::size_t DBLayer::getCurrentUserID() const
{
std::size_t currentUserId = 0;
QSqlQuery query(db_);
query.prepare("SELECT userId FROM UserSettingsTable WHERE isLoggedIn = 1 LIMIT 1");
if (query.exec() && query.next()) {
currentUserId = query.value("userId").toULongLong();
}
return currentUserId;
}
bool DBLayer::setInactiveHabit(const std::size_t habit_id) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法停用习惯";
return false;
}
QSqlQuery query(db_);
query.prepare("UPDATE HabitTable SET isActive = 0 WHERE habitId = :habitId AND isDeleted = 0");
query.bindValue(":habitId", static_cast<int>(habit_id));
if (!query.exec())
{
qDebug() << "停用习惯失败:" << query.lastError().text();
closeDatabase();
return false;
}
// 检查是否有行被更新
if (query.numRowsAffected() <= 0)
{
qDebug() << "停用习惯失败:未找到指定ID的习惯或习惯已被删除";
closeDatabase();
return false;
}
closeDatabase();
return true;
}
bool DBLayer::setActiveHabit(std::size_t habit_id) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法启用习惯";
return false;
}
QSqlQuery query(db_);
query.prepare("UPDATE HabitTable SET isActive = 1 WHERE habitId = :habitId AND isDeleted = 0");
query.bindValue(":habitId", static_cast<int>(habit_id));
if (!query.exec())
{
qDebug() << "启用习惯失败:" << query.lastError().text();
closeDatabase();
return false;
}
// 检查是否有行被更新
if (query.numRowsAffected() <= 0)
{
qDebug() << "启用习惯失败:未找到指定ID的习惯或习惯已被删除";
closeDatabase();
return false;
}
closeDatabase();
return true;
}
bool DBLayer::savePomodoroState(int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法保存番茄钟状态";
return false;
}
std::size_t currentUserId = getCurrentUserID();
if (currentUserId == 0)
{
// 如果没有登录用户,使用默认用户ID 1
currentUserId = 1;
//qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
}
// 确保数据库连接仍然打开
if (!db_.isOpen()) {
//qDebug() << "数据库连接已关闭,重新打开";
if (!openDatabase()) {
//qDebug() << "重新打开数据库失败";
return false;
}
}
QSqlQuery query(db_);
query.prepare("INSERT OR REPLACE INTO PomodoroStateTable "
"(userId, state, totalSeconds, remainingSeconds, remark, startTime) "
"VALUES (:userId, :state, :totalSeconds, :remainingSeconds, :remark, :startTime)");
query.bindValue(":userId", static_cast<int>(currentUserId));
query.bindValue(":state", state);
query.bindValue(":totalSeconds", total_seconds);
query.bindValue(":remainingSeconds", remaining_seconds);
query.bindValue(":remark", QString::fromStdString(remark));
query.bindValue(":startTime", QString::fromStdString(start_time));
if (!query.exec())
{
qDebug() << "保存番茄钟状态失败:" << query.lastError().text();
closeDatabase();
return false;
}
closeDatabase();
return true;
}
bool DBLayer::loadPomodoroState(int& state, int& total_seconds, int& remaining_seconds, std::string& remark, std::string& start_time) const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法加载番茄钟状态";
return false;
}
std::size_t currentUserId = getCurrentUserID();
if (currentUserId == 0)
{
// 如果没有登录用户,使用默认用户ID 1
currentUserId = 1;
//qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
}
// 确保数据库连接仍然打开
if (!db_.isOpen()) {
//qDebug() << "数据库连接已关闭,重新打开";
if (!openDatabase()) {
//qDebug() << "重新打开数据库失败";
return false;
}
}
QSqlQuery query(db_);
query.prepare("SELECT state, totalSeconds, remainingSeconds, remark, startTime "
"FROM PomodoroStateTable WHERE userId = :userId");
query.bindValue(":userId", static_cast<int>(currentUserId));
if (!query.exec())
{
qDebug() << "加载番茄钟状态失败:" << query.lastError().text();
closeDatabase();
return false;
}
if (query.next())
{
state = query.value("state").toInt();
total_seconds = query.value("totalSeconds").toInt();
remaining_seconds = query.value("remainingSeconds").toInt();
remark = query.value("remark").toString().toStdString();
start_time = query.value("startTime").toString().toStdString();
closeDatabase();
return true;
}
closeDatabase();
return false; // 没有找到状态记录
}
bool DBLayer::clearPomodoroState() const
{
if (!openDatabase())
{
qDebug() << "数据库打开失败,无法清除番茄钟状态";
return false;
}
std::size_t currentUserId = getCurrentUserID();
if (currentUserId == 0)
{
// 如果没有登录用户,使用默认用户ID 1
currentUserId = 1;
//qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
}
// 确保数据库连接仍然打开
if (!db_.isOpen()) {
//qDebug() << "数据库连接已关闭,重新打开";
if (!openDatabase()) {
//qDebug() << "重新打开数据库失败";
return false;
}
}
QSqlQuery query(db_);
query.prepare("DELETE FROM PomodoroStateTable WHERE userId = :userId");
query.bindValue(":userId", static_cast<int>(currentUserId));
if (!query.exec())
{
qDebug() << "清除番茄钟状态失败:" << query.lastError().text();
closeDatabase();
return false;
}
closeDatabase();
return true;
}
You can’t perform that action at this time.
