import { RhAacService } from './../../aac.service'; import { Component, OnInit } from '@angular/core'; import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; import { OpMode, RhSafeAny, RhUserDomainDto } from 'rh-base/model'; import { MsgHelper, FormHelper, ArrayHelper } from 'rh-base/core'; @Component({ selector: 'rh-user-domain-manage', templateUrl: './user-domain-manage.component.html', styleUrls: ['./user-domain-manage.component.less'] }) export class RhUserDomainManageComponent implements OnInit { userDomainForm!: UntypedFormGroup; fFormData!: RhUserDomainDto; submitBusy = false; filterUserId = ''; dataSetLoading = false; dataSet: RhUserDomainDto[] = []; opSign: number = OpMode.OpAdd; constructor(private fb: UntypedFormBuilder, private operator: RhAacService) {} // userIdAsyncValidator = (control: FormControl): Observable<{ [name: string]: boolean }> => // new Observable((observer: Observer) => { // control.valueChanges.pipe( // debounceTime(500), // distinctUntilChanged(), // switchMap(value => this.operator.CheckUserExist(new UpdatePasswordDto(value, null, null))) // ).subscribe(result => { // if (result && result.Success) { // observer.next({ inexistent: false }); // } else { // observer.next({ inexistent: true }); // } // observer.complete(); // }); // }) ngOnInit() { this.fFormData = this.initUserDomain(); this.createUserDomainForm(this.fFormData); } createUserDomainForm(dto: RhUserDomainDto): void { // 创建表单 this.userDomainForm = this.fb.group({ UserId: [dto.UserId, [Validators.required]], FactoryCode: [dto.FactoryCode], GroupCode: [dto.GroupCode], SupplierCode: [dto.SupplierCode] }); // 发布订阅 const inputField = this.userDomainForm.get('UserId'); // AuthValidatorHelper.ValidateUserExist(inputField, this.operator); } initUserDomain(): RhUserDomainDto { return new RhUserDomainDto(null, null, null, null); } searchUserDomainById(): void { if (this.filterUserId === null) { MsgHelper.ShowErrorModal('请先输入用户账号!'); return; } this.operator.GetUserDomain(this.filterUserId).subscribe((result) => { FormHelper.HandleDataResult(result, (r) => { this.filterUserId = ''; this.dataSet = [result.Attach]; }); }); } searchByUserId($event: RhSafeAny): void { if ($event.keyCode === 13) { this.searchUserDomainById(); } } editDto(item: RhUserDomainDto): void { this.fFormData = item; this.opSign = OpMode.OpEdit; this.userDomainForm.reset(item); } removeDto(item: RhUserDomainDto): void { MsgHelper.ShowDeleteConfirm('您确定要删除用户域的相关数据吗?', () => { this.operator.RemoveUserDomain(item).subscribe((result) => { FormHelper.HandleOpResult(result, (r) => { this.dataSet = ArrayHelper.Remove(this.dataSet, (f) => f.UserId === item.UserId); }); }); }); } submitForm($event: Event): void { $event.preventDefault(); FormHelper.YGSubmitForm(this.fFormData, this.userDomainForm, (dto) => { this.submitBusy = true; this.operator.CreateUserDomain(dto as RhUserDomainDto).subscribe((result) => { FormHelper.HandleOpResult(result, (r) => { this.resetForm($event); this.opSign = OpMode.OpAdd; this.dataSet.push(dto as RhUserDomainDto); MsgHelper.ShowInfoModal(r.Message as string); }); }); this.submitBusy = false; }); } resetForm($event: Event): void { $event.preventDefault(); this.submitBusy = false; this.userDomainForm.reset(this.initUserDomain()); } }