refactor: 更新BinTaskManagerService,重命名区域注释,优化库位处理逻辑,确保数据一致性,调整binTask数据格式化方式

This commit is contained in:
xudan 2025-09-11 09:44:44 +08:00
parent c41263d39f
commit c9033a4c18

View File

@ -140,7 +140,7 @@ export class BinTaskManagerService {
//#endregion //#endregion
//#region 数据编辑相关 //#region 数据转换相关
/** /**
* BinTask数据为可编辑的格式 * BinTask数据为可编辑的格式
@ -178,6 +178,10 @@ export class BinTaskManagerService {
}); });
} }
//#endregion
//#region 工具方法
/** /**
* *
* @param value * @param value
@ -237,7 +241,7 @@ export class BinTaskManagerService {
//#endregion //#endregion
//#region 场景更新相关 //#region 场景数据更新相关
/** /**
* BinTask配置 * BinTask配置
@ -246,11 +250,19 @@ export class BinTaskManagerService {
* @param binTasks BinTask配置数据 * @param binTasks BinTask配置数据
*/ */
public updateBinTask(pointName: string, locationName: string, binTasks: BinTaskItem[]): void { public updateBinTask(pointName: string, locationName: string, binTasks: BinTaskItem[]): void {
const binLocationGroups = this.getBinLocationGroups(); let binLocationGroups = this.getBinLocationGroups();
if (binLocationGroups.length === 0) return;
// 如果没有任何库位组,创建一个默认组
if (binLocationGroups.length === 0) {
binLocationGroups = [{
binLocationList: []
}];
}
// 查找目标库位 // 查找目标库位
let targetLocation: any = null; let targetLocation: any = null;
let targetGroup: any = null;
for (const group of binLocationGroups) { for (const group of binLocationGroups) {
if (group && Array.isArray(group.binLocationList)) { if (group && Array.isArray(group.binLocationList)) {
const location = group.binLocationList.find( const location = group.binLocationList.find(
@ -258,14 +270,30 @@ export class BinTaskManagerService {
); );
if (location) { if (location) {
targetLocation = location; targetLocation = location;
targetGroup = group;
break; break;
} }
} }
} }
// 如果没找到库位,创建一个新的库位条目
if (!targetLocation) { if (!targetLocation) {
console.warn(`未找到库位: ${pointName} - ${locationName}`); // 使用第一个组,如果不存在则创建
return; if (!targetGroup) {
targetGroup = binLocationGroups[0];
if (!targetGroup.binLocationList) {
targetGroup.binLocationList = [];
}
}
// 创建新的库位条目
targetLocation = {
pointName: pointName,
instanceName: locationName,
property: []
};
targetGroup.binLocationList.push(targetLocation);
} }
// 确保property数组存在 // 确保property数组存在
@ -278,14 +306,15 @@ export class BinTaskManagerService {
if (!binTaskProperty) { if (!binTaskProperty) {
binTaskProperty = { binTaskProperty = {
key: 'binTask', key: 'binTask',
type: 'string', type: 'json',
stringValue: '' stringValue: ''
}; };
targetLocation.property.push(binTaskProperty); targetLocation.property.push(binTaskProperty);
} }
// 更新binTask数据 // 更新binTask数据 - 使用格式化的JSON字符串保持与解析时一致的格式
binTaskProperty.stringValue = JSON.stringify(binTasks); binTaskProperty.stringValue = JSON.stringify(binTasks, null, 4);
binTaskProperty.type = 'json';
// 更新原始场景数据 // 更新原始场景数据
this.updateOriginalSceneData(binLocationGroups); this.updateOriginalSceneData(binLocationGroups);
@ -365,7 +394,7 @@ export class BinTaskManagerService {
const binLocationGroups = Array.isArray(rawData) const binLocationGroups = Array.isArray(rawData)
? (rawData as BinLocationGroup[]) ? (rawData as BinLocationGroup[])
: (rawData as { binLocationsList: BinLocationGroup[] })?.binLocationsList; : (rawData as BinLocationsList)?.binLocationsList;
return Array.isArray(binLocationGroups) ? binLocationGroups : []; return Array.isArray(binLocationGroups) ? binLocationGroups : [];
} }
@ -406,176 +435,4 @@ export class BinTaskManagerService {
*/ */
export interface TaskData { export interface TaskData {
operations: Record<string, Record<string, any>>; operations: Record<string, Record<string, any>>;
} }
/**
* BinTask编辑器操作接口
*/
export interface BinTaskEditorOperations {
// 任务操作
addTask: () => void;
removeTask: (taskIndex: number) => void;
// 操作操作
addOperation: (taskIndex: number) => void;
removeOperation: (taskIndex: number, opKey: string) => void;
updateOperationKey: (taskIndex: number, oldKey: string, newKey: string) => void;
// 属性操作
addProperty: (taskIndex: number, opKey: string) => void;
removeProperty: (taskIndex: number, opKey: string, propKey: string) => void;
updatePropertyKey: (taskIndex: number, opKey: string, oldPropKey: string, newPropKey: string) => void;
updatePropertyValue: (taskIndex: number, opKey: string, propKey: string, value: any) => void;
changeValueType: (taskIndex: number, opKey: string, propKey: string, newType: string) => void;
}
/**
* BinTask编辑器状态管理
*/
export class BinTaskEditorState {
private taskList: TaskData[] = [];
private operations: BinTaskEditorOperations;
private manager: BinTaskManagerService;
constructor(operations: BinTaskEditorOperations, manager?: BinTaskManagerService) {
this.operations = operations;
this.manager = manager || new BinTaskManagerService({} as any); // 提供默认值
}
/**
*
*/
public getTaskList(): TaskData[] {
return this.taskList;
}
/**
*
*/
public setTaskList(taskList: TaskData[]): void {
this.taskList = taskList;
}
/**
*
*/
public loadInitialData(initialData: BinTaskItem[]): void {
this.taskList = this.manager.parseBinTaskData(initialData);
}
/**
*
*/
public getSaveData(): BinTaskItem[] {
return this.manager.convertToBinTaskData(this.taskList);
}
/**
*
*/
public addTask(): void {
this.taskList.push({ operations: {} });
}
/**
*
*/
public removeTask(taskIndex: number): void {
this.taskList.splice(taskIndex, 1);
}
/**
*
*/
public addOperation(taskIndex: number): void {
const task = this.taskList[taskIndex];
if (task) {
const newOpKey = this.manager.generateUniqueKey('operation');
task.operations[newOpKey] = this.manager.createNewOperation();
}
}
/**
*
*/
public removeOperation(taskIndex: number, opKey: string): void {
const task = this.taskList[taskIndex];
if (task && task.operations[opKey]) {
delete task.operations[opKey];
}
}
/**
*
*/
public updateOperationKey(taskIndex: number, oldKey: string, newKey: string): void {
const task = this.taskList[taskIndex];
if (task && task.operations[oldKey] && newKey && newKey !== oldKey) {
const operation = task.operations[oldKey];
delete task.operations[oldKey];
task.operations[newKey] = operation;
}
}
/**
*
*/
public addProperty(taskIndex: number, opKey: string): void {
const task = this.taskList[taskIndex];
if (task && task.operations[opKey]) {
const newPropKey = this.manager.generateUniqueKey('property');
task.operations[opKey][newPropKey] = this.manager.createNewProperty();
}
}
/**
*
*/
public removeProperty(taskIndex: number, opKey: string, propKey: string): void {
const task = this.taskList[taskIndex];
if (task && task.operations[opKey] && task.operations[opKey][propKey] !== undefined) {
delete task.operations[opKey][propKey];
}
}
/**
*
*/
public updatePropertyKey(
taskIndex: number,
opKey: string,
oldPropKey: string,
newPropKey: string
): void {
const task = this.taskList[taskIndex];
if (task && task.operations[opKey] &&
task.operations[opKey][oldPropKey] !== undefined &&
newPropKey && newPropKey !== oldPropKey) {
const value = task.operations[opKey][oldPropKey];
delete task.operations[opKey][oldPropKey];
task.operations[opKey][newPropKey] = value;
}
}
/**
*
*/
public updatePropertyValue(taskIndex: number, opKey: string, propKey: string, value: any): void {
const task = this.taskList[taskIndex];
if (task && task.operations[opKey]) {
task.operations[opKey][propKey] = value;
}
}
/**
*
*/
public changeValueType(taskIndex: number, opKey: string, propKey: string, newType: string): void {
const task = this.taskList[taskIndex];
if (task && task.operations[opKey] && task.operations[opKey][propKey] !== undefined) {
const currentValue = task.operations[opKey][propKey];
const newValue = this.manager.convertValueType(currentValue, newType);
task.operations[opKey][propKey] = newValue;
}
}
}