From c9033a4c1875584fe19888b438946d7533c504fe Mon Sep 17 00:00:00 2001 From: xudan Date: Thu, 11 Sep 2025 09:44:44 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0BinTaskManagerSer?= =?UTF-8?q?vice=EF=BC=8C=E9=87=8D=E5=91=BD=E5=90=8D=E5=8C=BA=E5=9F=9F?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=EF=BC=8C=E4=BC=98=E5=8C=96=E5=BA=93=E4=BD=8D?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=B8=80=E8=87=B4=E6=80=A7=EF=BC=8C=E8=B0=83?= =?UTF-8?q?=E6=95=B4binTask=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/bintask-manager.service.ts | 223 +++++------------------- 1 file changed, 40 insertions(+), 183 deletions(-) diff --git a/src/services/bintask-manager.service.ts b/src/services/bintask-manager.service.ts index 8bb72ed..7681500 100644 --- a/src/services/bintask-manager.service.ts +++ b/src/services/bintask-manager.service.ts @@ -140,7 +140,7 @@ export class BinTaskManagerService { //#endregion - //#region 数据编辑相关 + //#region 数据转换相关 /** * 解析BinTask数据为可编辑的格式 @@ -178,6 +178,10 @@ export class BinTaskManagerService { }); } + //#endregion + + //#region 工具方法 + /** * 获取值的类型 * @param value 任意值 @@ -237,7 +241,7 @@ export class BinTaskManagerService { //#endregion - //#region 场景更新相关 + //#region 场景数据更新相关 /** * 更新库位的BinTask配置 @@ -246,11 +250,19 @@ export class BinTaskManagerService { * @param binTasks BinTask配置数据 */ public updateBinTask(pointName: string, locationName: string, binTasks: BinTaskItem[]): void { - const binLocationGroups = this.getBinLocationGroups(); - if (binLocationGroups.length === 0) return; + let binLocationGroups = this.getBinLocationGroups(); + + // 如果没有任何库位组,创建一个默认组 + if (binLocationGroups.length === 0) { + binLocationGroups = [{ + binLocationList: [] + }]; + } // 查找目标库位 let targetLocation: any = null; + let targetGroup: any = null; + for (const group of binLocationGroups) { if (group && Array.isArray(group.binLocationList)) { const location = group.binLocationList.find( @@ -258,14 +270,30 @@ export class BinTaskManagerService { ); if (location) { targetLocation = location; + targetGroup = group; break; } } } + // 如果没找到库位,创建一个新的库位条目 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数组存在 @@ -278,14 +306,15 @@ export class BinTaskManagerService { if (!binTaskProperty) { binTaskProperty = { key: 'binTask', - type: 'string', + type: 'json', stringValue: '' }; targetLocation.property.push(binTaskProperty); } - // 更新binTask数据 - binTaskProperty.stringValue = JSON.stringify(binTasks); + // 更新binTask数据 - 使用格式化的JSON字符串,保持与解析时一致的格式 + binTaskProperty.stringValue = JSON.stringify(binTasks, null, 4); + binTaskProperty.type = 'json'; // 更新原始场景数据 this.updateOriginalSceneData(binLocationGroups); @@ -365,7 +394,7 @@ export class BinTaskManagerService { const binLocationGroups = Array.isArray(rawData) ? (rawData as BinLocationGroup[]) - : (rawData as { binLocationsList: BinLocationGroup[] })?.binLocationsList; + : (rawData as BinLocationsList)?.binLocationsList; return Array.isArray(binLocationGroups) ? binLocationGroups : []; } @@ -406,176 +435,4 @@ export class BinTaskManagerService { */ export interface TaskData { operations: Record>; -} - -/** - * 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; - } - } -} +} \ No newline at end of file