import { IncomingInspectOrderDataCollectionDto, SimpleTreeDto } from '../../../qmp-model'; import { Component, OnInit, Input, Output, EventEmitter, TemplateRef } from '@angular/core'; import { RhSafeAny } from 'rh-base/model'; @Component({ selector: 'rh-inspect-group-item-display', templateUrl: './inspect-group-item-display.component.html', styleUrls: ['./inspect-group-item-display.component.less'] }) export class RhInspectGroupItemDisplayComponent implements OnInit { /** 当没有选中任何检验项目,是否默认选中第一个 */ @Input() rhEnableDefaultSelect = true; @Input() rhData!: IncomingInspectOrderDataCollectionDto; /** item位置的模板,默认传参:item数据,group:group数据 */ @Input() rhItemTemplate!: TemplateRef; @Output() rhSelectInpsectItemChange = new EventEmitter(); dataset: SimpleTreeDto[] = []; // /** 当前正在录入数据的检验项目 */ // currentInputInspectItem: IncomingInspectItemDataCollectionDto; // inspectGroupDataset: RhSelectItem[] = []; // currentInspectGroup: string; // /** (第二层模型数据) */ // itemDataCollectionDataset: IncomingInspectItemDataCollectionDto[] = []; // currentGroupCollectionDataset: IncomingInspectGroupDataCollectionDto; // groupCollectionDataset: IncomingInspectGroupDataCollectionDto[]; constructor() { // } ngOnInit(): void { this.initInspectGroupTree(); this.switchNext(); } onInspectGroupChange(node: SimpleTreeDto) { node.expand = !node.expand; this.dataset = [...this.dataset]; // this.currentInspectGroup = value; // this.currentGroupCollectionDataset = this.groupCollectionDataset.find((ele) => ele.InspectItemGroupCode === value); // if (this.currentGroupCollectionDataset) { // this.itemDataCollectionDataset = this.currentGroupCollectionDataset.ItemDataCollectionDtos || []; // if (this.itemDataCollectionDataset?.length) { // this.selectInspectItemChange(this.itemDataCollectionDataset[0]); // } // } } onSelectInspectItemChange(node: SimpleTreeDto) { node.select = true; if (node?.parent && node?.parent?.expand !== true) { node.parent.expand = true; } this.rhSelectInpsectItemChange.emit(node); this.dataset.forEach((group) => { group.children?.forEach((item) => { item.select = item === node; }); }); // this.currentInputInspectItem = dto; // this.currentInputInspectItem.select = true; // // 切换检验项目时要更新产品数据信息 // this.editInspectDataset = this.currentInputInspectItem.ItemDataValueDtos; // this.initEditInspectDatas(this.editInspectDataset); } initInspectGroupTree() { if (!this.rhData?.GroupDataCollectionDtos?.length) { return; } this.rhData.GroupDataCollectionDtos.forEach((groupData, groupIndex, groupDataset) => { const groupNode = new SimpleTreeDto( groupData.InspectItemGroupName, groupData.InspectItemGroupCode, 'folder', false, false, groupData, [], 'folder-open', void 0, 0 ); groupNode.location = [{ index: groupIndex, length: groupDataset?.length }]; if (!groupData?.ItemDataCollectionDtos?.length) { return; } groupData.ItemDataCollectionDtos.forEach((inspectItem, inspectIndex, inspectDataset) => { const inspectNode = new SimpleTreeDto( inspectItem.InspectItemName, inspectItem.InspectOrderEntryCode, 'code', true, false, inspectItem, [], 'code', groupNode, 1 ); inspectNode.location = [...groupNode.location, { index: inspectIndex, length: inspectDataset?.length }]; groupNode.children?.push(inspectNode); }); this.dataset.push(groupNode); }); } // 自动跳转到下一个检验项 switchNext() { const target = this.findSelectItem(); // 当没有任何选中项目默认选中第一个组的第一个项目 if (!target) { this.dataset[0].children[0].select = true; this.onSelectInspectItemChange(this.dataset[0].children[0]); } else { const next = this.findNextSelectItem(target); this.onSelectInspectItemChange(next); } } private findSelectItem(): SimpleTreeDto | null { if (!this.dataset?.length) { return null; } let targetItem: SimpleTreeDto | null = null; this.dataset.some((group) => { if (!group.children?.length) { return false; } const index = group.children.findIndex((ele) => ele.select); if (index !== -1) { targetItem = group.children[index]; return true; } else { return false; } }); return targetItem; } private findNextSelectItem(node: SimpleTreeDto): SimpleTreeDto { const currentLocation = node.location; // 当组内的项目没有到达最后一个,选中下一个项目 if (currentLocation[1].index < currentLocation[1].length - 1) { return this.dataset[currentLocation[0].index].children[currentLocation[1].index + 1]; } else { // 当组内的项目已经是最后一个,选中下一个组的第一个 return this.dataset[(currentLocation[0].index + 1) % currentLocation[0].length].children[0]; } } }