214 lines
7.3 KiB
JavaScript
214 lines
7.3 KiB
JavaScript
"use strict";
|
||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
};
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.WarehouseModule = void 0;
|
||
// 仓库模块 - 处理仓库管理、显示、交互
|
||
const warehouseService_1 = __importDefault(require("../../../services/warehouseService"));
|
||
const helpers_1 = require("../../../utils/helpers");
|
||
class WarehouseModule {
|
||
constructor(pageContext, dataModule) {
|
||
this.lastLoadTime = 0;
|
||
this.isLoading = false;
|
||
this.loadPromise = null;
|
||
this.pageContext = pageContext;
|
||
this.dataModule = dataModule;
|
||
}
|
||
/**
|
||
* 加载仓库数据(带缓存和防抖机制)
|
||
*/
|
||
async loadWarehouses(forceRefresh = false) {
|
||
// 防抖机制:如果正在加载,返回同一个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_1.default.getWarehouses();
|
||
// 更新地图标记点
|
||
this.updateWarehouseMarkers(warehouses);
|
||
this.lastLoadTime = Date.now();
|
||
console.log('仓库数据加载完成,数量:', warehouses.length);
|
||
}
|
||
catch (error) {
|
||
console.error('加载仓库数据失败:', error);
|
||
(0, helpers_1.showToast)('加载仓库数据失败');
|
||
}
|
||
finally {
|
||
this.isLoading = false;
|
||
this.loadPromise = null;
|
||
}
|
||
})();
|
||
return this.loadPromise;
|
||
}
|
||
/**
|
||
* 更新仓库标记点
|
||
*/
|
||
updateWarehouseMarkers(warehouses) {
|
||
const { markers } = this.pageContext.data;
|
||
// 移除现有的仓库标记点
|
||
const filteredMarkers = markers.filter((marker) => 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, position) {
|
||
// 关闭其他面板
|
||
this.pageContext.hideAllPanels();
|
||
// 更新数据模块中的当前仓库和面板状态
|
||
this.dataModule.setCurrentWarehouse(warehouse);
|
||
this.dataModule.setPanelPosition(position);
|
||
this.dataModule.toggleWarehouseModal(true, 'bottom');
|
||
}
|
||
/**
|
||
* 隐藏仓库详情面板
|
||
*/
|
||
hideWarehousePanel() {
|
||
// 更新数据模块中的面板状态
|
||
this.dataModule.toggleWarehouseModal(false);
|
||
this.dataModule.setCurrentWarehouse(null);
|
||
}
|
||
/**
|
||
* 展开仓库详情面板
|
||
*/
|
||
expandWarehousePanel() {
|
||
this.dataModule.toggleWarehouseModal(true, 'full');
|
||
}
|
||
/**
|
||
* 收起仓库详情面板
|
||
*/
|
||
collapseWarehousePanel() {
|
||
this.dataModule.toggleWarehouseModal(true, 'bottom');
|
||
}
|
||
/**
|
||
* 处理仓库标记点点击
|
||
*/
|
||
onWarehouseMarkerClick(warehouse, position) {
|
||
console.log('仓库被点击:', warehouse);
|
||
// 显示仓库详情面板
|
||
this.showWarehousePanel(warehouse, position);
|
||
}
|
||
/**
|
||
* 获取仓库库存状态
|
||
*/
|
||
getWarehouseInventoryStatus(warehouse) {
|
||
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) {
|
||
const status = this.getWarehouseInventoryStatus(warehouse);
|
||
switch (status) {
|
||
case 'high':
|
||
return '库存充足';
|
||
case 'medium':
|
||
return '库存适中';
|
||
case 'low':
|
||
return '库存不足';
|
||
default:
|
||
return '未知状态';
|
||
}
|
||
}
|
||
/**
|
||
* 获取仓库库存状态颜色
|
||
*/
|
||
getWarehouseInventoryStatusColor(warehouse) {
|
||
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) {
|
||
try {
|
||
await warehouseService_1.default.createWarehouse(warehouseData);
|
||
(0, helpers_1.showToast)('仓库创建成功');
|
||
console.log('新仓库创建成功:', warehouseData);
|
||
// 不需要重新加载仓库数据,因为仓库数据变化不频繁
|
||
// 用户可以通过手动刷新来获取最新数据
|
||
}
|
||
catch (error) {
|
||
console.error('创建仓库失败:', error);
|
||
(0, helpers_1.showToast)('创建仓库失败');
|
||
}
|
||
}
|
||
/**
|
||
* 删除仓库
|
||
*/
|
||
async deleteWarehouse(warehouseId) {
|
||
try {
|
||
await warehouseService_1.default.deleteWarehouse(warehouseId);
|
||
(0, helpers_1.showToast)('仓库删除成功');
|
||
console.log(`仓库 ${warehouseId} 已删除`);
|
||
// 不需要重新加载仓库数据,因为仓库数据变化不频繁
|
||
// 用户可以通过手动刷新来获取最新数据
|
||
}
|
||
catch (error) {
|
||
console.error('删除仓库失败:', error);
|
||
(0, helpers_1.showToast)('删除仓库失败');
|
||
}
|
||
}
|
||
/**
|
||
* 强制刷新仓库数据(忽略缓存)
|
||
*/
|
||
async forceRefreshWarehouses() {
|
||
console.log('强制刷新仓库数据');
|
||
return this.loadWarehouses(true);
|
||
}
|
||
}
|
||
exports.WarehouseModule = WarehouseModule;
|