Files
suixinkan_uikit/suixinkan/Features/DroneDock/Models/DroneDockHomeModels.swift
T

344 lines
17 KiB
Swift

import Foundation
/// 朱工首页接口的已验证结构;数组缺失或类型异常视为解析错误,不伪装成空列表。
struct DroneDockHomeResponse: Decodable, Equatable, Sendable {
/// 服务端返回的数据所属景区。
let scenicID: Int
/// 当前景区的机巢设备,按真实非空响应的字段读取。
let airports: [DroneDockHomeAirport]
/// 按真实响应字段读取的拍摄订单,保持后端返回顺序。
let orders: [DroneDockHomeOrder]
/// 今日数据缺失时保留未知,不生成零值统计。
let today: DroneDockHomeToday?
/// 已验证的首页顶层字段名。
private enum CodingKeys: String, CodingKey {
case scenicID = "scenic_id"
case airports, orders, today
}
/// 严格读取首页骨架,兼容服务端使用整数或整数字符串表示景区 ID。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
scenicID = try container.decode(DroneDockHomeInteger.self, forKey: .scenicID).value
guard scenicID > 0 else {
throw DecodingError.dataCorruptedError(forKey: .scenicID, in: container, debugDescription: "景区 ID 必须为正整数")
}
airports = try container.decode([DroneDockHomeAirport].self, forKey: .airports)
guard Set(airports.map(\.id)).count == airports.count else {
throw DecodingError.dataCorruptedError(forKey: .airports, in: container, debugDescription: "设备 ID 重复,无法确定设备身份")
}
orders = try container.decode([DroneDockHomeOrder].self, forKey: .orders)
guard Set(orders.map(\.orderID)).count == orders.count else {
throw DecodingError.dataCorruptedError(forKey: .orders, in: container, debugDescription: "订单 ID 重复,无法确定订单身份")
}
today = try container.decodeIfPresent(DroneDockHomeToday.self, forKey: .today)
}
}
/// 首页设备卡的数据,只表达后端展示信息,不推导起飞或拍摄许可。
struct DroneDockHomeAirport: Decodable, Equatable, Sendable {
/// 后端设备稳定 ID,用于 Diffable 项身份。
let id: Int
/// 后端设备名称。
let name: String
/// 后端设备序列号,不将它与尚未确认的飞控参数混用。
let snNumber: String?
/// 后端设备所属景区 ID。
let scenicAreaID: Int?
/// 原始在线状态;尚未取得完整枚举约定。
let onlineStatus: Int?
/// 原始工作状态;不据此决定设备可否执行任务。
let workStatus: Int?
/// 后端工作状态展示文案。
let workStatusLabel: String?
/// 原始接单配置;不等同于本次起飞许可。
let isReceiveOrder: Int?
/// 任务类型原始值;当前样本为 null,暂不假定其枚举或值类型。
let taskType: DroneDockHomeJSONValue?
/// 后端读数列表,展示时按稳定 key 匹配,不依赖返回顺序。
let metrics: [DroneDockHomeMetric]
/// 已实测的设备字段名。
private enum CodingKeys: String, CodingKey {
case id, name, metrics
case snNumber = "sn_number"
case scenicAreaID = "scenic_area_id"
case onlineStatus = "online_status"
case workStatus = "work_status"
case workStatusLabel = "work_status_label"
case isReceiveOrder = "is_receive_order"
case taskType = "task_type"
}
/// 读取实际设备结构;身份缺失时报错,缺失遥测保持未知。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(DroneDockHomeInteger.self, forKey: .id).value
guard id > 0 else {
throw DecodingError.dataCorruptedError(forKey: .id, in: container, debugDescription: "设备 ID 必须为正整数")
}
name = try container.decode(String.self, forKey: .name)
snNumber = try container.decodeIfPresent(String.self, forKey: .snNumber)
scenicAreaID = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .scenicAreaID)?.value
onlineStatus = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .onlineStatus)?.value
workStatus = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .workStatus)?.value
workStatusLabel = try container.decodeIfPresent(String.self, forKey: .workStatusLabel)
isReceiveOrder = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .isReceiveOrder)?.value
taskType = try container.decodeIfPresent(DroneDockHomeJSONValue.self, forKey: .taskType)
metrics = try container.decodeIfPresent([DroneDockHomeMetric].self, forKey: .metrics) ?? []
}
/// 状态标签使用后端文案;缺失、空白或占位文案不显示成空闲。
var statusTitle: String { DroneDockHomeMetric.usableText(workStatusLabel) ?? "状态待确认" }
/// 只映射已验证的空闲状态样本,其余保持中性色,不新增业务状态推断。
var statusTone: DroneDockStatusTone { workStatus == 1 && statusTitle == "空闲" ? .green : .gray }
/// 返回指定读数的后端展示值,保留单位;未知值不从 raw 猜测生成。
func metricText(for key: String) -> String {
metrics.first { $0.key == key }?.displayText ?? "—"
}
/// 仅在电量原始值为 0~100 的真实数字时绘制环形进度,零电量与未知分开。
var batteryProgress: Double? {
guard let metric = metrics.first(where: { $0.key == "battery" }),
case let .number(value)? = metric.raw, value >= 0, value <= 100 else { return nil }
return NSDecimalNumber(decimal: value).doubleValue / 100
}
}
/// 首页单项设备读数,区分可展示的后端文案和未经解释的原始值。
struct DroneDockHomeMetric: Decodable, Equatable, Sendable {
/// 后端读数稳定键,如 battery、env_temp。
let key: String
/// 后端读数名称。
let label: String?
/// 后端原始读数;除合法数值电量环外不据此推测单位或设备许可。
let raw: DroneDockHomeJSONValue?
/// 后端格式化展示值。
let value: String?
/// 统一未知占位,但保留有效零值及服务端单位。
var displayText: String { Self.usableText(value) ?? "—" }
/// 将空白和仅由破折号组成的占位文案判为未知。
static func usableText(_ value: String?) -> String? {
guard let value else { return nil }
let text = value.trimmingCharacters(in: .whitespacesAndNewlines)
guard !text.isEmpty, !text.allSatisfy({ "-–—−― ".contains($0) }) else { return nil }
return text
}
}
/// 首页今日统计;保留服务端字段语义,未知项不显示为零。
struct DroneDockHomeToday: Decodable, Equatable, Sendable {
/// 服务端的今日订单数量。
let orderCount: Int?
/// 服务端的今日订单金额,使用十进制金额避免二进制浮点误差。
let orderAmount: Decimal?
/// 服务端 finished_shoot 数值,其业务统计口径仍需与后端核对。
let finishedShoot: Int?
/// 已验证的统计字段名。
private enum CodingKeys: String, CodingKey {
case orderCount = "order_count"
case orderAmount = "order_amount"
case finishedShoot = "finished_shoot"
}
/// 兼容数字和数字字符串;缺失或 null 保持 nil,非法文本拒绝解析。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
orderCount = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .orderCount)?.value
orderAmount = try container.decodeIfPresent(DroneDockHomeDecimal.self, forKey: .orderAmount)?.value
finishedShoot = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .finishedShoot)?.value
}
}
/// 首页拍摄订单的展示数据;状态及动作仅用于呈现,不代表飞行执行许可。
struct DroneDockHomeOrder: Decodable, Equatable, Sendable {
/// 后端订单稳定 ID,用于 Diffable 项身份。
let orderID: Int
/// 后端订单编号,保留字符串形式与前导零。
let orderNumber: String?
/// 后端游客展示名称。
let touristLabel: String?
/// 后端已脱敏的游客手机号。
let touristPhoneMask: String?
/// 后端套餐名称,不据此推断套餐类型或项目 ID。
let packageName: String?
/// 后端订单状态展示文案。
let statusLabel: String?
/// 后端拍摄总次数,未知时不默认成零。
let shotTotal: Int?
/// 后端已拍摄次数,不在客户端根据任务状态重新计算。
let shotUsed: Int?
/// 后端剩余拍摄次数,不用总次数减已拍摄次数推断。
let shotRemain: Int?
/// 后端格式化的拍摄进度文案。
let progressText: String?
/// 后端素材状态原始值,保留尚未验证的值。
let mediaStatus: String?
/// 后端素材状态展示文案;空文案不生成新的业务状态。
let mediaStatusLabel: String?
/// 后端主动作标识,仅用于展示入口,不作为执行权限。
let primaryAction: String?
/// 后端主动作按钮文案。
let primaryActionLabel: String?
/// 后端游客用户 ID。
let userID: Int?
/// 已实测的首页订单字段名。
private enum CodingKeys: String, CodingKey {
case orderID = "order_id"
case orderNumber = "order_number"
case touristLabel = "tourist_label"
case touristPhoneMask = "tourist_phone_mask"
case packageName = "package_name"
case statusLabel = "status_label"
case shotTotal = "shot_total"
case shotUsed = "shot_used"
case shotRemain = "shot_remain"
case progressText = "progress_text"
case mediaStatus = "media_status"
case mediaStatusLabel = "media_status_label"
case primaryAction = "primary_action"
case primaryActionLabel = "primary_action_label"
case userID = "user_id"
}
/// 严格校验列表身份和计数;缺失展示字段保留未知。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
orderID = try container.decode(DroneDockHomeInteger.self, forKey: .orderID).value
guard orderID > 0 else {
throw DecodingError.dataCorruptedError(forKey: .orderID, in: container, debugDescription: "订单 ID 必须为正整数")
}
orderNumber = try container.decodeIfPresent(String.self, forKey: .orderNumber)
touristLabel = try container.decodeIfPresent(String.self, forKey: .touristLabel)
touristPhoneMask = try container.decodeIfPresent(String.self, forKey: .touristPhoneMask)
packageName = try container.decodeIfPresent(String.self, forKey: .packageName)
statusLabel = try container.decodeIfPresent(String.self, forKey: .statusLabel)
shotTotal = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .shotTotal)?.value
shotUsed = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .shotUsed)?.value
shotRemain = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .shotRemain)?.value
for (key, value) in [(CodingKeys.shotTotal, shotTotal), (.shotUsed, shotUsed), (.shotRemain, shotRemain)] {
if let value, value < 0 {
throw DecodingError.dataCorruptedError(forKey: key, in: container, debugDescription: "拍摄次数不能为负数")
}
}
progressText = try container.decodeIfPresent(String.self, forKey: .progressText)
mediaStatus = try container.decodeIfPresent(String.self, forKey: .mediaStatus)
mediaStatusLabel = try container.decodeIfPresent(String.self, forKey: .mediaStatusLabel)
primaryAction = try container.decodeIfPresent(String.self, forKey: .primaryAction)
primaryActionLabel = try container.decodeIfPresent(String.self, forKey: .primaryActionLabel)
userID = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .userID)?.value
}
/// 页面稳定身份,与后端订单 ID 一致。
var id: Int { orderID }
/// 展示后端订单编号;缺失时不以内部 ID 冒充订单编号。
var orderNumberText: String { DroneDockHomeMetric.usableText(orderNumber) ?? "—" }
/// 优先展示服务端游客文案,其次使用已脱敏手机号。
var touristTitle: String {
if let label = DroneDockHomeMetric.usableText(touristLabel) { return label }
if let phone = DroneDockHomeMetric.usableText(touristPhoneMask) { return "游客 \(phone)" }
return "游客信息待确认"
}
/// 展示后端套餐名称,缺失时不生成套餐或次数。
var packageTitle: String { DroneDockHomeMetric.usableText(packageName) ?? "套餐信息待确认" }
/// 展示后端状态,缺失时不默认成待拍摄。
var statusTitle: String { DroneDockHomeMetric.usableText(statusLabel) ?? "状态待确认" }
/// 优先展示后端进度文案;没有文案时逐项展示真实计数与未知占位。
var progressTitle: String {
if let text = DroneDockHomeMetric.usableText(progressText) { return text }
let used = shotUsed.map(String.init) ?? "—"
let total = shotTotal.map(String.init) ?? "—"
let remain = shotRemain.map(String.init) ?? "—"
return "已拍摄 \(used)/\(total) 次,剩余 \(remain) 次"
}
/// 只有后端同时提供动作标识和文案时才显示按钮,不猜测缺失动作。
var actionTitle: String? {
guard DroneDockHomeMetric.usableText(primaryAction) != nil else { return nil }
return DroneDockHomeMetric.usableText(primaryActionLabel)
}
/// 仅展示有效的后端素材状态文案,不将空值转成未上传等新状态。
var mediaStatusTitle: String? {
DroneDockHomeMetric.usableText(mediaStatusLabel)
}
}
/// 保留未知记录的 JSON 结构;数字采用 Decimal,不参与未确认业务计算。
indirect enum DroneDockHomeJSONValue: Decodable, Equatable, Sendable {
/// JSON 对象。
case object([String: DroneDockHomeJSONValue])
/// JSON 数组。
case array([DroneDockHomeJSONValue])
/// JSON 字符串。
case string(String)
/// JSON 数字。
case number(Decimal)
/// JSON 布尔值。
case bool(Bool)
/// JSON 空值。
case null
/// 按 JSON 实际类型解码,不把字符串自动转换为其他类型。
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if container.decodeNil() { self = .null }
else if let value = try? container.decode(Bool.self) { self = .bool(value) }
else if let value = try? container.decode(String.self) { self = .string(value) }
else if let value = try? container.decode(Decimal.self) { self = .number(value) }
else if let value = try? container.decode([String: DroneDockHomeJSONValue].self) { self = .object(value) }
else { self = .array(try container.decode([DroneDockHomeJSONValue].self)) }
}
}
/// 已知整数统计字段的严格兼容解码器。
private struct DroneDockHomeInteger: Decodable {
let value: Int
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let number = try? container.decode(Int.self) {
value = number
} else if let text = try? container.decode(String.self), let number = Int(text) {
value = number
} else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "需要整数或整数字符串")
}
}
}
/// 已知金额字段的十进制兼容解码器,拒绝只含部分合法数字的字符串。
private struct DroneDockHomeDecimal: Decodable {
let value: Decimal
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let text = try? container.decode(String.self) {
let normalized = text.trimmingCharacters(in: .whitespacesAndNewlines)
guard normalized.range(of: #"^-?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$"#, options: .regularExpression) != nil,
let number = Decimal(string: normalized, locale: Locale(identifier: "en_US_POSIX")),
!number.isNaN else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "金额必须为有效十进制数")
}
value = number
} else {
value = try container.decode(Decimal.self)
guard !value.isNaN else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "金额必须为有效十进制数")
}
}
}
}