feat: 增强库位服务,添加颜色计算和批量更新功能,优化pen对象状态管理
This commit is contained in:
parent
0e38d2fff6
commit
bc836c22c7
@ -224,6 +224,24 @@ export class StorageLocationService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据占用状态计算颜色
|
||||
* @param isOccupied 是否被占用
|
||||
* @returns 对应的颜色值
|
||||
*/
|
||||
private getColorByOccupiedState(isOccupied: boolean): string {
|
||||
return isOccupied ? this.config.colors.occupied : this.config.colors.available;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认颜色配置
|
||||
* @returns 默认颜色
|
||||
*/
|
||||
private getDefaultColor(): string {
|
||||
return this.config.colors.default;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新单个动作点的边框颜色
|
||||
* @param pointId 画布点ID
|
||||
@ -238,7 +256,7 @@ export class StorageLocationService {
|
||||
const allOccupied = locations.every((loc) => loc.is_occupied);
|
||||
const allLocked = locations.every((loc) => loc.is_locked);
|
||||
|
||||
const color = allOccupied ? this.config.colors.occupied : this.config.colors.available;
|
||||
const color = this.getColorByOccupiedState(allOccupied);
|
||||
|
||||
// 更新边框颜色
|
||||
this.editor?.updatePointBorderColor(pointId, color);
|
||||
@ -443,12 +461,11 @@ export class StorageLocationService {
|
||||
|
||||
// 处理库位名称列表更新(用于重新创建时更新状态)
|
||||
if (Array.isArray(updates)) {
|
||||
const states: Record<string, { occupied: boolean; locked: boolean }> = {};
|
||||
updates.forEach(locationName => {
|
||||
const inner = storageStateMap.get(pointId);
|
||||
const state = inner?.get(locationName) || { occupied: false, locked: false };
|
||||
states[locationName] = { occupied: !!state.occupied, locked: !!state.locked };
|
||||
});
|
||||
const states = updates.reduce((acc, locationName) => {
|
||||
const state = storageStateMap.get(pointId)?.get(locationName) || { occupied: false, locked: false };
|
||||
acc[locationName] = { occupied: !!state.occupied, locked: !!state.locked };
|
||||
return acc;
|
||||
}, {} as Record<string, { occupied: boolean; locked: boolean }>);
|
||||
this.update(pointId, states);
|
||||
return;
|
||||
}
|
||||
@ -463,12 +480,8 @@ export class StorageLocationService {
|
||||
|
||||
// 查找并更新pen对象
|
||||
const storagePen = this.findStorageLocationPen(pointId, updates);
|
||||
if (storagePen?.storageLocation) {
|
||||
const updatedState = { ...storagePen.storageLocation, ...state };
|
||||
this.editor.updatePen(storagePen.id!, {
|
||||
storageLocation: updatedState,
|
||||
background: updatedState.occupied ? this.config.colors.occupied : this.config.colors.default,
|
||||
}, false);
|
||||
if (storagePen?.storageLocation && storagePen.id) {
|
||||
this.updatePenStorageState(storagePen.id, storagePen.storageLocation, state);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -476,26 +489,17 @@ export class StorageLocationService {
|
||||
// 处理批量更新
|
||||
if (typeof updates === 'object') {
|
||||
// 批量同步状态到storageStateMap
|
||||
Object.entries(updates).forEach(([locationName, state]) => {
|
||||
this.syncStorageStateToMap(pointId, locationName, {
|
||||
occupied: state.occupied || false,
|
||||
locked: state.locked || false
|
||||
});
|
||||
});
|
||||
this.batchSyncStorageStateToMap(pointId, updates);
|
||||
|
||||
// 批量更新pen对象
|
||||
const storagePens = this.getStorageLocationPens(pointId);
|
||||
storagePens.forEach(pen => {
|
||||
if (pen.storageLocation) {
|
||||
if (pen.storageLocation && pen.id) {
|
||||
const locationName = pen.storageLocation.locationName;
|
||||
const state = updates[locationName];
|
||||
|
||||
if (state && this.editor) {
|
||||
const updatedState = { ...pen.storageLocation, ...state };
|
||||
this.editor.updatePen(pen.id!, {
|
||||
storageLocation: updatedState,
|
||||
background: updatedState.occupied ? this.config.colors.occupied : this.config.colors.default,
|
||||
}, false);
|
||||
if (state) {
|
||||
this.updatePenStorageState(pen.id, pen.storageLocation, state);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -509,11 +513,13 @@ export class StorageLocationService {
|
||||
public delete(pointId: string): void {
|
||||
if (!this.editor) return;
|
||||
|
||||
const { penTypes, tags } = this.config;
|
||||
const storagePens = this.editor.find(
|
||||
`${this.config.penTypes.storageLocation},${this.config.penTypes.storageMore},${this.config.penTypes.storageBackground}`
|
||||
`${penTypes.storageLocation},${penTypes.storageMore},${penTypes.storageBackground}`
|
||||
).filter(
|
||||
(pen) => pen.tags?.includes(`${this.config.tags.pointPrefix}${pointId}`)
|
||||
(pen) => pen.tags?.includes(`${tags.pointPrefix}${pointId}`)
|
||||
);
|
||||
|
||||
if (storagePens.length > 0) {
|
||||
this.editor.delete(storagePens, true, true);
|
||||
}
|
||||
@ -537,16 +543,59 @@ export class StorageLocationService {
|
||||
inner.set(locationName, state);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新pen对象的存储库位状态和背景颜色
|
||||
* @param penId pen对象ID
|
||||
* @param storageLocation 存储库位信息
|
||||
* @param state 新的状态
|
||||
*/
|
||||
private updatePenStorageState(
|
||||
penId: string,
|
||||
storageLocation: any,
|
||||
state: { occupied?: boolean; locked?: boolean }
|
||||
): void {
|
||||
if (!this.editor) return;
|
||||
|
||||
const updatedState = { ...storageLocation, ...state };
|
||||
this.editor.updatePen(penId, {
|
||||
storageLocation: updatedState,
|
||||
background: this.getColorByOccupiedState(updatedState.occupied || false),
|
||||
}, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新库位状态到storageStateMap
|
||||
* @param pointId 动作点ID
|
||||
* @param updates 批量更新数据
|
||||
*/
|
||||
private batchSyncStorageStateToMap(
|
||||
pointId: string,
|
||||
updates: Record<string, { occupied?: boolean; locked?: boolean }>
|
||||
): void {
|
||||
Object.entries(updates).forEach(([locationName, state]) => {
|
||||
this.syncStorageStateToMap(pointId, locationName, {
|
||||
occupied: !!state.occupied,
|
||||
locked: !!state.locked
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取动作点的库位pen对象列表
|
||||
* @param pointId 动作点ID
|
||||
* @param includeMore 是否包含storage-more类型
|
||||
* @returns 库位pen对象列表
|
||||
*/
|
||||
private getStorageLocationPens(pointId: string): MapPen[] {
|
||||
private getStorageLocationPens(pointId: string, includeMore: boolean = true): MapPen[] {
|
||||
if (!this.editor) return [];
|
||||
|
||||
return this.editor.find(`${this.config.penTypes.storageLocation},${this.config.penTypes.storageMore}`).filter(
|
||||
(pen) => pen.tags?.includes(`${this.config.tags.pointPrefix}${pointId}`)
|
||||
const { penTypes, tags } = this.config;
|
||||
const searchTypes = includeMore
|
||||
? `${penTypes.storageLocation},${penTypes.storageMore}`
|
||||
: penTypes.storageLocation;
|
||||
|
||||
return this.editor.find(searchTypes).filter(
|
||||
(pen) => pen.tags?.includes(`${tags.pointPrefix}${pointId}`)
|
||||
);
|
||||
}
|
||||
|
||||
@ -557,10 +606,8 @@ export class StorageLocationService {
|
||||
* @returns 库位pen对象
|
||||
*/
|
||||
private findStorageLocationPen(pointId: string, locationName: string): MapPen | undefined {
|
||||
if (!this.editor) return undefined;
|
||||
|
||||
return this.editor.find(this.config.penTypes.storageLocation).find(
|
||||
(pen) => pen.storageLocation?.pointId === pointId && pen.storageLocation?.locationName === locationName
|
||||
return this.getStorageLocationPens(pointId, false).find(
|
||||
(pen) => pen.storageLocation?.locationName === locationName
|
||||
);
|
||||
}
|
||||
|
||||
@ -571,15 +618,13 @@ export class StorageLocationService {
|
||||
public createAll(): void {
|
||||
if (!this.editor) return;
|
||||
|
||||
const actionPoints = this.editor.find(this.config.penTypes.point).filter(
|
||||
(pen) => pen.point?.type === MapPointType.动作点 && pen.point?.associatedStorageLocations?.length
|
||||
);
|
||||
|
||||
actionPoints.forEach((pen) => {
|
||||
if (pen.id && pen.point?.associatedStorageLocations) {
|
||||
this.create(pen.id, pen.point.associatedStorageLocations);
|
||||
}
|
||||
});
|
||||
this.editor.find(this.config.penTypes.point)
|
||||
.filter(pen => pen.point?.type === MapPointType.动作点 && pen.point?.associatedStorageLocations?.length)
|
||||
.forEach(pen => {
|
||||
if (pen.id && pen.point?.associatedStorageLocations) {
|
||||
this.create(pen.id, pen.point.associatedStorageLocations);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user