Files
suixinkan_ios_new/suixinkan/Features/CooperationOrder/ViewModels/CooperationAcquirerViewModel.swift

56 lines
1.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// CooperationAcquirerViewModel.swift
// suixinkan
//
// Created by Codex on 2026/6/29.
//
import Foundation
import Combine
/// ViewModel
@MainActor
final class CooperationAcquirerViewModel: ObservableObject {
@Published var acquirers: [CooperativeSaler] = []
@Published private(set) var hasLoadedOnce = false
///
func reload(api: CooperationOrderServing, storeId: Int?) async {
do {
let storeParam = storeId.flatMap { $0 > 0 ? $0 : nil }
let data = try await api.cooperativeSalers(storeId: storeParam, page: 1, pageSize: 20)
acquirers = data.list
} catch {
if !hasLoadedOnce {
acquirers = []
}
}
hasLoadedOnce = true
}
///
func updateRemark(
saleUserId: Int,
remark: String,
api: CooperationOrderServing
) async throws {
guard saleUserId > 0 else { return }
if let message = CooperationOrderRemarkName.validationMessage(for: remark) {
throw CooperationOrderRemarkValidationError(message: message)
}
let trimmedRemark = CooperationOrderRemarkName.trimmed(remark)
try await api.saleUserUpdateRemark(saleUserId: saleUserId, photogRemarkName: trimmedRemark)
acquirers = acquirers.map { acquirer in
guard acquirer.saleUserId == saleUserId else { return acquirer }
return acquirer.withPhotogRemarkName(trimmedRemark)
}
}
}
///
struct CooperationOrderRemarkValidationError: LocalizedError {
let message: String
var errorDescription: String? { message }
}