94 lines
4.4 KiB
Swift
94 lines
4.4 KiB
Swift
import Foundation
|
|
|
|
/// 未确认交卷的原始请求及所属业务身份,供 App 重启后沿原键恢复。
|
|
struct TrainingPendingSubmission: Codable, Equatable, Sendable {
|
|
let taskID: String
|
|
let request: TrainingSubmissionRequest
|
|
let accountScope: String
|
|
let scenicID: Int
|
|
let storeID: Int
|
|
|
|
/// 固定本次提交的身份和请求,恢复时不读取新的草稿版本替换它。
|
|
init(taskID: String, request: TrainingSubmissionRequest, context: TrainingContext) {
|
|
self.taskID = taskID
|
|
self.request = request
|
|
accountScope = context.accountScope
|
|
scenicID = context.scenicID
|
|
storeID = context.storeID
|
|
}
|
|
|
|
/// 本次交卷的持久化作用域,不包含 Token。
|
|
var context: TrainingContext {
|
|
TrainingContext(accountScope: accountScope, scenicID: scenicID, storeID: storeID)
|
|
}
|
|
}
|
|
|
|
/// 待恢复记录损坏时停止生成新提交,避免重复成绩。
|
|
enum TrainingPendingSubmissionError: Error, LocalizedError, Equatable, Sendable {
|
|
case invalidRecord, alreadyPending
|
|
|
|
/// 告知用户尚不能安全恢复本次提交。
|
|
var errorDescription: String? {
|
|
switch self {
|
|
case .invalidRecord: "上次交卷状态暂无法确认,请联系工作人员核查成绩后重试"
|
|
case .alreadyPending: "上次交卷尚未确认,请先恢复原提交结果"
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 按账号、景区、店铺和任务隔离待确认交卷,支持注入独立 UserDefaults 测试。
|
|
final class TrainingPendingSubmissionStore {
|
|
private static let lock = NSRecursiveLock()
|
|
private let defaults: UserDefaults
|
|
|
|
/// 使用指定的持久化域保存不含凭证的交卷请求。
|
|
init(defaults: UserDefaults = .standard) { self.defaults = defaults }
|
|
|
|
/// 草稿与交卷使用同一注入域,但以独立的键和数据格式保存。
|
|
func makeDraftStore() -> TrainingDraftStore { TrainingDraftStore(defaults: defaults) }
|
|
|
|
/// 当前店铺登录身份;与正式数据源一致,不要求景区 ID 大于零。
|
|
@MainActor static func currentContext() -> TrainingContext? {
|
|
let session = AppStore.shared.session
|
|
guard session.isLoggedIn, session.accountType == .storeUser, !session.accountCachePrefix.isEmpty else { return nil }
|
|
return TrainingContext(accountScope: session.accountCachePrefix, scenicID: session.currentScenicId, storeID: session.currentStoreId)
|
|
}
|
|
|
|
/// 读取当前作用域记录,损坏记录不当作首次交卷处理。
|
|
func load(taskID: String, context: TrainingContext) throws -> TrainingPendingSubmission? {
|
|
Self.lock.lock()
|
|
defer { Self.lock.unlock() }
|
|
guard let data = defaults.data(forKey: key(taskID: taskID, context: context)) else { return nil }
|
|
guard let value = try? JSONDecoder().decode(TrainingPendingSubmission.self, from: data),
|
|
value.taskID == taskID, value.context == context,
|
|
!value.request.submissionID.isEmpty, !value.request.paperSnapshotID.isEmpty,
|
|
value.request.draftRevision > 0 else { throw TrainingPendingSubmissionError.invalidRecord }
|
|
return value
|
|
}
|
|
|
|
/// 在发起网络提交前保存原始请求;不得用当前版本覆盖待恢复版本。
|
|
func save(_ value: TrainingPendingSubmission) throws {
|
|
Self.lock.lock()
|
|
defer { Self.lock.unlock() }
|
|
if let existing = try load(taskID: value.taskID, context: value.context), existing != value {
|
|
throw TrainingPendingSubmissionError.alreadyPending
|
|
}
|
|
let storageKey = key(taskID: value.taskID, context: value.context)
|
|
defaults.set(try JSONEncoder().encode(value), forKey: storageKey)
|
|
}
|
|
|
|
/// 只移除仍对应这次提交的记录,避免迟到响应删除后续待恢复请求。
|
|
func remove(_ value: TrainingPendingSubmission) throws {
|
|
Self.lock.lock()
|
|
defer { Self.lock.unlock() }
|
|
guard try load(taskID: value.taskID, context: value.context) == value else { return }
|
|
defaults.removeObject(forKey: key(taskID: value.taskID, context: value.context))
|
|
}
|
|
|
|
private func key(taskID: String, context: TrainingContext) -> String {
|
|
let scope = Data(context.accountScope.utf8).map { String(format: "%02x", $0) }.joined()
|
|
let task = Data(taskID.utf8).map { String(format: "%02x", $0) }.joined()
|
|
return "training-pending-submission:v1:\(scope):scenic_\(context.scenicID):store_\(context.storeID):task_\(task)"
|
|
}
|
|
}
|