diff --git a/src/services/storage-location.service.ts b/src/services/storage-location.service.ts index f5d5980..a13757a 100644 --- a/src/services/storage-location.service.ts +++ b/src/services/storage-location.service.ts @@ -274,6 +274,82 @@ export class StorageLocationService { } } + /** + * 更新指定点的边框颜色(优化版本,只更新有变化的点) + * @param changedPointIds 有变化的点ID列表 + */ + private updatePointBorderColorsOptimized(changedPointIds: Set) { + for (const pointId of changedPointIds) { + this.updatePointBorderColor(pointId); + } + } + + /** + * 检查库位数据是否有变化,避免不必要的更新 + * @param newLocationsByPointId 新的库位数据 + * @returns 是否有变化 + */ + private hasStorageLocationDataChanged(newLocationsByPointId: Map): boolean { + const currentData = this.storageLocations.value; + + // 如果数据量不同,肯定有变化 + if (currentData.size !== newLocationsByPointId.size) { + return true; + } + + // 检查每个点的数据是否有变化 + for (const [pointId, newLocations] of newLocationsByPointId.entries()) { + const currentLocations = currentData.get(pointId); + if (!currentLocations || currentLocations.length !== newLocations.length) { + return true; + } + + // 检查每个库位的状态是否有变化 + for (const newLocation of newLocations) { + const currentLocation = currentLocations.find(loc => loc.id === newLocation.id); + if (!currentLocation || + currentLocation.is_occupied !== newLocation.is_occupied || + currentLocation.is_locked !== newLocation.is_locked) { + return true; + } + } + } + + return false; + } + + /** + * 检查库位数据变化并返回有变化的点ID列表 + * @param newLocationsByPointId 新的库位数据 + * @returns 有变化的点ID列表 + */ + private getChangedPointIds(newLocationsByPointId: Map): Set { + const changedPointIds = new Set(); + const currentData = this.storageLocations.value; + + // 检查每个点的数据是否有变化 + for (const [pointId, newLocations] of newLocationsByPointId.entries()) { + const currentLocations = currentData.get(pointId); + if (!currentLocations || currentLocations.length !== newLocations.length) { + changedPointIds.add(pointId); + continue; + } + + // 检查每个库位的状态是否有变化 + for (const newLocation of newLocations) { + const currentLocation = currentLocations.find(loc => loc.id === newLocation.id); + if (!currentLocation || + currentLocation.is_occupied !== newLocation.is_occupied || + currentLocation.is_locked !== newLocation.is_locked) { + changedPointIds.add(pointId); + break; + } + } + } + + return changedPointIds; + } + /** * 处理库位状态更新消息 * @param message 库位状态更新消息 @@ -299,6 +375,12 @@ export class StorageLocationService { } }); + // 检查数据是否有变化,避免不必要的更新 + const changedPointIds = this.getChangedPointIds(locationsByPointId); + if (changedPointIds.size === 0) { + return; // 数据没有变化,跳过更新 + } + this.storageLocations.value = locationsByPointId; // 同步维护 storageStateMap @@ -307,16 +389,12 @@ export class StorageLocationService { const inner = new Map(); list.forEach((loc) => { inner.set(loc.layer_name, { occupied: loc.is_occupied, locked: loc.is_locked }); - // console.log(`更新库位状态映射: ${pointId} - ${loc.layer_name}`, { - // occupied: loc.is_occupied, - // locked: loc.is_locked - // }); }); storageStateMap.set(pointId, inner); } - // 更新动作点的边框颜色 - this.updatePointBorderColors(); + // 只更新有变化的点的边框颜色 + this.updatePointBorderColorsOptimized(changedPointIds); // 重新创建所有动作点的库位pen对象以反映最新状态 this.createAll();