Files
suixinkan_ios_new/suixinkan/Features/WithdrawalAudit/ViewModels/WithdrawalAuditViewModel.swift
汉秋 1a0d1c25f4 Add order tail flows and migrate queue, message, settlement, and audit modules.
Wire deposit orders, historical shooting, and multi-travel uploads into Orders, and replace home placeholders for queue management, message center, scenic settlement, and withdrawal audit.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-25 13:39:02 +08:00

140 lines
3.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.

//
// WithdrawalAuditViewModel.swift
// suixinkan
//
// Created by Codex on 2026/6/25.
//
import Foundation
import Observation
///
enum WithdrawalAuditFilter: String, CaseIterable, Identifiable {
case all
case processing
case completed
var id: String { rawValue }
///
var title: String {
switch self {
case .all: "全部"
case .processing: "处理中"
case .completed: "已完成"
}
}
}
@MainActor
@Observable
/// ViewModel
final class WithdrawalAuditViewModel {
var records: [WalletWithdrawRecord] = []
var selectedFilter: WithdrawalAuditFilter = .all
var isLoading = false
var isLoadingMore = false
var loadFailed = false
var loadFailureReason: String?
var errorMessage: String?
private let pageSize = 10
private var page = 1
private var total = 0
///
var filteredRecords: [WalletWithdrawRecord] {
filteredRecords(for: selectedFilter)
}
///
var hasMore: Bool {
records.count < total
}
/// 使 total
var totalCount: Int {
max(total, records.count)
}
///
var processingCount: Int {
filteredRecords(for: .processing).count
}
///
var completedCount: Int {
filteredRecords(for: .completed).count
}
///
func filteredRecords(for filter: WithdrawalAuditFilter) -> [WalletWithdrawRecord] {
switch filter {
case .all:
return records
case .processing:
return records.filter { record in
let status = record.statusLabel
return status.contains("") || status.contains("")
}
case .completed:
return records.filter { record in
let status = record.statusLabel
return status.contains("完成") || status.contains("到账") || status.contains("通过")
}
}
}
///
func selectFilter(_ filter: WithdrawalAuditFilter) {
selectedFilter = filter
}
///
func reload(api: WalletServing) async {
isLoading = true
loadFailed = false
loadFailureReason = nil
errorMessage = nil
defer { isLoading = false }
do {
let response = try await api.walletWithdrawList(page: 1, pageSize: pageSize)
records = response.item
total = response.total
page = 1
} catch {
clearRecords()
loadFailed = true
loadFailureReason = error.localizedDescription
errorMessage = error.localizedDescription
}
}
///
func loadMore(api: WalletServing) async {
guard hasMore, !isLoadingMore, !isLoading else { return }
isLoadingMore = true
errorMessage = nil
defer { isLoadingMore = false }
let nextPage = page + 1
do {
let response = try await api.walletWithdrawList(page: nextPage, pageSize: pageSize)
records += response.item
total = response.total
page = nextPage
} catch {
errorMessage = error.localizedDescription
}
}
///
private func clearRecords() {
records = []
page = 1
total = 0
isLoadingMore = false
}
}