import { OrganizationStructureTreeNode, OrganizationStructureInput, FactorynfoQueryDto, FactoryInfoDto } from './../../mdp-model'; import { RhSelectItem, RhPortType } from 'rh-base/model'; import { Component, OnInit } from '@angular/core'; import { NzTreeNode, NzTreeService } from 'ng-zorro-antd/tree'; import { MsgHelper, FormHelper, ObjectHelper, RhStorageService } from 'rh-base/core'; import { OpMode } from 'rh-base/model'; import { UntypedFormGroup, UntypedFormBuilder, Validators } from '@angular/forms'; import { RhFactoryLayoutService } from '../factory-layout.service'; import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop'; import { NzTreeHelper } from '../../shared/utils/tree-helper'; const FACTORY_ORGANIGATION_TYPE = 'Factory'; // 组织架构(部门信息) @Component({ selector: 'rh-organization-structure-setting', templateUrl: './organization-structure-setting.component.html', styleUrls: ['./organization-structure-setting.component.less'] }) export class RhOrganizationStructureSettingComponent implements OnInit { // 根据用户获取对应工厂信息 factoryCode: string | null = null; factoryName: string | null = null; treeName = '组织架构'; // 节点处理 dataSource: OrganizationStructureTreeNode[] = []; childNodesData: OrganizationStructureTreeNode[] = []; dataLoading = false; nodeDatas: NzTreeNode[] = []; expandedKeys: string[] = []; selectedKeys: string[] = []; selectedData: OrganizationStructureTreeNode | null = null; selectedNode: NzTreeNode | null = null; // 表单处理 editForm!: UntypedFormGroup; editDto!: OrganizationStructureInput; submitLoading = false; statusChanging = false; // 节点排序 isShowSortModal = false; // 禁用节点显示 isShowDisabledNode = true; // 增加工厂 isShowFactoryInfoModal = false; selectedFactoryData!: FactoryInfoDto; // 辅助数据 origanizationTypes: RhSelectItem[] = []; // isOrganizationTypeDisable = false; isShowExportModal = false; // /** 组织架构根节点 */ // rootNodeData: RhSafeAny; MdpApiPort = RhPortType.mdp; constructor( private operator: RhFactoryLayoutService, private treeSer: NzTreeService, private fb: UntypedFormBuilder, private storage: RhStorageService ) {} ngOnInit(): void { this.factoryCode = this.storage.getUserSession()?.User?.FactoryCode as string; this.getDatas(); this.editDto = this.initEditDto(); this.createEditForm(this.editDto); this.getOrganizationTypes(); this.getFactoryName(); } // *****************************获取树结构************************************* /** 获取组织架构树数据 */ getDatas(isEnable?: string | null) { this.dataLoading = true; this.operator.GetOrganizationStructureTree(this.factoryCode as string, isEnable || null).subscribe( (res) => { if (res.Success) { this.dataSource = res.Attach; this.getNodeDatas(); this.selectedNode = null; this.selectedData = null; } else { this.dataSource = []; this.nodeDatas = []; } this.dataLoading = false; }, () => { this.dataLoading = false; } ); } /** 获取节点列表 */ getNodeDatas() { this.nodeDatas = NzTreeHelper.transformToNodes(this.dataSource, { title: 'OrganizationStructureName', key: 'OrganizationStructureCode', children: 'OrganizationStructureTreeNodes' }); // 刷新数据后,重新选中之前选中的节点 if (this.selectedNode) { const tempNodes = NzTreeHelper.getSelectedNodes(this.nodeDatas, [this.selectedNode.key]); if (tempNodes.length) { this.selectedNode = tempNodes[0]; this.selectedNode.isSelected = true; this.selectedKeys = [this.selectedNode.key]; this.expandedKeys = NzTreeHelper.getAllParentNodes(this.selectedNode).map((f) => f.key); } else { this.selectedNode = null; this.selectedKeys = []; this.expandedKeys = []; } this.selectTreeNode(this.selectedNode as NzTreeNode); } } // ****************************表单处理******************************** /** 存储组织架构数据 */ storeDatas(dto: OrganizationStructureInput) { this.submitLoading = true; this.operator.StoreOrganizationStructureData(dto).subscribe( (res) => { if (res.Success) { this.selectedNode = new NzTreeNode({ key: res.Attach?.['OrganizationStructureCode'], title: res.Attach?.['OrganizationStructureName'], customData: res.Attach }); const msg = this.editDto.OpSign === OpMode.OpAdd ? '新增' : '编辑'; MsgHelper.ShowSuccessMessage(`${msg}数据成功!`); this.getDatas(); this.resetEditForm(null); this.enableIsEnableFormControl(); this.enableOrganizationTypeControl(); } else { MsgHelper.ShowWarningModal(`存储数据失败!${res.Message}`); } this.submitLoading = false; }, () => { this.submitLoading = false; } ); } initEditDto() { const dto = OrganizationStructureInput.create(); dto.FactoryCode = this.factoryCode; dto.IsEnable = true; dto.OpSign = OpMode.OpAdd; return dto; } createEditForm(dto: OrganizationStructureInput) { this.editForm = this.fb.group({ /** 父节点部门名称 */ ParentOrganizationStructureName: [null], /** 部门代码 */ OrganizationStructureCode: [null], /** 部门名称 */ OrganizationStructureName: [dto.OrganizationStructureName, [Validators.required]], /** 部门描述 */ OrganizationStructureDisplayName: [dto.OrganizationStructureDisplayName, [Validators.required]], /** 部门类型 */ OrganizationType: [dto.OrganizationType, [Validators.required]], /** 组织机构外部编码 */ OrganizationStructureExternalCode: [dto.OrganizationStructureExternalCode], /** 是否启用 */ IsEnable: [dto.IsEnable, [Validators.required]], /** 排序 */ SortNum: [dto.SortNum, [Validators.required]], /** 备注 */ Remark: [dto.Remark], /** 工厂编码 */ FactoryCode: [dto.FactoryCode] }); } submitEditForm($event: Event) { if ($event) { $event.preventDefault(); } FormHelper.YGSubmitForm(this.editDto, this.editForm, (dto) => { this.storeDatas(dto as OrganizationStructureInput); }); } resetEditForm($event: Event | null) { if ($event) { $event.preventDefault(); } this.editDto = this.initEditDto(); this.editForm.reset(this.editDto); } enableIsEnableFormControl() { this.editForm?.get('IsEnable')?.enable(); } disableIsEnableFormControl() { this.editForm?.get('IsEnable')?.disable(); } enableOrganizationTypeControl() { this.editForm?.get('OrganizationType')?.enable(); } disableOrganizationTypeControl() { this.editForm?.get('OrganizationType')?.disable(); } // ************************************操作************************************ /** 选中节点 */ selectTreeNode(eventnode: NzTreeNode) { this.editDto = this.initEditDto(); if (eventnode && eventnode.isSelected) { this.selectedNode = eventnode; this.selectedData = eventnode.origin['customData']; ObjectHelper.MapT(this.selectedData as OrganizationStructureTreeNode, this.editDto); this.editDto.OpSign = OpMode.OpEdit; this.editForm.reset(this.editDto); if ([FACTORY_ORGANIGATION_TYPE, 'WorkShop', 'ProductShop'].includes(this.editDto.OrganizationType as string)) { this.disableOrganizationTypeControl(); } else { this.enableOrganizationTypeControl(); } this.disableIsEnableFormControl(); } else { this.selectedNode = null; this.selectedData = null; this.editForm.reset(this.editDto); this.enableIsEnableFormControl(); this.enableOrganizationTypeControl(); } } /** 排序子节点 */ sortNodes() { if (!this.isAllowSort()) { MsgHelper.ShowWarningMessage('不存在子节点数据!'); return; } this.childNodesData = this.selectedData?.OrganizationStructureTreeNodes as OrganizationStructureTreeNode[]; this.openSortModal(); } openSortModal() { this.isShowSortModal = true; } closeSortModal() { this.childNodesData = []; this.isShowSortModal = false; } drop(event: CdkDragDrop): void { moveItemInArray(this.childNodesData, event.previousIndex, event.currentIndex); this.childNodesData = [...this.childNodesData]; } sortChildNodes() { const targetNodes: OrganizationStructureInput[] = []; this.childNodesData.forEach((ele, index) => { const temp = OrganizationStructureInput.create(); ObjectHelper.MapT(ele, temp); temp.ParentOrganizationStructureCode = this.selectedData?.OrganizationStructureCode; temp.SortNum = index + 1; targetNodes.push(temp); }); this.operator.UpdateOrganizationStructureSortNumDatas(targetNodes).subscribe((res) => { if (res.Success) { MsgHelper.ShowSuccessMessage('保存排序成功!'); this.getDatas(); this.closeSortModal(); } else { MsgHelper.ShowWarningMessage('保存排序失败!'); } }); } /** 增加节点(同级别) */ addTreeNode() { this.editDto = this.initEditDto(); this.editDto.ParentOrganizationStructureCode = this.selectedNode?.parentNode?.origin['customData'].OrganizationStructureCode; this.editForm.reset(this.editDto); this.editForm .get('ParentOrganizationStructureName') ?.setValue(this.selectedNode?.parentNode?.origin['customData'].OrganizationStructureName); this.enableIsEnableFormControl(); this.enableOrganizationTypeControl(); } /** 增加子节点 */ addChildTreeNode() { this.editDto = this.initEditDto(); this.editDto.ParentOrganizationStructureCode = this.selectedData?.OrganizationStructureCode; this.editForm.reset(this.editDto); this.editForm.get('ParentOrganizationStructureName')?.setValue(this.selectedData?.OrganizationStructureName); this.enableIsEnableFormControl(); this.enableOrganizationTypeControl(); } /** 启用节点 */ enableOrganization() { MsgHelper.ShowConfirmModal('是否确认启用该组织架构', '', () => { this.statusChanging = true; this.operator.EnableOrganizationStructure(this.editDto).subscribe( (res) => { if (res.Success) { MsgHelper.ShowSuccessMessage('启用成功!'); this.getDatas(); } else { MsgHelper.ShowWarningMessage(`启用失败!${res.Message}`); } this.statusChanging = false; }, () => { this.statusChanging = false; } ); }); } /** 禁用节点 */ disableOrganization() { MsgHelper.ShowConfirmModal('是否确认停用该组织架构', '', () => { this.statusChanging = true; this.operator.DisableOrganizationStructure(this.editDto).subscribe( (res) => { if (res.Success) { MsgHelper.ShowSuccessMessage('禁用成功!'); this.getDatas(); } else { MsgHelper.ShowWarningMessage(`禁用成功!${res.Message}`); } this.statusChanging = false; }, () => { this.statusChanging = false; } ); }); } /** 切换禁用节点显示状态 */ changeDisabledNodeShowStatus() { this.isShowDisabledNode = !this.isShowDisabledNode; if (this.isShowDisabledNode) { this.getDatas(); } else { this.getDatas('true'); } } // *************************增加工厂*********************************** openFactoryInfoModal() { this.isShowFactoryInfoModal = true; } closeFactoryInfoModal() { this.isShowFactoryInfoModal = false; } onSelectFactoryData(datas: FactoryInfoDto[] | FactoryInfoDto) { if (Array.isArray(datas) && datas.length) { this.selectedFactoryData = datas[0]; } else { this.selectedFactoryData = datas as FactoryInfoDto; } } addNewFactoryNode() { const node = this.nodeDatas.find((f) => f.origin['customData'].FactoryCode === this.selectedFactoryData.FactoryCode); if (node) { MsgHelper.ShowWarningMessage('该工厂信息已存在,请勿重复添加!'); } else { this.editDto = this.initEditDto(); // 组织架构代码会被后端覆盖赋值,用于禁用保存按钮 this.editDto.OrganizationStructureCode = this.selectedFactoryData.FactoryCode; this.editDto.OrganizationStructureName = this.selectedFactoryData.FactoryName; this.editDto.OrganizationStructureDisplayName = this.selectedFactoryData.FactoryFullName; this.editDto.FactoryCode = this.selectedFactoryData.FactoryCode; this.editDto.OrganizationType = FACTORY_ORGANIGATION_TYPE; this.editForm?.get('OrganizationType')?.disable(); this.editForm.reset(this.editDto); this.enableIsEnableFormControl(); this.closeFactoryInfoModal(); } } // *************************辅助数据*********************************** getOrganizationTypes() { this.operator.GetOrganizationStructureTypes().subscribe((res) => { if (res.Success) { this.origanizationTypes = res.Attach; } }); } getFactoryName() { const queryDto = FactorynfoQueryDto.create(); queryDto.FactoryCode = this.factoryCode; this.operator.GetFactoryInfoDatas(queryDto).subscribe((res) => { if (res.Success) { if (res.Attach && res.Attach.length) { this.factoryName = res.Attach[0].FactoryName as string; } if (this.factoryName) { this.treeName = this.treeName + '(' + this.factoryName + ')'; } } }); } // *************************页面显示逻辑******************************** isAddForm() { return this.editDto && this.editDto.OpSign === OpMode.OpAdd; } isEditForm() { return this.editDto && this.editDto.OpSign === OpMode.OpEdit; } isAllowSort() { return this.selectedNode && this.selectedNode.children && this.selectedNode.children.length; } isEnable() { return this.editDto.IsEnable; } isSelectChildNode() { return this.selectedNode && this.selectedNode.level !== 0; } //#region 导出区域 closeExportModal() { this.isShowExportModal = false; } openExportModal() { this.isShowExportModal = true; } //#endregion 导出区域结束 }