@@ -115,6 +115,23 @@ DBLayer::DBLayer(std::string db_file_name) : db_file_name_(std::move(db_file_nam
115115 qDebug () << " UserSettingsTable 创建成功" ;
116116 }
117117
118+ // 创建 PomodoroStateTable 用于存储番茄钟状态
119+ QString pomodoroStateSql = " CREATE TABLE IF NOT EXISTS PomodoroStateTable ("
120+ " userId INTEGER PRIMARY KEY, "
121+ " state INTEGER, "
122+ " totalSeconds INTEGER, "
123+ " remainingSeconds INTEGER, "
124+ " remark TEXT, "
125+ " startTime TEXT)" ;
126+ if (!query.exec (pomodoroStateSql))
127+ {
128+ qDebug () << " PomodoroStateTable 创建失败:" << query.lastError ().text ();
129+ }
130+ else
131+ {
132+ qDebug () << " PomodoroStateTable 创建成功" ;
133+ }
134+
118135 // 构造函数中初始化完后关闭数据库
119136 closeDatabase ();
120137}
@@ -816,3 +833,143 @@ bool DBLayer::setActiveHabit(std::size_t habit_id)
816833 closeDatabase ();
817834 return true ;
818835}
836+
837+ bool DBLayer::savePomodoroState (int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time)
838+ {
839+ if (!openDatabase ())
840+ {
841+ qDebug () << " 数据库打开失败,无法保存番茄钟状态" ;
842+ return false ;
843+ }
844+
845+ std::size_t currentUserId = getCurrentUserID ();
846+ if (currentUserId == 0 )
847+ {
848+ // 如果没有登录用户,使用默认用户ID 1
849+ currentUserId = 1 ;
850+ // qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
851+ }
852+
853+ // 确保数据库连接仍然打开
854+ if (!db_.isOpen ()) {
855+ // qDebug() << "数据库连接已关闭,重新打开";
856+ if (!openDatabase ()) {
857+ // qDebug() << "重新打开数据库失败";
858+ return false ;
859+ }
860+ }
861+
862+ QSqlQuery query (db_);
863+ query.prepare (" INSERT OR REPLACE INTO PomodoroStateTable "
864+ " (userId, state, totalSeconds, remainingSeconds, remark, startTime) "
865+ " VALUES (:userId, :state, :totalSeconds, :remainingSeconds, :remark, :startTime)" );
866+
867+ query.bindValue (" :userId" , static_cast <int >(currentUserId));
868+ query.bindValue (" :state" , state);
869+ query.bindValue (" :totalSeconds" , total_seconds);
870+ query.bindValue (" :remainingSeconds" , remaining_seconds);
871+ query.bindValue (" :remark" , QString::fromStdString (remark));
872+ query.bindValue (" :startTime" , QString::fromStdString (start_time));
873+
874+ if (!query.exec ())
875+ {
876+ qDebug () << " 保存番茄钟状态失败:" << query.lastError ().text ();
877+ closeDatabase ();
878+ return false ;
879+ }
880+
881+ closeDatabase ();
882+ return true ;
883+ }
884+
885+ bool DBLayer::loadPomodoroState (int & state, int & total_seconds, int & remaining_seconds, std::string& remark, std::string& start_time)
886+ {
887+ if (!openDatabase ())
888+ {
889+ qDebug () << " 数据库打开失败,无法加载番茄钟状态" ;
890+ return false ;
891+ }
892+
893+ std::size_t currentUserId = getCurrentUserID ();
894+ if (currentUserId == 0 )
895+ {
896+ // 如果没有登录用户,使用默认用户ID 1
897+ currentUserId = 1 ;
898+ // qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
899+ }
900+
901+ // 确保数据库连接仍然打开
902+ if (!db_.isOpen ()) {
903+ // qDebug() << "数据库连接已关闭,重新打开";
904+ if (!openDatabase ()) {
905+ // qDebug() << "重新打开数据库失败";
906+ return false ;
907+ }
908+ }
909+
910+ QSqlQuery query (db_);
911+ query.prepare (" SELECT state, totalSeconds, remainingSeconds, remark, startTime "
912+ " FROM PomodoroStateTable WHERE userId = :userId" );
913+ query.bindValue (" :userId" , static_cast <int >(currentUserId));
914+
915+ if (!query.exec ())
916+ {
917+ qDebug () << " 加载番茄钟状态失败:" << query.lastError ().text ();
918+ closeDatabase ();
919+ return false ;
920+ }
921+
922+ if (query.next ())
923+ {
924+ state = query.value (" state" ).toInt ();
925+ total_seconds = query.value (" totalSeconds" ).toInt ();
926+ remaining_seconds = query.value (" remainingSeconds" ).toInt ();
927+ remark = query.value (" remark" ).toString ().toStdString ();
928+ start_time = query.value (" startTime" ).toString ().toStdString ();
929+ closeDatabase ();
930+ return true ;
931+ }
932+
933+ closeDatabase ();
934+ return false ; // 没有找到状态记录
935+ }
936+
937+ bool DBLayer::clearPomodoroState ()
938+ {
939+ if (!openDatabase ())
940+ {
941+ qDebug () << " 数据库打开失败,无法清除番茄钟状态" ;
942+ return false ;
943+ }
944+
945+ std::size_t currentUserId = getCurrentUserID ();
946+ if (currentUserId == 0 )
947+ {
948+ // 如果没有登录用户,使用默认用户ID 1
949+ currentUserId = 1 ;
950+ // qDebug() << "没有当前登录用户,使用默认用户ID:" << currentUserId;
951+ }
952+
953+ // 确保数据库连接仍然打开
954+ if (!db_.isOpen ()) {
955+ // qDebug() << "数据库连接已关闭,重新打开";
956+ if (!openDatabase ()) {
957+ // qDebug() << "重新打开数据库失败";
958+ return false ;
959+ }
960+ }
961+
962+ QSqlQuery query (db_);
963+ query.prepare (" DELETE FROM PomodoroStateTable WHERE userId = :userId" );
964+ query.bindValue (" :userId" , static_cast <int >(currentUserId));
965+
966+ if (!query.exec ())
967+ {
968+ qDebug () << " 清除番茄钟状态失败:" << query.lastError ().text ();
969+ closeDatabase ();
970+ return false ;
971+ }
972+
973+ closeDatabase ();
974+ return true ;
975+ }
0 commit comments