feat(drone-dock): 拆分首页设备订单与统计接口

This commit is contained in:
2026-10-28 16:04:19 +08:00
parent 79884cf588
commit d01d18d5b3
11 changed files with 1290 additions and 636 deletions
@@ -1,23 +1,19 @@
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
case airports
}
/// 严格读取首页骨架,兼容服务端使用整数或整数字符串表示景区 ID。
/// 严格读取设备数组,拒绝身份缺失或重复的快照。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
scenicID = try container.decode(DroneDockHomeInteger.self, forKey: .scenicID).value
@@ -28,11 +24,39 @@ struct DroneDockHomeResponse: Decodable, Equatable, Sendable {
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 重复,无法确定订单身份")
}
}
/// 张工拍摄订单列表的一页结果;总数不由当前页条数推算。
struct DroneDockOrdersResponse: Decodable, Equatable, Sendable {
/// 当前页订单,保持后端排序。
let list: [DroneDockHomeOrder]
/// 当前筛选条件下的订单总数。
let total: Int
/// 当前响应页码,从 1 开始。
let page: Int
/// 服务端本次采用的每页数量。
let pageSize: Int
/// 分页响应字段。
private enum CodingKeys: String, CodingKey {
case list, total, page
case pageSize = "page_size"
}
/// 校验分页骨架和订单号,避免把失败数据当成空列表。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
list = try container.decode([DroneDockHomeOrder].self, forKey: .list)
total = try container.decode(DroneDockHomeInteger.self, forKey: .total).value
page = try container.decode(DroneDockHomeInteger.self, forKey: .page).value
pageSize = try container.decode(DroneDockHomeInteger.self, forKey: .pageSize).value
guard total >= 0, page > 0, (1...100).contains(pageSize), list.count <= pageSize, list.count <= total else {
throw DecodingError.dataCorruptedError(forKey: .page, in: container, debugDescription: "订单分页数据无效")
}
guard Set(list.map(\.orderID)).count == list.count else {
throw DecodingError.dataCorruptedError(forKey: .list, in: container, debugDescription: "订单号重复,无法确定订单身份")
}
today = try container.decodeIfPresent(DroneDockHomeToday.self, forKey: .today)
}
}
@@ -131,148 +155,181 @@ struct DroneDockHomeMetric: Decodable, Equatable, Sendable {
}
}
/// 首页今日统计;保留服务端字段语义,未知项不显示为零。
/// 张工的今日统计;仅覆盖本人当前景区类型 25 订单,按上海时区划分当天。
struct DroneDockHomeToday: Decodable, Equatable, Sendable {
/// 服务端的今日订单数量。
/// 今天付款且当前仍为已付款或已完成的成交订单数。
let orderCount: Int?
/// 服务端的今日订单金额,使用十进制金额避免二进制浮点误差。
/// 成交订单现金实收减成功退款,使用十进制金额。
let orderAmount: Decimal?
/// 服务端 finished_shoot 数值,其业务统计口径仍需与后端核对。
/// 今天经游客确认完成、当前状态为已完成的订单数。
let finishedShoot: Int?
/// 已验证的统计字段名。
/// today-stats 的字段,不兼容旧 home 的统计口径。
private enum CodingKeys: String, CodingKey {
case orderCount = "order_count"
case orderAmount = "order_amount"
case finishedShoot = "finished_shoot"
case orderCount = "deal_count"
case orderAmount = "deal_amount"
case finishedShoot = "completed_count"
}
/// 兼容数字和数字字符串;缺失或 null 保持 nil,非法文本拒绝解析。
/// 未知项保留 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
for (key, value) in [(CodingKeys.orderCount, orderCount), (.finishedShoot, finishedShoot)] {
if let value, value < 0 {
throw DecodingError.dataCorruptedError(forKey: key, in: container, debugDescription: "统计数量不能为负数")
}
}
}
}
/// 首页拍摄订单的展示数据;状态及动作仅用于呈现,不代表飞行执行许可。
/// 张工类型 25 订单摘要;操作入口取后端 can_* 权限,进入准备页仍需校验设备条件。
struct DroneDockHomeOrder: Decodable, Equatable, Sendable {
/// 后端订单稳定 ID,用于 Diffable 项身份。
let orderID: Int
/// 后端订单编号,保留字符串形式与前导零。
/// 订单号同时作为 Diffable 稳定身份,不虚构接口未提供的整数 ID。
let orderID: String
/// 原始订单号,保留前导零;解码时要求非空。
let orderNumber: String?
/// 后端游客展示名称。
let touristLabel: String?
/// 后端已脱敏的游客手机号。
/// 已购买项目 ID,传递给后续准备页读取购买快照。
let projectID: Int?
/// 订单创建时间的服务端字符串。
let createdAt: 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?
/// 拍摄列表状态 pending、processing、completed;未知值保留。
let shootStatus: String?
/// 订单支付和交付状态,与飞行任务状态分开。
let orderStatus: Int?
/// 整单原素材汇总状态。
let materialStatus: Int?
/// 已有成片的有效任务数。
let filmCount: Int?
/// 后端是否允许新增待飞任务。
let canCreateTask: Bool?
/// 后端是否允许上传成片。
let canUploadFilm: Bool?
/// 后端是否允许邀请游客确认完成。
let canConfirm: Bool?
/// 交付确认状态。
let confirmStatus: Int?
/// 游客确认完成时间,未完成时为空。
let confirmedAt: String?
/// 已实测的首页订单字段名。
/// 张工 orders 与订单摘要的字段。
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"
case orderNumber = "order_number", projectID = "project_id", createdAt = "created_at"
case touristPhoneMask = "user_phone", packageName = "project_name"
case shotTotal = "shoot_count", shotUsed = "shot_count", shotRemain = "remaining_count"
case shootStatus = "shoot_status", orderStatus = "order_status", materialStatus = "material_status"
case filmCount = "film_count", canCreateTask = "can_create_task", canUploadFilm = "can_upload_film"
case canConfirm = "can_confirm", confirmStatus = "confirm_status", confirmedAt = "confirmed_at"
}
/// 严格校验列表身份和计数;缺失展示字段保留未知。
/// 保留未知展示值,但拒绝无身份、负次数和错误类型。
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 必须为正整数")
let number = try container.decode(String.self, forKey: .orderNumber)
guard !number.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
number == number.trimmingCharacters(in: .whitespacesAndNewlines) else {
throw DecodingError.dataCorruptedError(forKey: .orderNumber, in: container, debugDescription: "订单号必须为有效非空字符串")
}
orderNumber = try container.decodeIfPresent(String.self, forKey: .orderNumber)
touristLabel = try container.decodeIfPresent(String.self, forKey: .touristLabel)
orderID = number
orderNumber = number
projectID = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .projectID)?.value
if let projectID, projectID <= 0 {
throw DecodingError.dataCorruptedError(forKey: .projectID, in: container, debugDescription: "项目 ID 必须为正整数")
}
createdAt = try container.decodeIfPresent(String.self, forKey: .createdAt)
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)] {
filmCount = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .filmCount)?.value
for (key, value) in [(CodingKeys.shotTotal, shotTotal), (.shotUsed, shotUsed), (.shotRemain, shotRemain), (.filmCount, filmCount)] {
if let value, value < 0 {
throw DecodingError.dataCorruptedError(forKey: key, in: container, debugDescription: "拍摄次数不能为负数")
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
shootStatus = try container.decodeIfPresent(String.self, forKey: .shootStatus)
orderStatus = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .orderStatus)?.value
materialStatus = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .materialStatus)?.value
canCreateTask = try container.decodeIfPresent(Bool.self, forKey: .canCreateTask)
canUploadFilm = try container.decodeIfPresent(Bool.self, forKey: .canUploadFilm)
canConfirm = try container.decodeIfPresent(Bool.self, forKey: .canConfirm)
confirmStatus = try container.decodeIfPresent(DroneDockHomeInteger.self, forKey: .confirmStatus)?.value
confirmedAt = try container.decodeIfPresent(String.self, forKey: .confirmedAt)
}
/// 页面稳定身份,与后端订单 ID 一致。
var id: Int { orderID }
/// 页面稳定身份。
var id: String { orderID }
/// 展示后端订单编号;缺失时不以内部 ID 冒充订单编号。
var orderNumberText: String { DroneDockHomeMetric.usableText(orderNumber) ?? "—" }
/// 原样展示后端订单号。
var orderNumberText: String { orderID }
/// 优先展示服务端游客文案,其次使用已脱敏手机号。
/// 仅使用后端提供的脱敏手机号。
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 statusTitle: String {
switch shootStatus {
case "pending": return "待拍摄"
case "processing": return "进行中"
case "completed": return "已完成"
default: return "状态待确认"
}
}
/// 优先展示后端进度文案;没有文案时逐项展示真实计数与未知占位。
/// 逐项展示真实计数和未知占位,不把任务数当作已使用次数。
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 primaryAction: String? {
if canCreateTask == true { return "start_shoot" }
if canConfirm == true { return "confirm_delivery" }
if canUploadFilm == true { return "view_materials" }
return nil
}
/// 仅展示有效的后端素材状态文案,不将空值转成未上传等新状态。
/// 与后端权限对应的按钮文案。
var actionTitle: String? {
switch primaryAction {
case "start_shoot": return "开始拍摄"
case "confirm_delivery": return "邀请确认"
case "view_materials": return "查看素材"
default: return nil
}
}
/// 映射原素材汇总状态;未知值不显示成成功或失败。
var mediaStatusTitle: String? {
DroneDockHomeMetric.usableText(mediaStatusLabel)
switch materialStatus {
case 0: return "未开始上传"
case 1: return "上传完成"
case 2: return "上传中"
case 3: return "上传失败"
default: return nil
}
}
}