import { FormHelper, ArrayHelper, MsgHelper, FileHelper } from 'rh-base/core'; import { Component, OnInit } from '@angular/core'; import { UntypedFormGroup, UntypedFormBuilder, Validators } from '@angular/forms'; import { RhAacService } from './../../aac.service'; import { OpMode, RHFeaturesDto, RHFeaturesQueryDto, RhSafeAny } from 'rh-base/model'; @Component({ selector: 'rh-feature-manage', templateUrl: './feature-manage.component.html', styleUrls: ['./feature-manage.component.less'] }) export class RhFeatureManageComponent implements OnInit { // **********查询form********************** queryForm!: UntypedFormGroup; /** 查询数据对象 */ queryDto!: RHFeaturesQueryDto; /** 导出专用查询对象 */ exportQueryDto!: RHFeaturesQueryDto; // *********编辑form********************** editForm!: UntypedFormGroup; editDto!: RHFeaturesDto; selectDatas: RHFeaturesDto[] = []; /** 提交Loading */ submitLoading = false; /** 表格Loading */ dataLoading = false; dataset: RHFeaturesDto[] = []; totalCount = 0; maxResultCount = 10; selectedIndex = 0; // check operation isAllCheck = false; isIndeterminate = false; pageIndex = 1; constructor(public fb: UntypedFormBuilder, public operator: RhAacService) {} ngOnInit() { this.queryDto = this.initQueryDto(); this.createQueryForm(this.queryDto); this.editDto = this.initEditItem(); this.createEditForm(this.editDto); } initQueryDto(): RHFeaturesQueryDto { const dto = RHFeaturesQueryDto.create(); dto.MaxResultCount = this.maxResultCount; dto.SkipCount = 0; return dto; } createQueryForm(dto: RHFeaturesQueryDto) { // 自行删除不需要的搜索条件 this.queryForm = this.fb.group({ /** 功能键值 */ Name: [dto.Name], /** 功能名称 */ DisplayName: [dto.DisplayName] }); } submitQueryForm(event: Event) { if (event) { event.preventDefault(); } if (this.queryDto) { this.queryDto.SkipCount = 0; } FormHelper.YGSubmitForm(this.queryDto, this.queryForm, (dto) => { this.getDatas(dto as RHFeaturesQueryDto); }); } resetQueryForm(event: Event) { if (event) { event.preventDefault(); } this.queryDto = this.initQueryDto(); this.queryForm.reset(this.queryDto); this.dataset = []; } pageIndexChange(event: number) { this.queryDto = this.initQueryDto(); this.queryDto.SkipCount = event * this.maxResultCount - this.maxResultCount; FormHelper.YGSubmitForm(this.queryDto, this.queryForm, (dto) => { this.getDatas(dto as RHFeaturesQueryDto); }); } pageSizeChange(event: number) { this.maxResultCount = event; this.pageIndexChange(1); } initEditItem(): RHFeaturesDto { const dto = RHFeaturesDto.create(); dto.OpSign = OpMode.OpAdd; return dto; } createEditForm(dto: RHFeaturesDto) { // 自行删除无需编辑的字段,修改验证器 this.editForm = this.fb.group({ /** 功能键值 */ Name: [dto.Name, [Validators.required]], /** 功能名称 */ DisplayName: [dto.DisplayName, [Validators.required]], /** 对应函数名称 */ MappedFunctionName: [dto.MappedFunctionName], /** 图标 */ Icon: [dto.Icon] }); } submitEditForm($event: Event) { if ($event) { $event.preventDefault(); } FormHelper.YGSubmitForm(this.editDto, this.editForm, (dto) => { this.operator.StoreFeaturesData(dto as RHFeaturesDto).subscribe((result) => { if (result && result.Success) { this.dataset = ArrayHelper.AddTo(this.dataset, dto as RHFeaturesDto, (item) => item.Name === dto?.Name); if (dto?.OpSign === OpMode.OpAdd) { MsgHelper.ShowSuccessMessage('添加信息成功!'); this.resetEditForm(null); } else { MsgHelper.ShowSuccessMessage('编辑信息成功'); this.closeEditTab(); } } else { MsgHelper.ShowWarningModal(`存储信息失败!${result.Message}`); } }); }); } resetEditForm($event: Event | null) { if ($event) { $event.preventDefault(); } if (this.editDto.OpSign === OpMode.OpAdd) { this.editDto = this.initEditItem(); } this.editForm.reset(this.editDto); } removeFormData() { MsgHelper.ShowDeleteConfirm('删除后数据无法恢复,是否确定?', () => { this.operator.RemoveFeaturesData(this.selectDatas).subscribe((result) => { if (result && result.Success) { this.getDatas(this.queryDto); MsgHelper.ShowSuccessMessage('删除成功!'); this.selectDatas = []; this.isAllCheck = this.isIndeterminate = false; } else { MsgHelper.ShowWarningModal(`删除数据失败!${result.Message}`); } }); }); } /** * 新建 */ openNewTab() { this.editDto = this.initEditItem(); this.editDto.OpSign = OpMode.OpAdd; this.createEditForm(this.editDto); this.selectedIndex = 1; } openEditTab(item: RHFeaturesDto) { // this.editDto.OpSign = OpMode.OpEdit; this.editDto = item; this.editDto.OpSign = OpMode.OpEdit; this.createEditForm(item); this.selectedIndex = 1; } closeEditTab() { this.resetEditForm(null); this.selectedIndex = 0; } allCheckedChange() { this.isIndeterminate = false; this.dataset.forEach((ele) => (ele.select = this.isAllCheck)); this.getSelectDatas(); } checkedChange() { this.isAllCheck = this.dataset.every((ele) => ele.select); this.isIndeterminate = !!(this.dataset.find((ele) => ele.select) && !this.isAllCheck); this.getSelectDatas(); } exportExcel() { if (!this.dataset.length) { MsgHelper.ShowWarningModal('表格无数据!'); return; } this.exportQueryDto.Mode = 8; // 模式8专门用于导出数据 // 显示正在导出对话框 const messageId = MsgHelper.ShowLoadingMessage('正在导出。。。'); this.operator.GetFeaturesDatas(this.exportQueryDto).subscribe( (result) => { if (result && result.Success) { FileHelper.exportExcelFile(result.Attach, '数据导出', 'datas'); } else { MsgHelper.ShowWarningModal(`获取导出数据失败!错误信息: ${result.Message}`); } MsgHelper.CloseLoadingMessage(messageId); }, () => { MsgHelper.CloseLoadingMessage(messageId); } ); } private getDatas(queryDto: RHFeaturesQueryDto) { this.setLoadingStatus(true); this.exportQueryDto = { ...queryDto }; if (queryDto.SkipCount === 0) { this.pageIndex = 1; } this.operator.GetFeaturesDatas(queryDto).subscribe( (result) => { if (result && result.Success) { this.dataset = result.Attach; this.totalCount = result.TotalCount; } else { MsgHelper.ShowInfoMessage('查询无数据'); this.dataset = []; this.totalCount = 0; } this.setLoadingStatus(false); }, () => { this.setLoadingStatus(false); } ); } private getSelectDatas() { this.selectDatas = this.dataset.filter((ele) => ele.select); } private setLoadingStatus(status: boolean) { this.dataLoading = this.submitLoading = status; } }