geopro/src/data/dto/NavDto.cpp

123 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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.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;
}
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<DsNode> parseDatasets(const QJsonArray& arr) {
std::vector<DsNode> out;
out.reserve(static_cast<size_t>(arr.size()));
for (const QJsonValue& v : arr) {
const QJsonObject o = v.toObject();
DsNode d;
d.id = str(o, "id");
d.name = str(o, "name");
d.ddType = str(o, "ddCode");
out.push_back(std::move(d));
}
return out;
}
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