diff --git a/calendar-service/assets/holiday-cn/2023.json b/calendar-service/assets/holiday-cn/2023.json
new file mode 100644
index 00000000..08811878
--- /dev/null
+++ b/calendar-service/assets/holiday-cn/2023.json
@@ -0,0 +1,180 @@
+{
+ "$schema": "https://raw.githubusercontent.com/NateScarlet/holiday-cn/master/schema.json",
+ "$id": "https://raw.githubusercontent.com/NateScarlet/holiday-cn/master/2023.json",
+ "year": 2023,
+ "papers": [
+ "http://www.gov.cn/zhengce/zhengceku/2022-12/08/content_5730844.htm"
+ ],
+ "days": [
+ {
+ "name": "元旦",
+ "date": "2022-12-31",
+ "isOffDay": true
+ },
+ {
+ "name": "元旦",
+ "date": "2023-01-01",
+ "isOffDay": true
+ },
+ {
+ "name": "元旦",
+ "date": "2023-01-02",
+ "isOffDay": true
+ },
+ {
+ "name": "春节",
+ "date": "2023-01-21",
+ "isOffDay": true
+ },
+ {
+ "name": "春节",
+ "date": "2023-01-22",
+ "isOffDay": true
+ },
+ {
+ "name": "春节",
+ "date": "2023-01-23",
+ "isOffDay": true
+ },
+ {
+ "name": "春节",
+ "date": "2023-01-24",
+ "isOffDay": true
+ },
+ {
+ "name": "春节",
+ "date": "2023-01-25",
+ "isOffDay": true
+ },
+ {
+ "name": "春节",
+ "date": "2023-01-26",
+ "isOffDay": true
+ },
+ {
+ "name": "春节",
+ "date": "2023-01-27",
+ "isOffDay": true
+ },
+ {
+ "name": "春节",
+ "date": "2023-01-28",
+ "isOffDay": false
+ },
+ {
+ "name": "春节",
+ "date": "2023-01-29",
+ "isOffDay": false
+ },
+ {
+ "name": "清明节",
+ "date": "2023-04-05",
+ "isOffDay": true
+ },
+ {
+ "name": "劳动节",
+ "date": "2023-04-23",
+ "isOffDay": false
+ },
+ {
+ "name": "劳动节",
+ "date": "2023-04-29",
+ "isOffDay": true
+ },
+ {
+ "name": "劳动节",
+ "date": "2023-04-30",
+ "isOffDay": true
+ },
+ {
+ "name": "劳动节",
+ "date": "2023-05-01",
+ "isOffDay": true
+ },
+ {
+ "name": "劳动节",
+ "date": "2023-05-02",
+ "isOffDay": true
+ },
+ {
+ "name": "劳动节",
+ "date": "2023-05-03",
+ "isOffDay": true
+ },
+ {
+ "name": "劳动节",
+ "date": "2023-05-06",
+ "isOffDay": false
+ },
+ {
+ "name": "端午节",
+ "date": "2023-06-22",
+ "isOffDay": true
+ },
+ {
+ "name": "端午节",
+ "date": "2023-06-23",
+ "isOffDay": true
+ },
+ {
+ "name": "端午节",
+ "date": "2023-06-24",
+ "isOffDay": true
+ },
+ {
+ "name": "端午节",
+ "date": "2023-06-25",
+ "isOffDay": false
+ },
+ {
+ "name": "中秋节、国庆节",
+ "date": "2023-09-29",
+ "isOffDay": true
+ },
+ {
+ "name": "中秋节、国庆节",
+ "date": "2023-09-30",
+ "isOffDay": true
+ },
+ {
+ "name": "中秋节、国庆节",
+ "date": "2023-10-01",
+ "isOffDay": true
+ },
+ {
+ "name": "中秋节、国庆节",
+ "date": "2023-10-02",
+ "isOffDay": true
+ },
+ {
+ "name": "中秋节、国庆节",
+ "date": "2023-10-03",
+ "isOffDay": true
+ },
+ {
+ "name": "中秋节、国庆节",
+ "date": "2023-10-04",
+ "isOffDay": true
+ },
+ {
+ "name": "中秋节、国庆节",
+ "date": "2023-10-05",
+ "isOffDay": true
+ },
+ {
+ "name": "中秋节、国庆节",
+ "date": "2023-10-06",
+ "isOffDay": true
+ },
+ {
+ "name": "中秋节、国庆节",
+ "date": "2023-10-07",
+ "isOffDay": false
+ },
+ {
+ "name": "中秋节、国庆节",
+ "date": "2023-10-08",
+ "isOffDay": false
+ }
+ ]
+}
\ No newline at end of file
diff --git a/calendar-service/assets/resources.qrc b/calendar-service/assets/resources.qrc
index e1f91365..81a423a9 100644
--- a/calendar-service/assets/resources.qrc
+++ b/calendar-service/assets/resources.qrc
@@ -1,6 +1,7 @@
pinyin.dict
+ holiday-cn/2023.json
holiday-cn/2024.json
diff --git a/calendar-service/src/calendarhuangli.cpp b/calendar-service/src/calendarhuangli.cpp
index b7b4f10c..0d4a5a77 100644
--- a/calendar-service/src/calendarhuangli.cpp
+++ b/calendar-service/src/calendarhuangli.cpp
@@ -5,10 +5,6 @@
#include "calendarhuangli.h"
#include "lunarandfestival/lunarmanager.h"
-#include
-#include
-#include
-
CalendarHuangLi::CalendarHuangLi(QObject *parent)
: QObject(parent)
, m_database(new DHuangLiDataBase(this))
@@ -16,9 +12,9 @@ CalendarHuangLi::CalendarHuangLi(QObject *parent)
}
//获取指定公历月的假日信息
-QString CalendarHuangLi::getFestivalMonth(quint32 year, quint32 month)
+QJsonArray CalendarHuangLi::getFestivalMonth(quint32 year, quint32 month)
{
- return m_database->queryFestivalList(year, static_cast(month));
+ return m_database->queryFestivalList(year, month);
}
QString CalendarHuangLi::getHuangLiDay(quint32 year, quint32 month, quint32 day)
diff --git a/calendar-service/src/calendarhuangli.h b/calendar-service/src/calendarhuangli.h
index b5e90d79..ba9a16f0 100644
--- a/calendar-service/src/calendarhuangli.h
+++ b/calendar-service/src/calendarhuangli.h
@@ -6,6 +6,8 @@
#define CALENDARHUANGLI_H
#include "dhuanglidatabase.h"
+#include "huangliData/dbusdatastruct.h"
+#include
#include
@@ -17,7 +19,7 @@ class CalendarHuangLi : public QObject
public:
explicit CalendarHuangLi(QObject *parent = nullptr);
- QString getFestivalMonth(quint32 year, quint32 month);
+ QJsonArray getFestivalMonth(quint32 year, quint32 month);
QString getHuangLiDay(quint32 year, quint32 month, quint32 day);
QString getHuangLiMonth(quint32 year, quint32 month, bool fill);
CaLunarDayInfo getLunarInfoBySolar(quint32 year, quint32 month, quint32 day);
diff --git a/calendar-service/src/dbmanager/dhuanglidatabase.cpp b/calendar-service/src/dbmanager/dhuanglidatabase.cpp
index b4aa35e2..ff6ff0a9 100644
--- a/calendar-service/src/dbmanager/dhuanglidatabase.cpp
+++ b/calendar-service/src/dbmanager/dhuanglidatabase.cpp
@@ -8,6 +8,10 @@
#include
#include
+#include
+#include
+#include
+#include
const QString HolidayDir = ":/holiday-cn";
@@ -23,37 +27,49 @@ DHuangLiDataBase::DHuangLiDataBase(QObject *parent)
dbOpen();
}
-QString DHuangLiDataBase::queryFestivalList(quint32 year, quint8 month)
+// readJSON 会读取一个JSON文件,如果 cache 为 true,则会缓存对象,以供下次使用
+QJsonDocument DHuangLiDataBase::readJSON(QString filename, bool cache)
+{
+ if (cache && readJSONCache.contains(filename)) {
+ return readJSONCache.value(filename);
+ }
+ qCDebug(ServiceLogger) << "read json file" << filename;
+ QJsonDocument doc;
+ QFile file(filename);
+ if (!file.open(QIODevice::ReadOnly)) {
+ qCWarning(ServiceLogger) << "cannot open json file" << filename;
+ } else {
+ auto data = file.readAll();
+ doc = QJsonDocument::fromJson(data);
+ }
+ readJSONCache.insert(filename, doc);
+ return readJSONCache.value(filename);
+}
+
+// queryFestivalList 查询指定月份的节假日列表
+QJsonArray DHuangLiDataBase::queryFestivalList(quint32 year, quint8 month)
{
qCDebug(ServiceLogger) << "query festival list"
<< "year" << year << "month" << month;
QJsonArray dataset;
- QFile file(QString("%1/%2.json").arg(HolidayDir).arg(year));
- qCDebug(ServiceLogger) << "festival file name" << file.fileName();
- if (file.open(QIODevice::ReadOnly)) {
- auto data = file.readAll();
- file.close();
- auto doc = QJsonDocument::fromJson(data);
- for (auto val : doc.object().value("days").toArray()) {
- auto day = val.toObject();
- auto name = day.value("name").toString();
- auto date = QDate::fromString(day.value("date").toString(), "yyyy-MM-dd");
- auto isOffday = day.value("isOffDay").toBool();
- if (quint32(date.year()) == year && quint32(date.month()) == month) {
- qCDebug(ServiceLogger) << "festival day" << name << date << isOffday;
- QJsonObject obj;
- obj.insert("name", name);
- obj.insert("date", date.toString("yyyy-MM-dd"));
- obj.insert("status", isOffday ? 1 : 2);
- dataset.append(obj);
- }
+ auto filename = QString("%1/%2.json").arg(HolidayDir).arg(year);
+ qCDebug(ServiceLogger) << "festival file name" << filename;
+ auto doc = readJSON(filename, true);
+ for (auto val : doc.object().value("days").toArray()) {
+ auto day = val.toObject();
+ auto name = day.value("name").toString();
+ auto date = QDate::fromString(day.value("date").toString(), "yyyy-MM-dd");
+ auto isOffday = day.value("isOffDay").toBool();
+ if (quint32(date.year()) == year && quint32(date.month()) == month) {
+ qCDebug(ServiceLogger) << "festival day" << name << date << isOffday;
+ QJsonObject obj;
+ obj.insert("name", name);
+ obj.insert("date", date.toString("yyyy-MM-dd"));
+ obj.insert("status", isOffday ? 1 : 2);
+ dataset.append(obj);
}
- file.close();
}
- QJsonDocument result;
- QJsonObject obj;
- result.setArray(dataset);
- return QString(result.toJson(QJsonDocument::Compact));
+ return dataset;
}
QList DHuangLiDataBase::queryHuangLiByDays(const QList &days)
diff --git a/calendar-service/src/dbmanager/dhuanglidatabase.h b/calendar-service/src/dbmanager/dhuanglidatabase.h
index 935b3ee6..fee7de4f 100644
--- a/calendar-service/src/dbmanager/dhuanglidatabase.h
+++ b/calendar-service/src/dbmanager/dhuanglidatabase.h
@@ -7,19 +7,22 @@
#include "ddatabase.h"
#include "huangliData/lunardatastruct.h"
-#include "huangliData/dbusdatastruct.h"
#include "lunarandfestival.h"
+#include
+#include
class DHuangLiDataBase : public DDataBase
{
Q_OBJECT
public:
explicit DHuangLiDataBase(QObject *parent = nullptr);
- QString queryFestivalList(quint32 year, quint8 month);
+ QJsonArray queryFestivalList(quint32 year, quint8 month);
QList queryHuangLiByDays(const QList &days);
void initDBData() override;
-
+private:
+ QJsonDocument readJSON(QString filename, bool cache);
+ QHash readJSONCache;
protected:
//创建数据库
void createDB() override;
diff --git a/calendar-service/src/dbusservice/dhuangliservice.cpp b/calendar-service/src/dbusservice/dhuangliservice.cpp
index 749e2833..c3235449 100644
--- a/calendar-service/src/dbusservice/dhuangliservice.cpp
+++ b/calendar-service/src/dbusservice/dhuangliservice.cpp
@@ -3,9 +3,9 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "dhuangliservice.h"
-#include "units.h"
#include "calendarprogramexitcontrol.h"
+#include "units.h"
DHuangliService::DHuangliService(QObject *parent)
: DServiceBase(serviceBasePath + "/HuangLi", serviceBaseName + ".HuangLi", parent)
@@ -17,26 +17,25 @@ DHuangliService::DHuangliService(QObject *parent)
CaHuangLiMonthInfo::registerMetaType();
}
-//获取指定公历月的假日信息
+// 获取指定公历月的假日信息
QString DHuangliService::getFestivalMonth(quint32 year, quint32 month)
{
DServiceExitControl exitControl;
- QString festivalInfo = m_huangli->getFestivalMonth(year, month);
- return festivalInfo;
+ auto arr = m_huangli->getFestivalMonth(year, month);
+ QJsonDocument result;
+ result.setArray(arr);
+ return result.toJson(QJsonDocument::Compact);
}
-//获取指定公历日的黄历信息
+// 获取指定公历日的黄历信息
QString DHuangliService::getHuangLiDay(quint32 year, quint32 month, quint32 day)
{
-
- if( 0 >= year || 0 >= month || 0 >= day)
- return "";
DServiceExitControl exitControl;
QString huangliInfo = m_huangli->getHuangLiDay(year, month, day);
return huangliInfo;
}
-//获取指定公历月的黄历信息
+// 获取指定公历月的黄历信息
QString DHuangliService::getHuangLiMonth(quint32 year, quint32 month, bool fill)
{
DServiceExitControl exitControl;
@@ -44,7 +43,7 @@ QString DHuangliService::getHuangLiMonth(quint32 year, quint32 month, bool fill)
return huangliInfo;
}
-//通过公历获取阴历信息
+// 通过公历获取阴历信息
CaLunarDayInfo DHuangliService::getLunarInfoBySolar(quint32 year, quint32 month, quint32 day)
{
DServiceExitControl exitControl;
@@ -52,7 +51,7 @@ CaLunarDayInfo DHuangliService::getLunarInfoBySolar(quint32 year, quint32 month,
return huangliInfo;
}
-//获取阴历月信息
+// 获取阴历月信息
CaLunarMonthInfo DHuangliService::getLunarMonthCalendar(quint32 year, quint32 month, bool fill)
{
DServiceExitControl exitControl;