Files
WXProgram/miniprogram/services/statisticsService.ts

158 lines
4.5 KiB
TypeScript
Raw Permalink Normal View History

2025-10-26 13:15:04 +08:00
// 统计服务 - 处理系统统计数据获取和缓存
import apiService from './apiService';
/**
*
*/
export interface StatisticsData {
employeeCount: number; // 员工总数
orderCount: number; // 订单总数
warehouseCount: number; // 仓库总数
pendingOrders: number; // 待处理订单数
activeOrders: number; // 进行中订单数
completedOrders: number; // 已完成订单数
timestamp: number; // 数据时间戳
}
/**
*
*
*/
class StatisticsService {
// 数据缓存
private cache: StatisticsData | null = null;
// 缓存超时时间5分钟
private readonly cacheTimeout = 5 * 60 * 1000;
/**
*
* API请求获取三个统计数据
* @param forceRefresh 使
*/
async getSystemStats(forceRefresh: boolean = false): Promise<StatisticsData> {
// 检查缓存是否有效,且不强制刷新
if (!forceRefresh && this.cache && Date.now() - this.cache.timestamp < this.cacheTimeout) {
console.log('使用缓存的统计数据');
return this.cache;
}
console.log('重新获取统计数据');
try {
// 使用系统统计API获取三个统计数据
const systemStats = await this.getSystemStatsFromAPI();
this.cache = {
...systemStats,
timestamp: Date.now()
};
return this.cache;
} catch (error) {
console.warn('系统统计API不可用使用默认值:', error);
// 如果API不可用返回默认值
this.cache = {
employeeCount: 0,
orderCount: 0,
warehouseCount: 0,
pendingOrders: 0,
activeOrders: 0,
completedOrders: 0,
timestamp: Date.now()
};
return this.cache;
}
}
/**
* API获取系统统计数据
*/
private async getSystemStatsFromAPI(): Promise<StatisticsData> {
try {
// 调用系统统计API
const response = await apiService.getSystemStats();
console.log('API返回的统计数据:', response);
// API返回的数据结构是 { data: { ... }, success: true }
// 需要提取data字段中的数据
const stats = (response as any).data || response;
console.log('提取的统计数据:', stats);
// 转换API响应格式为StatisticsData格式
// 根据API实际返回的字段进行映射
const result = {
employeeCount: stats.totalEmployees || stats.totalDeliveryPersons || 0,
orderCount: stats.totalOrders || (stats.pendingOrders || 0) + (stats.activeOrders || 0),
warehouseCount: stats.totalWarehouses || 0,
pendingOrders: stats.pendingOrders || 0,
activeOrders: stats.activeOrders || 0,
completedOrders: 0, // 需要从其他接口获取
timestamp: Date.now()
};
console.log('转换后的统计数据:', result);
return result;
} catch (error) {
console.error('获取系统统计数据失败:', error);
throw error;
}
}
/**
*
*/
refreshStats(): void {
this.cache = null;
console.log('统计数据缓存已清除');
}
/**
*
*/
getCacheStatus(): { hasCache: boolean; isExpired: boolean; age: number } {
if (!this.cache) {
return { hasCache: false, isExpired: true, age: 0 };
}
const age = Date.now() - this.cache.timestamp;
const isExpired = age > this.cacheTimeout;
return { hasCache: true, isExpired, age };
}
/**
*
* @param type 'employee' | 'order' | 'warehouse'
* @param count
*/
updateStats(type: 'employee' | 'order' | 'warehouse', count: number): void {
if (!this.cache) {
console.warn('没有缓存数据,无法更新统计');
return;
}
switch (type) {
case 'employee':
this.cache.employeeCount = Math.max(0, count);
break;
case 'order':
this.cache.orderCount = Math.max(0, count);
break;
case 'warehouse':
this.cache.warehouseCount = Math.max(0, count);
break;
}
this.cache.timestamp = Date.now();
console.log(`统计数据已更新: ${type} = ${count}`);
}
}
/**
*
* 使
*/
export default new StatisticsService();