134 lines
4.7 KiB
JavaScript
134 lines
4.7 KiB
JavaScript
"use strict";
|
||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
};
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
// 统计服务 - 处理系统统计数据获取和缓存
|
||
const apiService_1 = __importDefault(require("./apiService"));
|
||
/**
|
||
* 统计服务类
|
||
* 提供系统统计数据的获取和缓存功能
|
||
*/
|
||
class StatisticsService {
|
||
constructor() {
|
||
// 数据缓存
|
||
this.cache = null;
|
||
// 缓存超时时间(5分钟)
|
||
this.cacheTimeout = 5 * 60 * 1000;
|
||
}
|
||
/**
|
||
* 获取系统统计数据
|
||
* 采用一个API请求获取三个统计数据,后续操作客户端自行统计
|
||
* @param forceRefresh 是否强制刷新,不使用缓存
|
||
*/
|
||
async getSystemStats(forceRefresh = false) {
|
||
// 检查缓存是否有效,且不强制刷新
|
||
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获取系统统计数据
|
||
*/
|
||
async getSystemStatsFromAPI() {
|
||
try {
|
||
// 调用系统统计API
|
||
const response = await apiService_1.default.getSystemStats();
|
||
console.log('API返回的统计数据:', response);
|
||
// API返回的数据结构是 { data: { ... }, success: true }
|
||
// 需要提取data字段中的数据
|
||
const stats = response.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() {
|
||
this.cache = null;
|
||
console.log('统计数据缓存已清除');
|
||
}
|
||
/**
|
||
* 获取缓存状态
|
||
*/
|
||
getCacheStatus() {
|
||
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, count) {
|
||
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}`);
|
||
}
|
||
}
|
||
/**
|
||
* 统计服务单例实例
|
||
* 导出供应用程序全局使用
|
||
*/
|
||
exports.default = new StatisticsService();
|