Files
suixinkan_uikit/suixinkan/Features/DroneDock/Models/DroneDockPreparationModels.swift

248 lines
12 KiB
Swift

import Foundation
/// 本次定位得到的准备页坐标;未知位置使用 nil,不以零坐标替代。
struct DroneDockPreparationCoordinate: Equatable, Sendable {
/// 定位服务返回的纬度。
let latitude: Double
/// 定位服务返回的经度。
let longitude: Double
/// 保存一次实际定位结果,发送请求前再校验有效性。
init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
/// 仅接受有限且处于地理范围内的坐标,拒绝未定位时常见的零点占位。
var isValid: Bool {
latitude.isFinite && longitude.isFinite
&& (-90...90).contains(latitude) && (-180...180).contains(longitude)
&& (latitude != 0 || longitude != 0)
}
}
/// 朱工 workbench 的真实准备快照,仅描述当前订单、点位航线与服务端许可。
struct DroneDockPreparationResponse: Decodable, Equatable, Sendable {
/// 响应所属景区。
let scenicID: Int
/// 响应所属订单号。
let orderNumber: String
/// 服务端最新订单进度;缺失时不沿用旧额度。
let orderProgress: DroneDockPreparationOrderProgress?
/// 真实机巢摘要,不将它等同于已派机结果。
let airports: [DroneDockHomeAirport]
/// 该订单的拍摄点位,保持服务端顺序。
let spots: [DroneDockPreparationSpot]
/// 后端召唤许可快照,缺失时保持未知。
let shootGate: DroneDockPreparationShootGate?
/// 后端规则提示原文。
let tips: String?
/// workbench 已验证的顶层字段。
private enum CodingKeys: String, CodingKey {
case scenicID = "scenic_id", orderNumber = "order_number", orderProgress = "order_progress"
case airports, spots, shootGate = "shoot_gate", tips
}
/// 严格读取订单范围和列表身份,避免坏响应伪装成成功空态或破坏 Diffable 更新。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
scenicID = try container.positivePreparationID(forKey: .scenicID)
orderNumber = try container.decode(String.self, forKey: .orderNumber)
guard !orderNumber.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
throw DecodingError.dataCorruptedError(forKey: .orderNumber, in: container, debugDescription: "订单号不能为空")
}
orderProgress = try container.decodeIfPresent(DroneDockPreparationOrderProgress.self, forKey: .orderProgress)
if let progressOrder = orderProgress?.orderNumber, progressOrder != orderNumber {
throw DecodingError.dataCorruptedError(forKey: .orderProgress, in: container, debugDescription: "订单进度与当前订单不一致")
}
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 重复")
}
spots = try container.decode([DroneDockPreparationSpot].self, forKey: .spots)
guard Set(spots.map(\.id)).count == spots.count else {
throw DecodingError.dataCorruptedError(forKey: .spots, in: container, debugDescription: "拍摄点 ID 重复")
}
shootGate = try container.decodeIfPresent(DroneDockPreparationShootGate.self, forKey: .shootGate)
tips = try container.decodeIfPresent(String.self, forKey: .tips)
}
}
/// 准备页服务端订单进度,不在客户端推算剩余次数或扣减额度。
struct DroneDockPreparationOrderProgress: Decodable, Equatable, Sendable {
/// 进度所属订单号,存在时需与顶层一致。
let orderNumber: String?
/// 服务端套餐说明。
let packageName: String?
/// 服务端拍摄总次数。
let shotTotal: Int?
/// 服务端已拍次数,保留合法零值。
let shotUsed: Int?
/// 服务端剩余次数,缺失时保持未知。
let shotRemain: Int?
/// 服务端订单状态文案。
let statusLabel: String?
/// 订单进度字段名。
private enum CodingKeys: String, CodingKey {
case orderNumber = "order_number", packageName = "package_name", shotTotal = "shot_total"
case shotUsed = "shot_used", shotRemain = "shot_remain", statusLabel = "status_label"
}
/// 兼容整数及整数字符串,同时区分未知次数与零次。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
orderNumber = try container.decodeIfPresent(String.self, forKey: .orderNumber)
packageName = try container.decodeIfPresent(String.self, forKey: .packageName)
shotTotal = try container.preparationCountIfPresent(forKey: .shotTotal)
shotUsed = try container.preparationCountIfPresent(forKey: .shotUsed)
shotRemain = try container.preparationCountIfPresent(forKey: .shotRemain)
statusLabel = try container.decodeIfPresent(String.self, forKey: .statusLabel)
}
}
/// 拍摄点的服务端距离、已拍状态与可选航线;距离是否可用由请求是否带定位决定。
struct DroneDockPreparationSpot: Decodable, Equatable, Sendable {
/// 拍摄点稳定 ID。
let id: Int
/// 拍摄点名称。
let name: String
/// 服务端格式化距离,不自行换算距离文案。
let distanceLabel: String?
/// 服务端距离米数。
let distanceM: Double?
/// 服务端范围判断,缺失时不默认在范围内。
let inRange: Bool?
/// 本点位已拍次数。
let shotCount: Int?
/// 本点位已拍状态文案。
let shotStatusLabel: String?
/// 点位所属航线,按后端顺序展示。
let airLines: [DroneDockPreparationAirLine]
/// 已验证的点位字段名。
private enum CodingKeys: String, CodingKey {
case id = "scenic_spot_id", name, distanceLabel = "distance_label", distanceM = "distance_m"
case inRange = "in_range", shotCount = "shot_count", shotStatusLabel = "shot_status_label", airLines = "air_lines"
}
/// 点位和航线身份必须有效且点位内唯一,缺失距离与次数保留未知。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.positivePreparationID(forKey: .id)
name = try container.decode(String.self, forKey: .name)
distanceLabel = try container.decodeIfPresent(String.self, forKey: .distanceLabel)
distanceM = try container.decodeIfPresent(DroneDockPreparationNumber.self, forKey: .distanceM)?.value
if let distanceM, distanceM < 0 {
throw DecodingError.dataCorruptedError(forKey: .distanceM, in: container, debugDescription: "距离不能为负数")
}
inRange = try container.decodeIfPresent(Bool.self, forKey: .inRange)
shotCount = try container.preparationCountIfPresent(forKey: .shotCount)
shotStatusLabel = try container.decodeIfPresent(String.self, forKey: .shotStatusLabel)
airLines = try container.decode([DroneDockPreparationAirLine].self, forKey: .airLines)
guard Set(airLines.map(\.id)).count == airLines.count else {
throw DecodingError.dataCorruptedError(forKey: .airLines, in: container, debugDescription: "点位内航线 ID 重复")
}
}
}
/// 准备页返回的航线信息;不从飞行类型或时长推断设备执行许可。
struct DroneDockPreparationAirLine: Decodable, Equatable, Sendable {
/// 航线稳定 ID。
let id: Int
/// 航线名称。
let name: String
/// 服务端预计飞行时长原值。
let estimatedFlyTime: Int?
/// 服务端航线视频时长原值。
let lineVideoDuration: Int?
/// 服务端飞行类型原值。
let flyType: Int?
/// 已验证的航线字段名。
private enum CodingKeys: String, CodingKey {
case id = "air_line_id", name, estimatedFlyTime = "estimated_fly_time"
case lineVideoDuration = "line_video_duration", flyType = "fly_type"
}
/// 航线 ID 必须有效,时长未知时不显示伪造零值。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.positivePreparationID(forKey: .id)
name = try container.decode(String.self, forKey: .name)
estimatedFlyTime = try container.preparationCountIfPresent(forKey: .estimatedFlyTime)
lineVideoDuration = try container.preparationCountIfPresent(forKey: .lineVideoDuration)
flyType = try container.decodeIfPresent(DroneDockPreparationInteger.self, forKey: .flyType)?.value
}
}
/// 后端召唤门禁,仅提供当前许可与原因,不等于客户端已实现或执行召唤。
struct DroneDockPreparationShootGate: Decodable, Equatable, Sendable {
/// 后端明确允许召唤时为 true,缺失仍为未知。
let canShoot: Bool?
/// 后端门禁代码,保留未知新值。
let code: String?
/// 后端原因说明。
let message: String?
/// 后端主操作标识。
let primaryAction: String?
/// 后端主操作文案。
let primaryActionLabel: String?
/// 门禁字段名。
private enum CodingKeys: String, CodingKey {
case canShoot = "can_shoot", code, message
case primaryAction = "primary_action", primaryActionLabel = "primary_action_label"
}
}
/// 本接口整数与整数字符串的兼容读取器,不接受布尔、分数或部分数字。
private struct DroneDockPreparationInteger: 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 DroneDockPreparationNumber: Decodable {
let value: Double
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let number: Double
if let decoded = try? container.decode(Double.self) { number = decoded }
else if let text = try? container.decode(String.self), let decoded = Double(text) { number = decoded }
else { throw DecodingError.dataCorruptedError(in: container, debugDescription: "需要数字或数字字符串") }
guard number.isFinite else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "数字必须为有限值")
}
value = number
}
}
private extension KeyedDecodingContainer {
/// 读取会作为列表身份的正整数。
func positivePreparationID(forKey key: Key) throws -> Int {
let value = try decode(DroneDockPreparationInteger.self, forKey: key).value
guard value > 0 else {
throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "ID 必须为正整数")
}
return value
}
/// 读取可缺失的非负次数或时长。
func preparationCountIfPresent(forKey key: Key) throws -> Int? {
guard let value = try decodeIfPresent(DroneDockPreparationInteger.self, forKey: key)?.value else { return nil }
guard value >= 0 else {
throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "次数或时长不能为负数")
}
return value
}
}