import Foundation /// 订单素材库的整单摘要和购买点位,不受当前素材筛选条件影响。 struct DroneDockMaterialDetail: Decodable, Equatable, Sendable { /// 复用张工订单摘要中的次数、状态和操作权限。 let order: DroneDockHomeOrder /// 按购买快照顺序返回的点位筛选项。 let spots: [DroneDockMaterialSpot] /// 订单摘要位于根级,购买点位使用单数 spot 字段。 private enum CodingKeys: String, CodingKey { case spots = "spot" } /// 显式空数组表示无点位;缺少配置或重复身份视为异常响应。 init(from decoder: Decoder) throws { order = try DroneDockHomeOrder(from: decoder) let container = try decoder.container(keyedBy: CodingKeys.self) spots = try container.decode([DroneDockMaterialSpot].self, forKey: .spots) guard Set(spots.map(\.id)).count == spots.count else { throw DecodingError.dataCorruptedError(forKey: .spots, in: container, debugDescription: "素材库点位 ID 重复") } } } /// 订单购买快照中的点位筛选项。 struct DroneDockMaterialSpot: Decodable, Equatable, Sendable { /// 点位稳定身份,向 tasks 传递为 scenic_spot_id。 let id: Int /// 点位名称,缺失时由页面显示占位文本。 let name: String /// 张工点位快照字段。 private enum CodingKeys: String, CodingKey { case id = "scenic_spot_id", name = "scenic_spot_name" } /// 点位身份必须有效,缺失名称不影响查看该点位任务。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.positiveMaterialID(forKey: .id) name = try container.decodeIfPresent(String.self, forKey: .name) ?? "" } } /// 当前订单及点位范围内的全部有效任务;接口一次返回,无分页。 struct DroneDockMaterialTasks: Decodable, Equatable, Sendable { /// 任务列表所属订单号。 let orderNumber: String /// 当前范围内的任务记录数,不能用作成功拍摄次数。 let total: Int /// 保留服务端顺序及待飞、失败等任务。 let list: [DroneDockMaterialTask] /// tasks 响应根字段。 private enum CodingKeys: String, CodingKey { case orderNumber = "order_number", total, list } /// 校验完整列表及订单归属,避免异常结构被展示成真实空态。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) orderNumber = try container.materialOrderNumber(forKey: .orderNumber) total = try container.decode(DroneDockMaterialInteger.self, forKey: .total).value list = try container.decode([DroneDockMaterialTask].self, forKey: .list) guard total >= 0, total == list.count else { throw DecodingError.dataCorruptedError(forKey: .total, in: container, debugDescription: "任务总数与完整列表不一致") } guard Set(list.map(\.id)).count == list.count else { throw DecodingError.dataCorruptedError(forKey: .list, in: container, debugDescription: "素材库任务 ID 重复") } guard list.allSatisfy({ $0.orderNumber == orderNumber }) else { throw DecodingError.dataCorruptedError(forKey: .list, in: container, debugDescription: "任务不属于当前订单") } } } /// 一次有效飞行任务及其原素材、当前成片,不以素材是否就绪隐藏任务。 struct DroneDockMaterialTask: Decodable, Equatable, Sendable { /// 订单飞行任务身份,与实际飞行平台任务编号不同。 let id: Int /// 任务所属订单号。 let orderNumber: String /// 购买快照中的点位身份。 let spotID: Int /// 购买快照中的点位名称,缺失时为空。 let spotName: String /// 购买快照中的航线名称。 let airLineName: String? /// 订单内任务序号,不代表成功拍摄次数。 let sequence: Int? /// 后端飞行状态,未知状态原样保留。 let flightStatus: Int? /// 本任务原素材聚合状态。 let materialStatus: Int? /// 服务端记录的任务创建时间。 let createdAt: String? /// 本次任务的原素材,显式空数组表示尚无素材。 let materials: [DroneDockMaterialFile] /// 当前已交付的视频成片,未交付时为空。 let film: DroneDockMaterialFilm? /// 张工任务字段,id 与 task_id 表示同一任务。 private enum CodingKeys: String, CodingKey { case id, taskID = "task_id", orderNumber = "order_number" case spotID = "scenic_spot_id", spotName = "scenic_spot_name", airLineName = "air_line_name" case sequence = "seq", flightStatus = "flight_status", materialStatus = "material_status" case createdAt = "created_at", materials, film } /// 兼容两种任务 ID 字段,但拒绝冲突、重复素材及其他任务的成片。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) let identifier = try container.optionalPositiveMaterialID(forKey: .id) let taskIdentifier = try container.optionalPositiveMaterialID(forKey: .taskID) guard let resolvedID = identifier ?? taskIdentifier, identifier == nil || taskIdentifier == nil || identifier == taskIdentifier else { throw DecodingError.dataCorruptedError(forKey: .id, in: container, debugDescription: "任务 ID 缺失或不一致") } id = resolvedID orderNumber = try container.materialOrderNumber(forKey: .orderNumber) spotID = try container.positiveMaterialID(forKey: .spotID) spotName = try container.decodeIfPresent(String.self, forKey: .spotName) ?? "" airLineName = try container.decodeIfPresent(String.self, forKey: .airLineName) sequence = try container.optionalMaterialInteger(forKey: .sequence) flightStatus = try container.optionalMaterialInteger(forKey: .flightStatus) materialStatus = try container.optionalMaterialInteger(forKey: .materialStatus) createdAt = try container.decodeIfPresent(String.self, forKey: .createdAt) materials = try container.decode([DroneDockMaterialFile].self, forKey: .materials) film = try container.decodeIfPresent(DroneDockMaterialFilm.self, forKey: .film) guard Set(materials.map(\.id)).count == materials.count else { throw DecodingError.dataCorruptedError(forKey: .materials, in: container, debugDescription: "任务内素材 ID 重复") } if let filmTaskID = film?.taskID, filmTaskID != id { throw DecodingError.dataCorruptedError(forKey: .film, in: container, debugDescription: "成片不属于当前任务") } } } /// 飞行模块回写的原素材,上传未完成时保留其状态和失败说明。 struct DroneDockMaterialFile: Decodable, Equatable, Sendable { /// 素材稳定身份,在所属任务内唯一。 let id: Int /// 1 为图片、2 为视频,未知值保留且不开放预览。 let fileType: Int? /// 原素材文件名。 let fileName: String? /// 原素材字节数,缺失时不推测。 let fileSize: Int? /// 后端提供的封面地址。 let coverURL: String? /// 后端提供的文件地址,未上传完成时可能为空串。 let fileURL: String? /// 0 未开始、1 已就绪、2 上传中、3 失败。 let uploadStatus: Int? /// 服务端返回的上传失败原因。 let failureReason: String? /// 只有明确就绪且类型受支持的 HTTP(S) 文件才可预览。 var previewURL: URL? { guard uploadStatus == 1, fileType == 1 || fileType == 2 else { return nil } return DroneDockMaterialURL.preview(fileURL) } /// 张工原素材字段。 private enum CodingKeys: String, CodingKey { case id, fileType = "file_type", fileName = "file_name", fileSize = "file_size" case coverURL = "cover_url", fileURL = "file_url", uploadStatus = "upload_status" case failureReason = "failure_reason" } /// 解码可为空的回写信息,素材身份仍必须有效。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.positiveMaterialID(forKey: .id) fileType = try container.optionalMaterialInteger(forKey: .fileType) fileName = try container.decodeIfPresent(String.self, forKey: .fileName) fileSize = try container.optionalMaterialSize(forKey: .fileSize) coverURL = try container.decodeIfPresent(String.self, forKey: .coverURL) fileURL = try container.decodeIfPresent(String.self, forKey: .fileURL) uploadStatus = try container.optionalMaterialInteger(forKey: .uploadStatus) failureReason = try container.decodeIfPresent(String.self, forKey: .failureReason) } } /// 当前任务已交付的成片;缺少有效地址时保留记录但不提供预览。 struct DroneDockMaterialFilm: Decodable, Equatable, Sendable { /// 成片稳定身份。 let id: Int /// 所属任务身份,接口提供时必须与外层任务一致。 let taskID: Int? /// 成片文件名。 let fileName: String? /// 成片字节数。 let fileSize: Int? /// 后端提供的成片封面地址。 let coverURL: String? /// 后端提供的成片视频地址。 let fileURL: String? /// 已交付成片只接受有效 HTTP(S) 视频地址。 var previewURL: URL? { DroneDockMaterialURL.preview(fileURL) } /// 张工当前成片字段。 private enum CodingKeys: String, CodingKey { case id, taskID = "task_id", fileName = "file_name", fileSize = "file_size" case coverURL = "cover_url", fileURL = "file_url" } /// 容忍未回写的展示字段,保留可校验的任务归属。 init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.positiveMaterialID(forKey: .id) taskID = try container.optionalPositiveMaterialID(forKey: .taskID) fileName = try container.decodeIfPresent(String.self, forKey: .fileName) fileSize = try container.optionalMaterialSize(forKey: .fileSize) coverURL = try container.decodeIfPresent(String.self, forKey: .coverURL) fileURL = try container.decodeIfPresent(String.self, forKey: .fileURL) } } /// 素材库整数读取器,兼容整数字符串并拒绝布尔、分数和任意文本。 private struct DroneDockMaterialInteger: 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 enum DroneDockMaterialURL { /// 拒绝空值、空白字符、本地地址和无主机的 URL。 static func preview(_ value: String?) -> URL? { guard let value, !value.isEmpty, value.rangeOfCharacter(from: .whitespacesAndNewlines) == nil, let url = URL(string: value), let scheme = url.scheme?.lowercased(), ["https", "http"].contains(scheme), let host = url.host, !host.isEmpty else { return nil } return url } } private extension KeyedDecodingContainer { /// 素材、任务、点位及成片必须有正整数身份。 func positiveMaterialID(forKey key: Key) throws -> Int { let value = try decode(DroneDockMaterialInteger.self, forKey: key).value guard value > 0 else { throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "素材库 ID 必须为正整数") } return value } /// 可选关联身份在提供值时仍要求正整数。 func optionalPositiveMaterialID(forKey key: Key) throws -> Int? { guard let value = try optionalMaterialInteger(forKey: key) else { return nil } guard value > 0 else { throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "素材库关联 ID 必须为正整数") } return value } /// 状态码不限定已知枚举,缺失或 null 保留未知。 func optionalMaterialInteger(forKey key: Key) throws -> Int? { try decodeIfPresent(DroneDockMaterialInteger.self, forKey: key)?.value } /// 文件大小可缺失,但不能出现负字节数。 func optionalMaterialSize(forKey key: Key) throws -> Int? { let value = try optionalMaterialInteger(forKey: key) if let value, value < 0 { throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "素材大小不能为负数") } return value } /// 保留订单号前导零,禁止缺失或带首尾空白的身份。 func materialOrderNumber(forKey key: Key) throws -> String { let value = try decode(String.self, forKey: key) guard !value.isEmpty, value == value.trimmingCharacters(in: .whitespacesAndNewlines) else { throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "订单号必须为有效非空字符串") } return value } }