11#include " DBLayer.h"
2-
2+ #include " Utility.h"
3+ #include < QSqlDatabase>
4+ #include < QSqlQuery>
5+ #include < QSqlError>
36#include < utility>
47
5- #include " Utility.h"
68
79DBLayer::DBLayer (std::string db_file_name) : db_file_name_(std::move(db_file_name))
810{
@@ -70,7 +72,8 @@ DBLayer::DBLayer(std::string db_file_name) : db_file_name_(std::move(db_file_nam
7072 " userId INTEGER, "
7173 " recordDate TEXT, "
7274 " recordTime TEXT, "
73- " record TEXT)" ;
75+ " recordDuration TEXT, "
76+ " recordMark TEXT)" ;
7477 if (!query.exec (pomoSql))
7578 {
7679 qDebug () << " PomodoroTable 创建失败:" << query.lastError ().text ();
@@ -193,7 +196,7 @@ std::vector<Habit> DBLayer::getHabitLists() const
193196 return habits;
194197}
195198
196- bool DBLayer::insertHabit (const Habit &habit)
199+ bool DBLayer::insertHabit (const Habit &habit) const
197200{
198201 if (!openDatabase ())
199202 {
@@ -222,7 +225,7 @@ bool DBLayer::insertHabit(const Habit &habit)
222225 return true ;
223226}
224227
225- bool DBLayer::updateHabit (const Habit &habit)
228+ bool DBLayer::updateHabit (const Habit &habit) const
226229{
227230 if (!openDatabase ())
228231 {
@@ -253,7 +256,7 @@ bool DBLayer::updateHabit(const Habit &habit)
253256 return true ;
254257}
255258
256- bool DBLayer::deleteHabit (std::size_t habit_id)
259+ bool DBLayer::deleteHabit (std::size_t habit_id) const
257260{
258261 if (!openDatabase ())
259262 {
@@ -274,7 +277,7 @@ bool DBLayer::deleteHabit(std::size_t habit_id)
274277 return true ;
275278}
276279
277- bool DBLayer::insertHabitRecord (const Habit &habit)
280+ bool DBLayer::insertHabitRecord (const Habit &habit) const
278281{
279282 if (!openDatabase ())
280283 {
@@ -341,7 +344,7 @@ std::vector<Event> DBLayer::getEventLists() const
341344 return events;
342345}
343346
344- bool DBLayer::insertEvent (const Event &event)
347+ bool DBLayer::insertEvent (const Event &event) const
345348{
346349 if (!openDatabase ())
347350 {
@@ -371,7 +374,7 @@ bool DBLayer::insertEvent(const Event &event)
371374 return true ;
372375}
373376
374- bool DBLayer::updateEvent (const Event &event)
377+ bool DBLayer::updateEvent (const Event &event) const
375378{
376379 if (!openDatabase ())
377380 {
@@ -403,7 +406,7 @@ bool DBLayer::updateEvent(const Event &event)
403406 return true ;
404407}
405408
406- bool DBLayer::deleteEvent (std::size_t event_id)
409+ bool DBLayer::deleteEvent (std::size_t event_id) const
407410{
408411 if (!openDatabase ())
409412 {
@@ -424,31 +427,39 @@ bool DBLayer::deleteEvent(std::size_t event_id)
424427 return true ;
425428}
426429
427- void DBLayer::insertPomoRecord (Pomodoro pomo)
430+ void DBLayer::insertPomoRecord (const Pomodoro& pomo) const
428431{
429432 if (!openDatabase ())
430433 {
431- qDebug () << " 数据库打开失败,无法插入番茄钟记录 " ;
434+ qDebug () << " 数据库打开失败: " << db_. lastError (). text () ;
432435 return ;
433436 }
434- Utility utility;
437+
438+ QDateTime currentDateTime = QDateTime::currentDateTime ();
439+ QString dateStr = currentDateTime.toString (" yyyy-MM-dd" );
440+ QString timeStr = currentDateTime.toString (" HH:mm:ss" );
435441
436442 QSqlQuery query (db_);
437- query.prepare (" INSERT INTO PomodoroTable (userId, recordDate, recordTime, record) "
438- " VALUES (:userId, :recordDate, :recordTime, :record)" );
439- query.bindValue (" :userId" , 0 ); // 假设 userId 为 0
440- query.bindValue (" :recordDate" , QString::fromStdString (toString (utility.getCurrentTimeStamp ().first )));
441- query.bindValue (" :recordTime" , QString::fromStdString (toString (pomo.pomodoro_time )));
442- query.bindValue (" :record" , QString::fromStdString (pomo.record ));
443+ query.prepare (" INSERT INTO PomodoroTable (userId, recordDate, recordTime, recordDuration, recordMark) "
444+ " VALUES (:userId, :recordDate, :recordTime, :recordDuration, :recordMark)" );
445+
446+ // 确保所有参数都正确绑定
447+ query.bindValue (" :userId" , getCurrentUserID ());
448+ query.bindValue (" :recordDate" , dateStr);
449+ query.bindValue (" :recordTime" , timeStr);
450+ query.bindValue (" :recordDuration" , QString::fromStdString (toString (pomo.pomodoro_duration )));
451+ query.bindValue (" :recordMark" , QString::fromStdString (pomo.record ));
443452
444453 if (!query.exec ())
445454 {
446455 qDebug () << " 插入番茄钟记录失败:" << query.lastError ().text ();
456+ qDebug () << " 执行的SQL:" << query.lastQuery ();
457+ qDebug () << " 绑定的值:" << query.boundValues ();
447458 }
448459 closeDatabase ();
449460}
450461
451- DateRecord DBLayer::getRecordbyDate (Date date) const
462+ DateRecord DBLayer::getRecordByDate (Date date) const
452463{
453464 std::vector<std::pair<Time, Habit>> habit_records;
454465 std::vector<std::pair<Time, Pomodoro>> pomodoro_records;
@@ -492,28 +503,28 @@ DateRecord DBLayer::getRecordbyDate(Date date) const
492503 }
493504
494505 // 查询指定日期的事项记录
495- QSqlQuery eventQuery (db_);
496- eventQuery .prepare (" SELECT * FROM EventTable WHERE eventDate = :date AND isDeleted = 0" );
497- eventQuery .bindValue (" :date" , dateStr);
506+ QSqlQuery event_query (db_);
507+ event_query .prepare (" SELECT * FROM EventTable WHERE eventDate = :date AND isDeleted = 0" );
508+ event_query .bindValue (" :date" , dateStr);
498509
499- if (!eventQuery .exec ())
510+ if (!event_query .exec ())
500511 {
501- qDebug () << " 查询指定日期的事项记录失败:" << eventQuery .lastError ().text ();
512+ qDebug () << " 查询指定日期的事项记录失败:" << event_query .lastError ().text ();
502513 }
503514 else
504515 {
505- while (eventQuery .next ())
516+ while (event_query .next ())
506517 {
507518 Event event{
508- eventQuery .value (" eventId" ).toULongLong (),
509- eventQuery .value (" userId" ).toULongLong (),
510- eventQuery .value (" title" ).toString ().toStdString (),
511- dateFromString (eventQuery .value (" eventDate" ).toString ().toStdString ()),
512- timeFromString (eventQuery .value (" eventTime" ).toString ().toStdString ()),
513- eventQuery .value (" remindFlag" ).toBool (),
514- timeFromString (eventQuery .value (" remindTime" ).toString ().toStdString ()),
515- eventQuery .value (" isExpiredFlag" ).toBool (),
516- eventQuery .value (" isDeleted" ).toBool ()};
519+ event_query .value (" eventId" ).toULongLong (),
520+ event_query .value (" userId" ).toULongLong (),
521+ event_query .value (" title" ).toString ().toStdString (),
522+ dateFromString (event_query .value (" eventDate" ).toString ().toStdString ()),
523+ timeFromString (event_query .value (" eventTime" ).toString ().toStdString ()),
524+ event_query .value (" remindFlag" ).toBool (),
525+ timeFromString (event_query .value (" remindTime" ).toString ().toStdString ()),
526+ event_query .value (" isExpiredFlag" ).toBool (),
527+ event_query .value (" isDeleted" ).toBool ()};
517528
518529 // 使用事项的时间作为记录时间
519530 Time time = event.event_time ;
@@ -522,38 +533,40 @@ DateRecord DBLayer::getRecordbyDate(Date date) const
522533 }
523534
524535 // 查询指定日期的番茄钟使用记录
525- QSqlQuery pomodoroQuery (db_);
526- pomodoroQuery .prepare (" SELECT * FROM PomodoroTable WHERE recordDate = :date" );
527- pomodoroQuery .bindValue (" :date" , dateStr);
536+ QSqlQuery pomodoro_query (db_);
537+ pomodoro_query .prepare (" SELECT * FROM PomodoroTable WHERE recordDate = :date" );
538+ pomodoro_query .bindValue (" :date" , dateStr);
528539
529- if (!pomodoroQuery .exec ())
540+ if (!pomodoro_query .exec ())
530541 {
531- qDebug () << " 查询指定日期的番茄钟使用记录失败:" << pomodoroQuery .lastError ().text ();
542+ qDebug () << " 查询指定日期的番茄钟使用记录失败:" << pomodoro_query .lastError ().text ();
532543 }
533544 else
534545 {
535- while (pomodoroQuery .next ())
546+ while (pomodoro_query .next ())
536547 {
537548 // 提取数据库中的字段
538- std::size_t id = pomodoroQuery.value (" pomoId" ).toULongLong ();
539- QString recordTimeStr = pomodoroQuery.value (" recordTime" ).toString ();
540- std::string record = pomodoroQuery.value (" record" ).toString ().toStdString ();
549+ std::size_t id = pomodoro_query.value (" pomoId" ).toULongLong ();
550+ QString record_end_time = pomodoro_query.value (" recordTime" ).toString ();
551+ QString record_duration_str = pomodoro_query.value (" recordDuration" ).toString ();
552+ std::string record = pomodoro_query.value (" recordMark" ).toString ().toStdString ();
541553
542554 // 将时间字符串转换为 Time 对象
543- Time time = timeFromString (recordTimeStr.toStdString ());
555+ Time duration = timeFromString (record_duration_str.toStdString ());
556+ Time end_time = timeFromString (record_end_time.toStdString ());
544557
545558 // 构造 Pomodoro 对象
546- Pomodoro pomodoro (id, time , record);
559+ Pomodoro pomodoro (id, duration , record);
547560
548- pomodoro_records.emplace_back (time , pomodoro);
561+ pomodoro_records.emplace_back (end_time , pomodoro);
549562 }
550563 }
551564
552565 closeDatabase ();
553566 return DateRecord (habit_records, pomodoro_records, event_records);
554567}
555568
556- int DBLayer::getHabitIDMax ()
569+ int DBLayer::getHabitIDMax () const
557570{
558571 if (!openDatabase ())
559572 {
@@ -580,7 +593,7 @@ int DBLayer::getHabitIDMax()
580593 return maxId;
581594}
582595
583- int DBLayer::getEventIDMax ()
596+ int DBLayer::getEventIDMax () const
584597{
585598 if (!openDatabase ())
586599 {
@@ -607,7 +620,7 @@ int DBLayer::getEventIDMax()
607620 return maxId;
608621}
609622
610- int DBLayer::getPomoIDMax ()
623+ int DBLayer::getPomoIDMax () const
611624{
612625 if (!openDatabase ())
613626 {
@@ -747,32 +760,17 @@ std::size_t DBLayer::getCurrentUserID() const
747760{
748761 std::size_t currentUserId = 0 ;
749762
750- if (!openDatabase ())
751- {
752- qDebug () << " 数据库打开失败,无法获取当前用户ID" ;
753- return currentUserId;
754- }
755-
756763 QSqlQuery query (db_);
757764 query.prepare (" SELECT userId FROM UserSettingsTable WHERE isLoggedIn = 1 LIMIT 1" );
758765
759- if (!query.exec ())
760- {
761- qDebug () << " 查询当前用户ID失败:" << query.lastError ().text ();
762- closeDatabase ();
763- return currentUserId;
764- }
765-
766- if (query.next ())
767- {
766+ if (query.exec () && query.next ()) {
768767 currentUserId = query.value (" userId" ).toULongLong ();
769768 }
770769
771- closeDatabase ();
772770 return currentUserId;
773771}
774772
775- bool DBLayer::setInactiveHabit (std::size_t habit_id)
773+ bool DBLayer::setInactiveHabit (const std::size_t habit_id) const
776774{
777775 if (!openDatabase ())
778776 {
@@ -803,7 +801,7 @@ bool DBLayer::setInactiveHabit(std::size_t habit_id)
803801 return true ;
804802}
805803
806- bool DBLayer::setActiveHabit (std::size_t habit_id)
804+ bool DBLayer::setActiveHabit (std::size_t habit_id) const
807805{
808806 if (!openDatabase ())
809807 {
@@ -834,7 +832,7 @@ bool DBLayer::setActiveHabit(std::size_t habit_id)
834832 return true ;
835833}
836834
837- bool DBLayer::savePomodoroState (int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time)
835+ bool DBLayer::savePomodoroState (int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time) const
838836{
839837 if (!openDatabase ())
840838 {
@@ -882,7 +880,7 @@ bool DBLayer::savePomodoroState(int state, int total_seconds, int remaining_seco
882880 return true ;
883881}
884882
885- bool DBLayer::loadPomodoroState (int & state, int & total_seconds, int & remaining_seconds, std::string& remark, std::string& start_time)
883+ bool DBLayer::loadPomodoroState (int & state, int & total_seconds, int & remaining_seconds, std::string& remark, std::string& start_time) const
886884{
887885 if (!openDatabase ())
888886 {
@@ -934,7 +932,7 @@ bool DBLayer::loadPomodoroState(int& state, int& total_seconds, int& remaining_s
934932 return false ; // 没有找到状态记录
935933}
936934
937- bool DBLayer::clearPomodoroState ()
935+ bool DBLayer::clearPomodoroState () const
938936{
939937 if (!openDatabase ())
940938 {
0 commit comments