{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceLayer.cpp
More file actions
380 lines (315 loc) · 10 KB
/
Copy pathServiceLayer.cpp
File metadata and controls
380 lines (315 loc) · 10 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
#include "ServiceLayer.h"
#include <utility>
#include "Utility.h"
#include <QFile>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <ranges>
bool ServiceLayer::insertHabit(const std::string& name, const Date& start_date, const Date& end_date, std::size_t times_per_day)
{
auto times = this->getCurrentTimeStamp();
Date cur_date = times.first;
Time cur_time = times.second;
if (name.empty() || times_per_day <= 0 || start_date > end_date || end_date < cur_date)
{
return false;
}
this->db_layer.insertHabit(Habit{ 0, 0, name, times_per_day,
start_date, end_date, true, false });
return true;
}
bool ServiceLayer::updateHabit(std::size_t habit_id, std::string new_name, const Date& start_date, const Date& end_date,
std::size_t times_per_day, bool active_flag)
{
auto times = this->getCurrentTimeStamp();
Date cur_date = times.first;
Time cur_time = times.second;
if (times_per_day <= 0 || start_date > end_date || end_date < cur_date)
{
return false;
}
Habit habit = this->getHabitByID(habit_id);
habit.name = std::move(new_name);
habit.start_date = start_date;
habit.end_date = end_date;
habit.target_count = times_per_day;
habit.is_active = active_flag;
this->db_layer.updateHabit(habit);
return true;
}
bool ServiceLayer::deleteHabit(std::size_t habit_id)
{
this->db_layer.deleteHabit(habit_id);
return true;
}
bool ServiceLayer::insertEvent(const std::string& name, const Date& event_date, const Time& event_time, bool remind_flag,
const Time& remind_time)
{
auto times = this->getCurrentTimeStamp();
Date cur_date = times.first;
Time cur_time = times.second;
if (name.empty() || remind_time > event_time || cur_date > event_date)
{
return false;
}
this->db_layer.insertEvent(Event{ 0, 0, name,
event_date, event_time, remind_flag, remind_time });
return true;
}
bool ServiceLayer::updateEvent(std::size_t event_id, std::string title, const Date& event_date, const Time& event_time,
bool remind_flag, const Time& remind_time)
{
auto times = this->getCurrentTimeStamp();
Date cur_date = times.first;
Time cur_time = times.second;
if (remind_time > event_time || cur_date > event_date)
{
return false;
}
Event event = this->getEventByID(event_id);
event.title = std::move(title);
event.event_date = event_date;
event.event_time = event_time;
event.remind_flag = remind_flag;
event.remind_time = remind_time;
this->db_layer.updateEvent(event);
return true;
}
bool ServiceLayer::deleteEvent(int event_id)
{
this->db_layer.deleteEvent(event_id);
return true;
}
bool ServiceLayer::checkinHabit(const Habit& habit)
{
this->db_layer.insertHabitRecord(habit);
return true;
}
std::vector<Habit> ServiceLayer::getActiveHabits() const
{
std::vector<Habit> all_habits = this->db_layer.getHabitLists();
std::vector<Habit> active_habits;
for (const auto& habit : all_habits)
{
if (habit.is_active && !habit.is_deleted)
{
active_habits.push_back(habit);
}
}
return active_habits;
}
std::vector<Habit> ServiceLayer::getInactiveHabits() const
{
std::vector<Habit> all_habits = this->db_layer.getHabitLists();
std::vector<Habit> inactive_habits;
for (const auto& habit : all_habits)
{
if (!habit.is_active || habit.is_deleted)
{
inactive_habits.push_back(habit);
}
}
return inactive_habits;
}
std::vector<Event> ServiceLayer::getActiveEvents() const
{
std::vector<Event> all_events = this->db_layer.getEventLists();
std::vector<Event> active_events;
for (const auto& event : all_events) {
if (!event.is_expired_flag && !event.is_deleted) {
active_events.push_back(event);
}
}
return active_events;
}
bool ServiceLayer::inactiveHabit(std::size_t habit_id)
{
return db_layer.setInactiveHabit(habit_id);
}
bool ServiceLayer::activeHabit(std::size_t habit_id)
{
return db_layer.setActiveHabit(habit_id);
}
std::vector<Event> ServiceLayer::getExpiredEvents() const
{
std::vector<Event> all_events = this->db_layer.getEventLists();
std::vector<Event> expired_events;
for (const auto& event : all_events) {
if (event.is_expired_flag || event.is_deleted) {
expired_events.push_back(event);
}
}
return expired_events;
}
bool ServiceLayer::insertPomoRecord(const Pomodoro& pomodoro)
{
db_layer.insertPomoRecord(pomodoro);
return true;
}
std::vector<std::pair<std::size_t, std::size_t>> ServiceLayer::getHabitRecordsByDate(const Date& date) const
{
std::vector<std::pair<std::size_t, std::size_t>> stats;
auto y = date.year();
auto m = date.month();
unsigned days_in_month = static_cast<unsigned>
(
std::chrono::year_month_day_last{ y, std::chrono::month_day_last{m} }.day()
);
for (unsigned d = 1; d <= days_in_month; ++d)
{
Date current_day = Date{ y / m / std::chrono::day{d} };
std::size_t should = 0, actual = 0;
for (const auto& habit : db_layer.getHabitLists())
{
if (habit.start_date <= current_day && habit.end_date >= current_day)
should += habit.target_count;
}
actual = db_layer.getRecordByDate(current_day).habit_records.size(); // 注意字段名
stats.emplace_back(actual, should);
}
return stats;
}
DateRecord ServiceLayer::getAllRecordsByDate(const Date& date) const
{
// 获取数据库中的原始数据
return db_layer.getRecordByDate(date);
}
std::pair<Date, Time> ServiceLayer::getCurrentTimeStamp()
{
auto now_time=std::chrono::system_clock::now();
auto local_time=std::chrono::current_zone()->to_local(now_time);
auto time_since_midnight = local_time - std::chrono::floor<std::chrono::days>(local_time);
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(time_since_midnight);
return
{
std::chrono::year_month_day(std::chrono::floor<std::chrono::days>(local_time)),
std::chrono::hh_mm_ss(seconds)
};
}
Habit ServiceLayer::getHabitByID(std::size_t habit_id) const
{
for (const auto& habit : this->db_layer.getHabitLists())
{
if (habit.habit_id == habit_id)
{
return habit;
}
}
return Habit();
}
Event ServiceLayer::getEventByID(std::size_t event_id) const
{
for (const auto& event : this->db_layer.getEventLists())
{
if (event.event_id == event_id)
{
return event;
}
}
return Event();
}
std::vector<Habit> ServiceLayer::getHabitsByDate(const QDate date) const
{
std::vector<Habit> habits;
for (const auto& habit : this->db_layer.getHabitLists())
{
if (habit.start_date <= Utility::convertQTDateToDate(date) &&
Utility::convertQTDateToDate(date) <= habit.end_date)
{
habits.emplace_back(habit);
}
}
return habits;
}
std::vector<Event> ServiceLayer::getEventsByDate(const QDate date) const
{
std::vector<Event> events;
for (const auto& event : this->db_layer.getEventLists())
{
if (Utility::convertQTDateToDate(date) == event.event_date)
{
events.emplace_back(event);
}
}
return events;
}
std::size_t ServiceLayer::getHabitCheckInCount(const Habit& habit) const
{
const auto today = std::chrono::year_month_day(std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now()));
DateRecord records_today = getAllRecordsByDate(today);
std::size_t count = 0;
for (const auto& val : records_today.habit_records | std::views::values) {
if (val.habit_id == habit.habit_id) {
count += 1;
}
}
return count;
}
void ServiceLayer::init()
{
}
QStringList ServiceLayer::getAvailableThemes()
{
QFile file(THEMES_PATH);
if (!file.open(QIODevice::ReadOnly)) {
return QStringList();
}
const QByteArray data = file.readAll();
file.close();
const QJsonDocument doc = QJsonDocument::fromJson(data);
QJsonArray themes = doc.array();
QStringList theme_names;
for (const auto& value : themes) {
theme_names.append(value.toString());
}
return theme_names;
}
QString ServiceLayer::getCurrentThemeName()
{
QFile file(CURRENT_THEME_PATH);
if (!file.open(QIODevice::ReadOnly)) {
return "default"; // 默认主题
}
const QByteArray data = file.readAll();
file.close();
const QJsonDocument doc = QJsonDocument::fromJson(data);
return doc.object()["current_theme"].toString();
}
bool ServiceLayer::setCurrentTheme(const QString &theme_name)
{
QFile file(CURRENT_THEME_PATH);
if (!file.open(QIODevice::WriteOnly)) {
return false;
}
QJsonObject obj;
obj["current_theme"] = theme_name;
const QJsonDocument doc(obj);
file.write(doc.toJson());
file.close();
return true;
}
QJsonObject ServiceLayer::getThemeConfig(const QString &theme_name)
{
const QString theme_path = QString(":/themes/%1.json").arg(theme_name);
QFile file(theme_path);
if (!file.open(QIODevice::ReadOnly)) {
return QJsonObject();
}
const QByteArray data = file.readAll();
file.close();
const QJsonDocument doc = QJsonDocument::fromJson(data);
return doc.object();
}
bool ServiceLayer::savePomodoroState(int state, int total_seconds, int remaining_seconds, const std::string& remark, const std::string& start_time) const
{
return db_layer.savePomodoroState(state, total_seconds, remaining_seconds, remark, start_time);
}
bool ServiceLayer::loadPomodoroState(int& state, int& total_seconds, int& remaining_seconds, std::string& remark, std::string& start_time) const
{
return db_layer.loadPomodoroState(state, total_seconds, remaining_seconds, remark, start_time);
}
bool ServiceLayer::clearPomodoroState() const
{
return db_layer.clearPomodoroState();
}
You can’t perform that action at this time.
