Files
suixinkan_ios_new/suixinkan/Features/Statistics/ViewModels/StatisticsViewModel.swift
汉秋 1970572edd 迁移合作订单完整能力,并统一 AppRoleCode 角色权限与首页菜单展示。
新增 CooperationOrder 模块(双 Tab 列表、获客员绑定、主 Tab 扫码)、订单来源/带客单绑定及配套 API 测试;同步引入 roleCode 权限匹配与相关单元测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 13:33:00 +08:00

100 lines
3.2 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.

//
// StatisticsViewModel.swift
// suixinkan
//
// Created by Codex on 2026/6/22.
//
import Foundation
import Combine
@MainActor
/// ViewModel
final class StatisticsViewModel: ObservableObject {
@Published var selectedPeriod: StatisticsPeriod = .today
@Published private(set) var loading = false
@Published private(set) var loadingMore = false
@Published private(set) var summary = StatisticsSummaryResponse()
@Published private(set) var dailyItems: [StatisticsDailyItem] = []
@Published private(set) var totalDailyCount = 0
@Published private(set) var lastRefreshAt: Date?
@Published private var page = 1
private let pageSize = 15
///
var hasMore: Bool {
dailyItems.count < totalDailyCount
}
///
func selectPeriod(_ period: StatisticsPeriod, api: StatisticsServing, scenicId: Int?, appRole: AppRoleCode?) async throws {
selectedPeriod = period
try await reload(api: api, scenicId: scenicId, appRole: appRole)
}
///
func reload(api: StatisticsServing, scenicId: Int?, appRole: AppRoleCode?, showLoading: Bool = true) async throws {
guard let scenicId else {
reset()
return
}
if showLoading { loading = true }
defer { if showLoading { loading = false } }
let isScenicAdmin = appRole?.isScenicAdmin == true
async let summaryResult = api.summary(
scenicId: scenicId,
range: selectedPeriod.apiRange,
isScenicAdmin: isScenicAdmin
)
async let dailyResult = api.dailyList(
scenicId: scenicId,
startTime: selectedPeriod.apiStartTime,
endTime: selectedPeriod.apiEndTime,
page: 1,
pageSize: pageSize,
isScenicAdmin: isScenicAdmin
)
let (summaryData, dailyData) = try await (summaryResult, dailyResult)
summary = summaryData
dailyItems = dailyData.data
totalDailyCount = dailyData.total
page = 1
lastRefreshAt = Date()
}
///
func loadMore(api: StatisticsServing, scenicId: Int?, appRole: AppRoleCode?) async throws {
guard !loadingMore, hasMore, let scenicId else { return }
loadingMore = true
defer { loadingMore = false }
let nextPage = page + 1
let result = try await api.dailyList(
scenicId: scenicId,
startTime: selectedPeriod.apiStartTime,
endTime: selectedPeriod.apiEndTime,
page: nextPage,
pageSize: pageSize,
isScenicAdmin: appRole?.isScenicAdmin == true
)
dailyItems.append(contentsOf: result.data)
totalDailyCount = result.total
page = nextPage
}
///
private func reset() {
summary = StatisticsSummaryResponse()
dailyItems = []
totalDailyCount = 0
page = 1
lastRefreshAt = nil
loading = false
loadingMore = false
}
}