158 lines
5.5 KiB
C++
158 lines
5.5 KiB
C++
#include "dto/NavDto.hpp"
|
||
|
||
#include <QJsonValue>
|
||
|
||
#include <functional>
|
||
#include <set>
|
||
|
||
namespace geopro::data::dto {
|
||
|
||
namespace {
|
||
std::string str(const QJsonObject& o, const char* key) {
|
||
return o.value(QString::fromLatin1(key)).toString().toStdString();
|
||
}
|
||
|
||
ProjectSummary parseProjectItem(const QJsonObject& o) {
|
||
ProjectSummary p;
|
||
p.id = str(o, "id");
|
||
p.name = str(o, "projectName");
|
||
p.typeName = str(o, "projectTypeName");
|
||
p.crsCode = str(o, "referenceCRSCode");
|
||
p.crsName = str(o, "referenceCRSName");
|
||
p.code = str(o, "projectCode");
|
||
p.projectTypeId = str(o, "projectTypeId");
|
||
p.ownerCompany = str(o, "ownerCompanyName");
|
||
p.responsiblePerson = str(o, "responsiblePersonName");
|
||
p.createTime = str(o, "createTime");
|
||
p.status = o.value(QStringLiteral("status")).toInt();
|
||
return p;
|
||
}
|
||
} // namespace
|
||
|
||
std::vector<Workspace> parseWorkspaces(const QJsonArray& arr) {
|
||
std::vector<Workspace> out;
|
||
out.reserve(static_cast<size_t>(arr.size()));
|
||
for (const QJsonValue& v : arr) {
|
||
const QJsonObject o = v.toObject();
|
||
Workspace w;
|
||
w.id = str(o, "id");
|
||
w.name = str(o, "name");
|
||
w.ownerType = o.value(QStringLiteral("ownerType")).toInt();
|
||
w.isCurrent = o.value(QStringLiteral("isCurTenant")).toInt() == 1;
|
||
out.push_back(std::move(w));
|
||
}
|
||
return out;
|
||
}
|
||
|
||
ProjectPage parseProjects(const QJsonObject& data) {
|
||
ProjectPage page;
|
||
page.hasNextPage = data.value(QStringLiteral("hasNextPage")).toBool();
|
||
const QJsonArray list = data.value(QStringLiteral("projectList")).toArray();
|
||
page.projects.reserve(static_cast<size_t>(list.size()));
|
||
for (const QJsonValue& v : list) page.projects.push_back(parseProjectItem(v.toObject()));
|
||
return page;
|
||
}
|
||
|
||
std::vector<ProjectSummary> parseProjectList(const QJsonArray& arr) {
|
||
std::vector<ProjectSummary> out;
|
||
out.reserve(static_cast<size_t>(arr.size()));
|
||
for (const QJsonValue& v : arr) out.push_back(parseProjectItem(v.toObject()));
|
||
return out;
|
||
}
|
||
|
||
ProjectListPage parseProjectPage(const QJsonObject& data) {
|
||
ProjectListPage p;
|
||
p.rows = parseProjectList(data.value(QStringLiteral("list")).toArray());
|
||
p.total = data.value(QStringLiteral("total")).toInt();
|
||
return p;
|
||
}
|
||
|
||
std::vector<ProjectType> parseProjectTypes(const QJsonArray& arr) {
|
||
std::vector<ProjectType> out;
|
||
out.reserve(static_cast<size_t>(arr.size()));
|
||
for (const QJsonValue& v : arr) {
|
||
const QJsonObject o = v.toObject();
|
||
out.push_back(ProjectType{str(o, "id"), str(o, "name")});
|
||
}
|
||
return out;
|
||
}
|
||
|
||
std::vector<StructNode> parseStructNodes(const QJsonArray& arr) {
|
||
std::vector<StructNode> out;
|
||
out.reserve(static_cast<size_t>(arr.size()));
|
||
for (const QJsonValue& v : arr) {
|
||
const QJsonObject o = v.toObject();
|
||
StructNode n;
|
||
n.id = str(o, "id");
|
||
n.name = str(o, "name");
|
||
n.parentId = str(o, "parentId");
|
||
n.typeName = str(o, "typeName");
|
||
n.confCode = str(o, "confCode");
|
||
n.type = o.value(QStringLiteral("type")).toInt();
|
||
out.push_back(std::move(n));
|
||
}
|
||
return out;
|
||
}
|
||
|
||
std::vector<DsRow> parseDsRows(const QJsonArray& arr) {
|
||
std::vector<DsRow> out;
|
||
out.reserve(static_cast<size_t>(arr.size()));
|
||
for (const QJsonValue& v : arr) {
|
||
const QJsonObject o = v.toObject();
|
||
DsRow d;
|
||
d.id = str(o, "id");
|
||
d.dsName = str(o, "dsName");
|
||
d.typeName = str(o, "name"); // 注意:name 字段=ds类型名
|
||
d.ddCode = str(o, "ddCode");
|
||
d.createTime = str(o, "createTime");
|
||
const QJsonObject f = o.value(QStringLiteral("file")).toObject();
|
||
d.fileName = str(f, "name");
|
||
d.fileUrl = str(f, "url");
|
||
d.fileSize = static_cast<long long>(f.value(QStringLiteral("size")).toDouble());
|
||
out.push_back(std::move(d));
|
||
}
|
||
return out;
|
||
}
|
||
|
||
DsPage parseDsPage(const QJsonObject& data) {
|
||
DsPage p;
|
||
p.rows = parseDsRows(data.value(QStringLiteral("list")).toArray());
|
||
p.total = data.value(QStringLiteral("total")).toInt();
|
||
return p;
|
||
}
|
||
|
||
std::vector<StructTreeNode> buildStructTree(const std::vector<StructNode>& flat) {
|
||
// 过滤 DS(type==3):DS 不进对象树(按 TM 单独拉取到数据列表)。
|
||
std::vector<StructNode> nodes;
|
||
nodes.reserve(flat.size());
|
||
for (const auto& n : flat)
|
||
if (n.type != 3) nodes.push_back(n);
|
||
|
||
std::set<std::string> ids;
|
||
for (const auto& n : nodes) ids.insert(n.id);
|
||
// 根层:parentId 为空 / "0" / 不在集合内(孤儿)。
|
||
auto isRootLevel = [&](const StructNode& n) {
|
||
return n.parentId.empty() || n.parentId == "0" || ids.find(n.parentId) == ids.end();
|
||
};
|
||
std::set<std::string> visited; // 防环:每个 id 最多进树一次。
|
||
std::function<std::vector<StructTreeNode>(const std::string&, bool)> build =
|
||
[&](const std::string& parentId, bool root) {
|
||
std::vector<StructTreeNode> out;
|
||
for (const auto& n : nodes) {
|
||
const bool belongs = root ? isRootLevel(n) : (n.parentId == parentId);
|
||
if (!belongs) continue;
|
||
if (visited.count(n.id)) continue;
|
||
visited.insert(n.id);
|
||
StructTreeNode t;
|
||
t.node = n;
|
||
t.isTm = (n.type == 2); // type: 1=项目根 2=TM(测线) 3=DS(已过滤)
|
||
t.children = build(n.id, false);
|
||
out.push_back(std::move(t));
|
||
}
|
||
return out;
|
||
};
|
||
return build(std::string(), true);
|
||
}
|
||
|
||
} // namespace geopro::data::dto
|