Files
next-tjwater-drainage-frontend/features/workbench/utils/feature-properties.ts
T

112 lines
2.6 KiB
TypeScript

import { formatValue } from "./format-value";
export type LocalizedFeatureProperty = {
key: string;
label: string;
value: string;
};
const PROPERTY_LABELS: Record<string, string> = {
id: "编号",
gid: "要素编号",
name: "名称",
code: "编码",
type: "类型",
node1: "起点节点",
node2: "终点节点",
from_node: "起点节点",
to_node: "终点节点",
diameter: "管径",
dn: "管径",
length: "长度",
roughness: "粗糙系数",
status: "状态",
material: "材质",
elevation: "高程",
demand: "需水量",
pressure: "压力",
flow: "流量",
velocity: "流速",
zone: "分区",
district: "片区",
source: "来源",
updated_at: "更新时间"
};
const PROPERTY_ORDER: Record<string, number> = {
id: 10,
gid: 11,
name: 12,
code: 13,
diameter: 20,
dn: 20,
length: 21,
roughness: 22,
material: 23,
node1: 24,
from_node: 24,
node2: 25,
to_node: 25,
elevation: 30,
demand: 31,
pressure: 32,
flow: 33,
velocity: 34,
status: 90
};
const STATUS_LABELS: Record<string, string> = {
active: "运行中",
inactive: "停用",
online: "在线",
offline: "离线",
open: "开启",
closed: "关闭",
normal: "正常",
warning: "告警",
critical: "严重"
};
export function getLocalizedFeatureProperties(properties: Record<string, unknown>): LocalizedFeatureProperty[] {
return Object.entries(properties).map(([key, value]) => ({
key,
label: getFeaturePropertyLabel(key),
value: formatFeaturePropertyValue(key, value)
})).sort((first, second) => getPropertyOrder(first.key) - getPropertyOrder(second.key));
}
export function getFeaturePropertyLabel(key: string) {
const normalizedKey = normalizePropertyKey(key);
return PROPERTY_LABELS[normalizedKey] ?? key;
}
export function formatFeaturePropertyValue(key: string, value: unknown) {
const normalizedKey = normalizePropertyKey(key);
if (isStatusKey(key) && typeof value === "string") {
return STATUS_LABELS[value.toLowerCase()] ?? value;
}
if ((normalizedKey === "diameter" || normalizedKey === "dn") && value !== null && value !== undefined && value !== "") {
return `DN${formatValue(value)}`;
}
if ((normalizedKey === "length" || normalizedKey === "elevation") && value !== null && value !== undefined && value !== "") {
return `${formatValue(value)} m`;
}
return formatValue(value);
}
function getPropertyOrder(key: string) {
return PROPERTY_ORDER[normalizePropertyKey(key)] ?? 100;
}
function normalizePropertyKey(key: string) {
return key.trim().replaceAll("-", "_").toLowerCase();
}
function isStatusKey(key: string) {
return normalizePropertyKey(key) === "status";
}