import { FormHelper, provideValueAccessor, noop, ObjectHelper } from 'rh-base/core'; import { SAMPLE_TYPE_DATAS } from './../../qmp-data'; import { RhSafeAny, RhSelectableDto, RhSelectItem } from 'rh-base/model'; import { IqcInspectSchemaGroupDto, SampleType, SampleCategoryType } from './../../qmp-model'; import { Component, OnInit, Input } from '@angular/core'; import { UntypedFormGroup, UntypedFormBuilder, Validators, ControlValueAccessor } from '@angular/forms'; import { NzSizeLDSType } from 'ng-zorro-antd/core/types'; import { isNotNil } from 'ng-zorro-antd/core/util'; @Component({ selector: 'rh-inspect-sample-rule', templateUrl: './inspect-sample-rule.component.html', styleUrls: ['./inspect-sample-rule.component.less'], providers: [provideValueAccessor(RhInspectSampleRuleComponent)] }) export class RhInspectSampleRuleComponent implements OnInit, ControlValueAccessor { @Input() rhData!: IqcInspectSchemaGroupDto; @Input() rhPlaceholder!: string; @Input() rhSize!: NzSizeLDSType; /** 是否显示国标tab */ @Input() rhShowStandardTab = true; /** 是否显示固定数量tab */ @Input() rhShowFixedTab = true; /** 是否显示固定百分比tab */ @Input() rhShowPercentagedTab = true; currentSampleCategory!: SampleCategoryType; sampleRuleDto!: IqcInspectSchemaGroupDto; sampleRuleForm!: UntypedFormGroup; isShowModal = false; // displayValue: string; _displayValue!: string; get displayValue() { return this._displayValue; } set displayValue(value: string) { this._displayValue = value; this.onChange ? this.onChange(value) : noop(); } sampleCategoryDataset = SAMPLE_TYPE_DATAS; categoryDateset: RhSelectItem[] = []; onChange!: (value: string) => void; onTouched!: (value: string) => void; constructor(public fb: UntypedFormBuilder) { // } ngOnInit(): void { this.modifyCategorySample(); setTimeout(() => { this.setDisplayValue(this.rhData?.SampleCategory as SampleCategoryType); }, 100); // this.rhData.SampleCategory = this.rhData.SampleCategory ; // this.currentSampleCategory = this.rhData.SampleCategory; this.sampleRuleDto = { ...this.rhData }; this.createSampleRuleForm(this.sampleRuleDto); this.rhSize = this.rhSize || 'default'; // this.setStandardInspectValidator(); } back() { this.closeModal(); } modifyCategorySample() { if (this.rhShowStandardTab) { this.categoryDateset.push(this.sampleCategoryDataset[0]); } if (this.rhShowFixedTab) { this.categoryDateset.push(this.sampleCategoryDataset[1]); } if (this.rhShowPercentagedTab) { this.categoryDateset.push(this.sampleCategoryDataset[2]); } } closeModal() { this.isShowModal = false; this.resetSampleRuleForm(null); } createSampleRuleForm(dto: IqcInspectSchemaGroupDto) { this.currentSampleCategory = this.rhData.SampleCategory || 'StandardInspect'; this.sampleRuleForm = this.fb.group({ SampleCategory: [dto.SampleCategory || this.currentSampleCategory, [Validators.required]], // 正常抽样方案 NormalSampleSchemaCode: [dto.NormalSampleSchemaCode], // NormalSampleSchemaName: [dto.NormalSampleSchemaName, [Validators.required]], NormalAqlValue: [dto.NormalAqlValue], // 加严抽样方案 TightenedSampleSchemaCode: [dto.TightenedSampleSchemaCode], // TightenedSampleSchemaName: [dto.TightenedSampleSchemaName, [Validators.required]], TightenedAqlValue: [dto.TightenedAqlValue], // 放宽抽样方案 ReducedSampleSchemaCode: [dto.ReducedSampleSchemaCode], // ReducedSampleSchemaName: [dto.ReducedSampleSchemaName, [Validators.required]], ReducedAqlValue: [dto.ReducedAqlValue], FixedInspectQty: [dto.FixedInspectQty], RateInspectQty: [dto.RateInspectQty] }); switch (this.currentSampleCategory) { case 'StandardInspect': this.setStandardInspectValidator(); break; case 'FixedInspect': this.setFixedInspectValidator(); break; case 'PercentageInspect': this.setPercentageInspectValidator(); break; } } onSelectSampleSchema(data: RhSelectableDto, sampleCategory: SampleType): void { switch (sampleCategory) { case 'Normal': this.sampleRuleDto.NormalSampleSchemaCode = data['SampleSchemaCode']; this.sampleRuleDto.NormalSampleSchemaName = data['SampleSchemaName']; this.sampleRuleDto.NormalAqlValue = data['AqlValue']; this.sampleRuleForm.get('NormalAqlValue')?.patchValue(data['AqlValue']); break; case 'Tighten': this.sampleRuleDto.TightenedSampleSchemaCode = data['SampleSchemaCode']; this.sampleRuleDto.TightenedSampleSchemaName = data['SampleSchemaName']; this.sampleRuleDto.TightenedAqlValue = data['AqlValue']; this.sampleRuleForm.get('TightenedAqlValue')?.patchValue(data['AqlValue']); break; case 'Reduce': this.sampleRuleDto.ReducedSampleSchemaCode = data['SampleSchemaCode']; this.sampleRuleDto.ReducedSampleSchemaName = data['SampleSchemaName']; this.sampleRuleDto.ReducedAqlValue = data['AqlValue']; this.sampleRuleForm.get('ReducedAqlValue')?.patchValue(data['AqlValue']); break; default: { const exhaustiveObj: never = sampleCategory; return exhaustiveObj; } } } openModal() { this.isShowModal = true; } resetSampleRuleForm($event: Event | null) { if ($event) { $event.preventDefault(); } this.sampleRuleForm.reset(this.rhData); } submitSampleRuleForm($event: Event) { if ($event) { $event.preventDefault(); } FormHelper.YGSubmitForm(this.sampleRuleDto, this.sampleRuleForm, (dto) => { this.setDisplayValue(this.currentSampleCategory); ObjectHelper.MapT(dto as RhSafeAny, this.rhData); this.closeModal(); }); } onSampleCategoryChange(category: SampleCategoryType) { this.currentSampleCategory = category; // this.sampleRuleForm.get('NormalSampleSchemaCode').clearValidators(); // this.sampleRuleForm.get('NormalAqlValue').clearValidators(); // this.sampleRuleForm.get('TightenedSampleSchemaCode').clearValidators(); // this.sampleRuleForm.get('TightenedAqlValue').clearValidators(); // this.sampleRuleForm.get('ReducedSampleSchemaCode').clearValidators(); // this.sampleRuleForm.get('ReducedAqlValue').clearValidators(); // 设置其他两个tab对应字段的校验 switch (category) { case 'StandardInspect': this.setStandardInspectValidator(); this.clearFixedInspectValidator(); this.clearPercentageInspetValidator(); break; case 'FixedInspect': this.setFixedInspectValidator(); this.clearStandardInspectValidator(); this.clearPercentageInspetValidator(); break; case 'PercentageInspect': this.setPercentageInspectValidator(); this.clearStandardInspectValidator(); this.clearFixedInspectValidator(); break; } this.sampleRuleForm.updateValueAndValidity(); } private setStandardInspectValidator() { if (!this.sampleRuleForm) { return; } this.sampleRuleForm.get('NormalSampleSchemaCode')?.setValidators(Validators.required); this.sampleRuleForm.get('NormalAqlValue')?.setValidators(Validators.required); this.sampleRuleForm.get('TightenedSampleSchemaCode')?.setValidators(Validators.required); this.sampleRuleForm.get('TightenedAqlValue')?.setValidators(Validators.required); this.sampleRuleForm.get('ReducedSampleSchemaCode')?.setValidators(Validators.required); this.sampleRuleForm.get('ReducedAqlValue')?.setValidators(Validators.required); } private clearStandardInspectValidator() { if (!this.sampleRuleForm) { return; } this.sampleRuleForm.get('NormalSampleSchemaCode')?.clearValidators(); this.sampleRuleForm.get('NormalAqlValue')?.clearValidators(); this.sampleRuleForm.get('TightenedSampleSchemaCode')?.clearValidators(); this.sampleRuleForm.get('TightenedAqlValue')?.clearValidators(); this.sampleRuleForm.get('ReducedSampleSchemaCode')?.clearValidators(); this.sampleRuleForm.get('ReducedAqlValue')?.clearValidators(); } private setFixedInspectValidator() { if (!this.sampleRuleForm) { return; } this.sampleRuleForm.get('FixedInspectQty')?.setValidators(Validators.required); } private clearFixedInspectValidator() { if (!this.sampleRuleForm) { return; } this.sampleRuleForm.get('FixedInspectQty')?.clearValidators(); } private setPercentageInspectValidator() { if (!this.sampleRuleForm) { return; } this.sampleRuleForm.get('RateInspectQty')?.setValidators(Validators.required); } private clearPercentageInspetValidator() { if (!this.sampleRuleForm) { return; } this.sampleRuleForm.get('RateInspectQty')?.clearValidators(); } private setDisplayValue(sampleCategory: SampleCategoryType) { switch (sampleCategory) { case 'StandardInspect': this.displayValue = '国标'; break; case 'PercentageInspect': { const percentStr = isNotNil(this.sampleRuleDto?.RateInspectQty) ? `:${this.sampleRuleDto.RateInspectQty}%` : ''; this.displayValue = `固定百分比${percentStr}`; break; } case 'FixedInspect': { const FixedStr = isNotNil(this.sampleRuleDto?.FixedInspectQty) ? `:${this.sampleRuleDto.FixedInspectQty}` : ''; this.displayValue = `固定数量${FixedStr}`; break; } default: this.displayValue = ''; break; } } //#region controlvalueaccessor区域 writeValue(obj: string): void { this.displayValue = obj; } registerOnChange(fn: (value: string) => void): void { this.onChange = fn; } registerOnTouched(fn: (value: string) => void): void { this.onTouched = fn; } // setDisabledState?(isDisabled: boolean): void { // } //#endregion controlvalueaccessor区域结束 }