refactor: 重构point-detail-card组件,优化库位任务数据处理逻辑,整合BinTaskManagerService,提升代码可读性和维护性
This commit is contained in:
parent
c9033a4c18
commit
7863867a5c
@ -1,12 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { MapAreaType, type MapPen, type MapPointInfo, MapPointType, type Rect } from '@api/map';
|
||||
import type {
|
||||
BinLocationGroup,
|
||||
BinLocationsList,
|
||||
BinTaskItem,
|
||||
BinTaskOperation,
|
||||
StorageLocationInfo,
|
||||
} from '@api/scene';
|
||||
import type { StorageLocationInfo } from '@api/scene';
|
||||
import { BinTaskManagerService } from '@core/bintask-manager.service';
|
||||
import type { EditorService } from '@core/editor.service';
|
||||
import sTheme from '@core/theme.service';
|
||||
import { isNil } from 'lodash-es';
|
||||
@ -19,6 +14,7 @@ type Props = {
|
||||
};
|
||||
const props = defineProps<Props>();
|
||||
const editor = inject(props.token)!;
|
||||
const binTaskManager = new BinTaskManagerService(editor.value);
|
||||
|
||||
const pen = computed<MapPen | undefined>(() => editor.value.getPenById(props.current));
|
||||
const point = computed<MapPointInfo | null>(() => {
|
||||
@ -62,8 +58,8 @@ const coArea1 = computed<string>(() => mapAreas(MapAreaType.库区));
|
||||
const coArea2 = computed<string>(() => mapAreas(MapAreaType.互斥区));
|
||||
|
||||
// 库位状态标签样式映射
|
||||
const getStorageStatusTag = (location: StorageLocationInfo) => {
|
||||
const tags = [];
|
||||
const getStorageStatusTag = (location: StorageLocationInfo): Array<{ text: string; color: string }> => {
|
||||
const tags: Array<{ text: string; color: string }> = [];
|
||||
|
||||
if (location.is_occupied) {
|
||||
tags.push({ text: '已占用', color: 'error' });
|
||||
@ -92,83 +88,14 @@ const getStorageStatusTag = (location: StorageLocationInfo) => {
|
||||
return tags;
|
||||
};
|
||||
|
||||
// 解析库位任务数据
|
||||
const parseBinTask = (binTaskString: string): BinTaskItem[] => {
|
||||
try {
|
||||
return JSON.parse(binTaskString.replace(/\n/g, '').trim());
|
||||
} catch (error) {
|
||||
console.error('解析库位任务数据失败:', error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
// 获取任务中的所有操作
|
||||
const getTaskOperations = (task: BinTaskItem) => {
|
||||
if (!task || typeof task !== 'object') return {};
|
||||
|
||||
const operations: Record<string, BinTaskOperation> = {};
|
||||
|
||||
// 遍历任务对象的所有属性,查找操作对象
|
||||
for (const [key, value] of Object.entries(task)) {
|
||||
if (value && typeof value === 'object' && 'operation' in value) {
|
||||
operations[key] = value as BinTaskOperation;
|
||||
}
|
||||
}
|
||||
|
||||
return operations;
|
||||
};
|
||||
|
||||
// 获取当前动作点对应的库位任务数据
|
||||
const binTaskData = computed(() => {
|
||||
try {
|
||||
const rawData = editor.value.getBinLocationsList();
|
||||
if (!point.value) return [];
|
||||
|
||||
const currentPointName = pen.value?.label || pen.value?.id;
|
||||
if (!currentPointName) return [];
|
||||
|
||||
if (!rawData || !point.value) return [];
|
||||
|
||||
const currentPointName = pen.value?.label || pen.value?.id;
|
||||
if (!currentPointName) return [];
|
||||
|
||||
// 获取库位组数据
|
||||
const binLocationGroups = Array.isArray(rawData)
|
||||
? (rawData as BinLocationGroup[])
|
||||
: (rawData as BinLocationsList)?.binLocationsList;
|
||||
|
||||
if (!binLocationGroups || !Array.isArray(binLocationGroups)) return [];
|
||||
|
||||
const allBinLocations = binLocationGroups
|
||||
.filter((group) => group && Array.isArray(group.binLocationList))
|
||||
.flatMap((group) => group.binLocationList)
|
||||
.filter((item) => item && typeof item === 'object');
|
||||
|
||||
return allBinLocations
|
||||
.filter((item) => item.pointName === currentPointName)
|
||||
.map((item) => {
|
||||
try {
|
||||
if (!item.property || !Array.isArray(item.property)) {
|
||||
return {
|
||||
instanceName: item.instanceName || '未知库位',
|
||||
binTasks: [],
|
||||
};
|
||||
}
|
||||
|
||||
const binTaskProperty = item.property.find((prop) => prop && prop.key === 'binTask');
|
||||
return {
|
||||
instanceName: item.instanceName || '未知库位',
|
||||
binTasks: binTaskProperty && binTaskProperty.stringValue ? parseBinTask(binTaskProperty.stringValue) : [],
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('处理库位任务数据失败:', error, item);
|
||||
return {
|
||||
instanceName: item.instanceName || '未知库位',
|
||||
binTasks: [],
|
||||
};
|
||||
}
|
||||
})
|
||||
.filter((item) => item.binTasks.length > 0);
|
||||
} catch (error) {
|
||||
console.error('获取库位任务数据失败:', error);
|
||||
return [];
|
||||
}
|
||||
return binTaskManager.getPointBinTaskDataForDisplay(currentPointName);
|
||||
});
|
||||
</script>
|
||||
|
||||
@ -290,7 +217,7 @@ const binTaskData = computed(() => {
|
||||
<div class="bin-tasks">
|
||||
<div v-for="(task, taskIndex) in binLocation.binTasks" :key="taskIndex" class="bin-task-item">
|
||||
<div
|
||||
v-for="(operation, operationType) in getTaskOperations(task)"
|
||||
v-for="(operation, operationType) in binTaskManager.getTaskOperations(task)"
|
||||
:key="operationType"
|
||||
class="task-operation"
|
||||
>
|
||||
|
@ -23,44 +23,15 @@ export class BinTaskManagerService {
|
||||
public getBinTaskData(pointName: string, locationName: string): BinTaskItem[] {
|
||||
if (!pointName || !locationName) return [];
|
||||
|
||||
try {
|
||||
const rawData = this.editor.getBinLocationsList();
|
||||
if (!rawData) return [];
|
||||
const targetLocation = this.findBinLocation(pointName, locationName);
|
||||
if (!targetLocation) return [];
|
||||
|
||||
const binLocationGroups = Array.isArray(rawData)
|
||||
? (rawData as BinLocationGroup[])
|
||||
: (rawData as BinLocationsList)?.binLocationsList;
|
||||
|
||||
if (!binLocationGroups || !Array.isArray(binLocationGroups)) return [];
|
||||
|
||||
const allBinLocations = binLocationGroups
|
||||
.filter((group) => group && Array.isArray(group.binLocationList))
|
||||
.flatMap((group) => group.binLocationList)
|
||||
.filter((item) => item && typeof item === 'object');
|
||||
|
||||
const targetLocation = allBinLocations.find(
|
||||
(item) => item.pointName === pointName && item.instanceName === locationName
|
||||
);
|
||||
|
||||
if (!targetLocation || !targetLocation.property || !Array.isArray(targetLocation.property)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const binTaskProperty = targetLocation.property.find((prop) => prop && prop.key === 'binTask');
|
||||
if (!binTaskProperty || !binTaskProperty.stringValue) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(binTaskProperty.stringValue.replace(/\n/g, '').trim());
|
||||
} catch (error) {
|
||||
console.error('解析BinTask数据失败:', error);
|
||||
return [];
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取BinTask数据失败:', error);
|
||||
const binTaskProperty = targetLocation.property?.find((prop) => prop && prop.key === 'binTask');
|
||||
if (!binTaskProperty || !binTaskProperty.stringValue) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return this.parseBinTask(binTaskProperty.stringValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,49 +42,19 @@ export class BinTaskManagerService {
|
||||
public getPointBinTaskData(pointName: string): Record<string, BinTaskItem[]> {
|
||||
if (!pointName) return {};
|
||||
|
||||
try {
|
||||
const rawData = this.editor.getBinLocationsList();
|
||||
if (!rawData) return {};
|
||||
const pointLocations = this.getPointBinLocations(pointName);
|
||||
const result: Record<string, BinTaskItem[]> = {};
|
||||
|
||||
const binLocationGroups = Array.isArray(rawData)
|
||||
? (rawData as BinLocationGroup[])
|
||||
: (rawData as BinLocationsList)?.binLocationsList;
|
||||
pointLocations.forEach((location) => {
|
||||
if (location.instanceName) {
|
||||
const binTaskProperty = location.property?.find((prop) => prop && prop.key === 'binTask');
|
||||
result[location.instanceName] = binTaskProperty && binTaskProperty.stringValue
|
||||
? this.parseBinTask(binTaskProperty.stringValue)
|
||||
: [];
|
||||
}
|
||||
});
|
||||
|
||||
if (!binLocationGroups || !Array.isArray(binLocationGroups)) return {};
|
||||
|
||||
const allBinLocations = binLocationGroups
|
||||
.filter((group) => group && Array.isArray(group.binLocationList))
|
||||
.flatMap((group) => group.binLocationList)
|
||||
.filter((item) => item && typeof item === 'object');
|
||||
|
||||
const pointLocations = allBinLocations.filter(
|
||||
(item) => item.pointName === pointName
|
||||
);
|
||||
|
||||
const result: Record<string, BinTaskItem[]> = {};
|
||||
|
||||
pointLocations.forEach((location) => {
|
||||
if (location.instanceName && location.property && Array.isArray(location.property)) {
|
||||
const binTaskProperty = location.property.find((prop) => prop && prop.key === 'binTask');
|
||||
if (binTaskProperty && binTaskProperty.stringValue) {
|
||||
try {
|
||||
const binTasks = JSON.parse(binTaskProperty.stringValue.replace(/\n/g, '').trim());
|
||||
result[location.instanceName] = binTasks;
|
||||
} catch (error) {
|
||||
console.error('解析库位BinTask数据失败:', error, location);
|
||||
result[location.instanceName] = [];
|
||||
}
|
||||
} else {
|
||||
result[location.instanceName] = [];
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('获取动作点BinTask数据失败:', error);
|
||||
return {};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -138,6 +79,66 @@ export class BinTaskManagerService {
|
||||
return binTaskData.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析库位任务数据
|
||||
* @param binTaskString 库位任务字符串
|
||||
* @returns 解析后的任务数据
|
||||
*/
|
||||
public parseBinTask(binTaskString: string): BinTaskItem[] {
|
||||
try {
|
||||
return JSON.parse(binTaskString.replace(/\n/g, '').trim());
|
||||
} catch (error) {
|
||||
console.error('解析库位任务数据失败:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取任务中的所有操作
|
||||
* @param task 任务对象
|
||||
* @returns 操作对象映射
|
||||
*/
|
||||
public getTaskOperations(task: BinTaskItem): Record<string, any> {
|
||||
if (!task || typeof task !== 'object') return {};
|
||||
|
||||
const operations: Record<string, any> = {};
|
||||
|
||||
// 遍历任务对象的所有属性,查找操作对象
|
||||
for (const [key, value] of Object.entries(task)) {
|
||||
if (value && typeof value === 'object' && 'operation' in value) {
|
||||
operations[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前动作点对应的库位任务数据(用于显示)
|
||||
* @param pointName 动作点名称
|
||||
* @returns 库位任务数据列表
|
||||
*/
|
||||
public getPointBinTaskDataForDisplay(pointName: string): Array<{
|
||||
instanceName: string;
|
||||
binTasks: BinTaskItem[];
|
||||
}> {
|
||||
if (!pointName) return [];
|
||||
|
||||
const pointLocations = this.getPointBinLocations(pointName);
|
||||
|
||||
return pointLocations
|
||||
.map((item) => {
|
||||
const binTaskProperty = item.property?.find((prop) => prop && prop.key === 'binTask');
|
||||
return {
|
||||
instanceName: item.instanceName || '未知库位',
|
||||
binTasks: binTaskProperty && binTaskProperty.stringValue
|
||||
? this.parseBinTask(binTaskProperty.stringValue)
|
||||
: [],
|
||||
};
|
||||
})
|
||||
.filter((item) => item.binTasks.length > 0);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 数据转换相关
|
||||
@ -399,6 +400,41 @@ export class BinTaskManagerService {
|
||||
return Array.isArray(binLocationGroups) ? binLocationGroups : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有库位数据
|
||||
* @returns 所有库位数据数组
|
||||
*/
|
||||
private getAllBinLocations(): any[] {
|
||||
const binLocationGroups = this.getBinLocationGroups();
|
||||
return binLocationGroups
|
||||
.filter((group) => group && Array.isArray(group.binLocationList))
|
||||
.flatMap((group) => group.binLocationList)
|
||||
.filter((item) => item && typeof item === 'object');
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定动作点和库位的库位数据
|
||||
* @param pointName 动作点名称
|
||||
* @param locationName 库位名称
|
||||
* @returns 库位数据,如果未找到则返回null
|
||||
*/
|
||||
private findBinLocation(pointName: string, locationName: string): any | null {
|
||||
const allBinLocations = this.getAllBinLocations();
|
||||
return allBinLocations.find(
|
||||
(item) => item.pointName === pointName && item.instanceName === locationName
|
||||
) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定动作点的所有库位数据
|
||||
* @param pointName 动作点名称
|
||||
* @returns 库位数据数组
|
||||
*/
|
||||
private getPointBinLocations(pointName: string): any[] {
|
||||
const allBinLocations = this.getAllBinLocations();
|
||||
return allBinLocations.filter((item) => item.pointName === pointName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查库位项是否存在
|
||||
* @param pointName 动作点名称
|
||||
|
Loading…
x
Reference in New Issue
Block a user