import Foundation /// 张工 project-info 的订单购买快照,权威提供次数、可选点位与航线。 struct DroneDockProjectInfoResponse: Decodable, Equatable, Sendable { /// 根级订单摘要,与首页 orders 使用同一业务字段和权限口径。 let order: DroneDockHomeOrder /// 购买快照中的点位,保持服务端顺序,不拼入其他来源的额外点位。 let spots: [DroneDockPurchasedSpot] /// 购买点位数组使用单数 spot 字段。 private enum CodingKeys: String, CodingKey { case spots = "spot" } /// 要求项目身份和点位数组存在,空数组才表示真实空态。 init(from decoder: Decoder) throws { order = try DroneDockHomeOrder(from: decoder) guard order.projectID != nil else { throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "购买快照缺少项目 ID")) } let container = try decoder.container(keyedBy: CodingKeys.self) spots = try container.decode([DroneDockPurchasedSpot].self, forKey: .spots) guard Set(spots.map(\.id)).count == spots.count else { throw DecodingError.dataCorruptedError(forKey: .spots, in: container, debugDescription: "购买点位 ID 重复") } } } /// 订单购买范围内的拍摄点;是否可选由 available 明确返回,不由位置或次数推导。 struct DroneDockPurchasedSpot: Decodable, Equatable, Sendable { /// 拍摄点稳定 ID。 let id: Int /// 购买快照的点位名称。 let name: String /// 当前点位是否可用;缺失保留未知。 let available: Bool? /// 点位封面地址,可缺失或为空。 let coverURL: String? /// 购买范围内的航线,按服务端顺序展示。 let airLines: [DroneDockPurchasedAirLine] /// 张工购买点位字段,不接收 workbench 的 name 作为替代。 private enum CodingKeys: String, CodingKey { case id = "scenic_spot_id", name = "scenic_spot_name", available case coverURL = "cover_url", airLines = "air_lines" } /// 点位与本点位航线身份必须有效且唯一,空航线数组保留为空。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.positivePurchasedID(forKey: .id) name = try container.decode(String.self, forKey: .name) available = try container.decodeIfPresent(Bool.self, forKey: .available) coverURL = try container.decodeIfPresent(String.self, forKey: .coverURL) airLines = try container.decode([DroneDockPurchasedAirLine].self, forKey: .airLines) guard Set(airLines.map(\.id)).count == airLines.count else { throw DecodingError.dataCorruptedError(forKey: .airLines, in: container, debugDescription: "购买点位内航线 ID 重复") } } } /// 订单已购航线及样片配置;不可用原因直接用于页面说明。 struct DroneDockPurchasedAirLine: Decodable, Equatable, Sendable { /// 航线稳定 ID。 let id: Int /// 航线名称。 let name: String /// 当前航线是否可用,未知不自动视为可用。 let available: Bool? /// 后端说明当前不可用的原因。 let unavailableReason: String? /// 与该购买航线关联的样片,缺失或 null 表示没有可展示样片。 let samples: [DroneDockPurchasedSample] /// 张工购买航线字段。 private enum CodingKeys: String, CodingKey { case id = "air_line_id", name = "air_line_name", available case unavailableReason = "unavailable_reason", samples } /// 不替换未知可用状态或缺失样片地址,也不使用旧接口名称字段。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.positivePurchasedID(forKey: .id) name = try container.decode(String.self, forKey: .name) available = try container.decodeIfPresent(Bool.self, forKey: .available) unavailableReason = try container.decodeIfPresent(String.self, forKey: .unavailableReason) samples = try container.decodeIfPresent([DroneDockPurchasedSample].self, forKey: .samples) ?? [] guard Set(samples.map(\.id)).count == samples.count else { throw DecodingError.dataCorruptedError(forKey: .samples, in: container, debugDescription: "航线内样片 ID 重复") } } } /// 航线样片的服务端展示信息,不自行拼接预览或封面地址。 struct DroneDockPurchasedSample: Decodable, Equatable, Sendable { /// 样片稳定 ID。 let id: Int /// 样片名称,缺失时保留未知。 let name: String? /// 样片封面地址。 let coverURL: String? /// 样片视频预览地址。 let previewURL: String? /// 样片字段名。 private enum CodingKeys: String, CodingKey { case id, name, coverURL = "cover_url", previewURL = "preview_url" } /// 校验样片身份并兼容缺失、null 与空字符串的展示字段。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.positivePurchasedID(forKey: .id) name = try container.decodeIfPresent(String.self, forKey: .name) coverURL = try container.decodeIfPresent(String.self, forKey: .coverURL) previewURL = try container.decodeIfPresent(String.self, forKey: .previewURL) } } /// 购买配置 ID 的整数读取器,兼容整数字符串但拒绝布尔、分数和任意文本。 private struct DroneDockPurchasedInteger: 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 extension KeyedDecodingContainer { /// 用于点位、航线、样片稳定身份的正整数。 func positivePurchasedID(forKey key: Key) throws -> Int { let value = try decode(DroneDockPurchasedInteger.self, forKey: key).value guard value > 0 else { throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "购买配置 ID 必须为正整数") } return value } }