Skip to content

Commit

Permalink
feat: Optimize holiday query, change the return type to QJsonArray
Browse files Browse the repository at this point in the history
优化查询节假日返回类型为QJsonArray, 提高代码可读性,便于调用者处理

Log:
  • Loading branch information
myml committed Dec 28, 2023
1 parent 7872dd6 commit f1d7181
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 44 deletions.
180 changes: 180 additions & 0 deletions calendar-service/assets/holiday-cn/2023.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
1 change: 1 addition & 0 deletions calendar-service/assets/resources.qrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<RCC>
<qresource prefix="/">
<file>pinyin.dict</file>
<file>holiday-cn/2023.json</file>
<file>holiday-cn/2024.json</file>
</qresource>
</RCC>
8 changes: 2 additions & 6 deletions calendar-service/src/calendarhuangli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
#include "calendarhuangli.h"
#include "lunarandfestival/lunarmanager.h"

#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>

CalendarHuangLi::CalendarHuangLi(QObject *parent)
: QObject(parent)
, m_database(new DHuangLiDataBase(this))
{
}

//获取指定公历月的假日信息
QString CalendarHuangLi::getFestivalMonth(quint32 year, quint32 month)
QJsonArray CalendarHuangLi::getFestivalMonth(quint32 year, quint32 month)
{
return m_database->queryFestivalList(year, static_cast<quint8>(month));
return m_database->queryFestivalList(year, month);
}

QString CalendarHuangLi::getHuangLiDay(quint32 year, quint32 month, quint32 day)
Expand Down
4 changes: 3 additions & 1 deletion calendar-service/src/calendarhuangli.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define CALENDARHUANGLI_H

#include "dhuanglidatabase.h"
#include "huangliData/dbusdatastruct.h"
#include <qjsonarray.h>

#include <QObject>

Expand All @@ -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);
Expand Down
66 changes: 41 additions & 25 deletions calendar-service/src/dbmanager/dhuanglidatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#include <QDebug>
#include <QSqlError>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

const QString HolidayDir = ":/holiday-cn";

Expand All @@ -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<stHuangLi> DHuangLiDataBase::queryHuangLiByDays(const QList<stDay> &days)
Expand Down
9 changes: 6 additions & 3 deletions calendar-service/src/dbmanager/dhuanglidatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@

#include "ddatabase.h"
#include "huangliData/lunardatastruct.h"
#include "huangliData/dbusdatastruct.h"
#include "lunarandfestival.h"
#include <qjsonarray.h>
#include <qjsondocument.h>

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<stHuangLi> queryHuangLiByDays(const QList<stDay> &days);

void initDBData() override;

private:
QJsonDocument readJSON(QString filename, bool cache);
QHash<QString, QJsonDocument> readJSONCache;
protected:
//创建数据库
void createDB() override;
Expand Down
Loading

0 comments on commit f1d7181

Please sign in to comment.