239 lines
6.6 KiB
TypeScript
239 lines
6.6 KiB
TypeScript
|
// 仓库模块 - 处理仓库管理、显示、交互
|
|||
|
import warehouseService from '../../../services/warehouseService';
|
|||
|
import { showToast } from '../../../utils/helpers';
|
|||
|
import { DataModule } from './dataModule';
|
|||
|
|
|||
|
export class WarehouseModule {
|
|||
|
private pageContext: any;
|
|||
|
private dataModule: DataModule;
|
|||
|
private lastLoadTime: number = 0;
|
|||
|
private isLoading: boolean = false;
|
|||
|
private loadPromise: Promise<void> | null = null;
|
|||
|
|
|||
|
constructor(pageContext: any, dataModule: DataModule) {
|
|||
|
this.pageContext = pageContext;
|
|||
|
this.dataModule = dataModule;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 加载仓库数据(带缓存和防抖机制)
|
|||
|
*/
|
|||
|
async loadWarehouses(forceRefresh: boolean = false): Promise<void> {
|
|||
|
// 防抖机制:如果正在加载,返回同一个Promise
|
|||
|
if (this.isLoading && this.loadPromise && !forceRefresh) {
|
|||
|
return this.loadPromise;
|
|||
|
}
|
|||
|
|
|||
|
// 缓存机制:5分钟内不重复加载相同数据
|
|||
|
const now = Date.now();
|
|||
|
const cacheTime = 5 * 60 * 1000; // 5分钟缓存
|
|||
|
if (!forceRefresh && now - this.lastLoadTime < cacheTime) {
|
|||
|
console.log('使用缓存的仓库数据');
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
this.isLoading = true;
|
|||
|
this.loadPromise = (async () => {
|
|||
|
try {
|
|||
|
const warehouses = await warehouseService.getWarehouses();
|
|||
|
|
|||
|
// 更新地图标记点
|
|||
|
this.updateWarehouseMarkers(warehouses);
|
|||
|
|
|||
|
this.lastLoadTime = Date.now();
|
|||
|
console.log('仓库数据加载完成,数量:', warehouses.length);
|
|||
|
} catch (error) {
|
|||
|
console.error('加载仓库数据失败:', error);
|
|||
|
showToast('加载仓库数据失败');
|
|||
|
} finally {
|
|||
|
this.isLoading = false;
|
|||
|
this.loadPromise = null;
|
|||
|
}
|
|||
|
})();
|
|||
|
|
|||
|
return this.loadPromise;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 更新仓库标记点
|
|||
|
*/
|
|||
|
private updateWarehouseMarkers(warehouses: any[]): void {
|
|||
|
const { markers } = this.pageContext.data;
|
|||
|
|
|||
|
// 移除现有的仓库标记点
|
|||
|
const filteredMarkers = markers.filter((marker: any) => marker.type !== 'warehouse');
|
|||
|
|
|||
|
// 添加新的仓库标记点
|
|||
|
const warehouseMarkers = warehouses.map((warehouse, index) => {
|
|||
|
// 验证仓库坐标是否有效
|
|||
|
let validLongitude = warehouse.longitude;
|
|||
|
let validLatitude = warehouse.latitude;
|
|||
|
|
|||
|
if (isNaN(validLongitude) || isNaN(validLatitude)) {
|
|||
|
validLongitude = 102.833722; // 默认经度
|
|||
|
validLatitude = 24.880095; // 默认纬度
|
|||
|
}
|
|||
|
|
|||
|
return {
|
|||
|
id: 1000 + index, // 仓库标记点ID从1000开始
|
|||
|
longitude: validLongitude,
|
|||
|
latitude: validLatitude,
|
|||
|
iconPath: '/images/warehouse.png',
|
|||
|
width: 24,
|
|||
|
height: 24,
|
|||
|
zIndex: 10,
|
|||
|
type: 'warehouse',
|
|||
|
data: warehouse
|
|||
|
};
|
|||
|
});
|
|||
|
|
|||
|
// 更新数据模块中的标记点
|
|||
|
const allMarkers = [...filteredMarkers, ...warehouseMarkers];
|
|||
|
this.dataModule.updateMarkers(allMarkers);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 显示仓库详情面板
|
|||
|
*/
|
|||
|
showWarehousePanel(warehouse: any, position: { x: number, y: number }): void {
|
|||
|
// 关闭其他面板
|
|||
|
this.pageContext.hideAllPanels();
|
|||
|
|
|||
|
// 更新数据模块中的当前仓库和面板状态
|
|||
|
this.dataModule.setCurrentWarehouse(warehouse);
|
|||
|
this.dataModule.setPanelPosition(position);
|
|||
|
this.dataModule.toggleWarehouseModal(true, 'bottom');
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 隐藏仓库详情面板
|
|||
|
*/
|
|||
|
hideWarehousePanel(): void {
|
|||
|
// 更新数据模块中的面板状态
|
|||
|
this.dataModule.toggleWarehouseModal(false);
|
|||
|
this.dataModule.setCurrentWarehouse(null);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 展开仓库详情面板
|
|||
|
*/
|
|||
|
expandWarehousePanel(): void {
|
|||
|
this.dataModule.toggleWarehouseModal(true, 'full');
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 收起仓库详情面板
|
|||
|
*/
|
|||
|
collapseWarehousePanel(): void {
|
|||
|
this.dataModule.toggleWarehouseModal(true, 'bottom');
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 处理仓库标记点点击
|
|||
|
*/
|
|||
|
onWarehouseMarkerClick(warehouse: any, position: { x: number, y: number }): void {
|
|||
|
console.log('仓库被点击:', warehouse);
|
|||
|
|
|||
|
// 显示仓库详情面板
|
|||
|
this.showWarehousePanel(warehouse, position);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取仓库库存状态
|
|||
|
*/
|
|||
|
getWarehouseInventoryStatus(warehouse: any): string {
|
|||
|
const capacity = warehouse.capacity || 100;
|
|||
|
const currentStock = warehouse.currentStock || 0;
|
|||
|
const percentage = (currentStock / capacity) * 100;
|
|||
|
|
|||
|
if (percentage >= 80) {
|
|||
|
return 'high';
|
|||
|
} else if (percentage >= 50) {
|
|||
|
return 'medium';
|
|||
|
} else {
|
|||
|
return 'low';
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取仓库库存状态文本
|
|||
|
*/
|
|||
|
getWarehouseInventoryStatusText(warehouse: any): string {
|
|||
|
const status = this.getWarehouseInventoryStatus(warehouse);
|
|||
|
|
|||
|
switch (status) {
|
|||
|
case 'high':
|
|||
|
return '库存充足';
|
|||
|
case 'medium':
|
|||
|
return '库存适中';
|
|||
|
case 'low':
|
|||
|
return '库存不足';
|
|||
|
default:
|
|||
|
return '未知状态';
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 获取仓库库存状态颜色
|
|||
|
*/
|
|||
|
getWarehouseInventoryStatusColor(warehouse: any): string {
|
|||
|
const status = this.getWarehouseInventoryStatus(warehouse);
|
|||
|
|
|||
|
switch (status) {
|
|||
|
case 'high':
|
|||
|
return '#52c41a'; // 绿色
|
|||
|
case 'medium':
|
|||
|
return '#faad14'; // 橙色
|
|||
|
case 'low':
|
|||
|
return '#f5222d'; // 红色
|
|||
|
default:
|
|||
|
return '#d9d9d9'; // 灰色
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 创建新仓库
|
|||
|
*/
|
|||
|
async createWarehouse(warehouseData: any): Promise<void> {
|
|||
|
try {
|
|||
|
await warehouseService.createWarehouse(warehouseData);
|
|||
|
|
|||
|
showToast('仓库创建成功');
|
|||
|
console.log('新仓库创建成功:', warehouseData);
|
|||
|
|
|||
|
// 不需要重新加载仓库数据,因为仓库数据变化不频繁
|
|||
|
// 用户可以通过手动刷新来获取最新数据
|
|||
|
|
|||
|
} catch (error) {
|
|||
|
console.error('创建仓库失败:', error);
|
|||
|
showToast('创建仓库失败');
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 删除仓库
|
|||
|
*/
|
|||
|
async deleteWarehouse(warehouseId: number): Promise<void> {
|
|||
|
try {
|
|||
|
await warehouseService.deleteWarehouse(warehouseId);
|
|||
|
|
|||
|
showToast('仓库删除成功');
|
|||
|
console.log(`仓库 ${warehouseId} 已删除`);
|
|||
|
|
|||
|
// 不需要重新加载仓库数据,因为仓库数据变化不频繁
|
|||
|
// 用户可以通过手动刷新来获取最新数据
|
|||
|
|
|||
|
} catch (error) {
|
|||
|
console.error('删除仓库失败:', error);
|
|||
|
showToast('删除仓库失败');
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 强制刷新仓库数据(忽略缓存)
|
|||
|
*/
|
|||
|
async forceRefreshWarehouses(): Promise<void> {
|
|||
|
console.log('强制刷新仓库数据');
|
|||
|
return this.loadWarehouses(true);
|
|||
|
}
|
|||
|
}
|