| | |
| | | import { NzMessageService } from 'ng-zorro-antd'; |
| | | import { filter } from 'rxjs/operators'; |
| | | import { HttpClient } from '@angular/common/http'; |
| | | import { FormGroup, FormBuilder, FormControl, Validators, FormArray } from '@angular/forms'; |
| | | import { FormGroup, FormBuilder, FormControl, Validators, FormArray, AbstractControl } from '@angular/forms'; |
| | | import { SensorsService } from '@business/services/http/sensors.service'; |
| | | import { Grid, PageBean } from '@business/entity/grid'; |
| | | import { Organization, AlarmConfig, AlarmSensorLevel, AlarmConfigValue } from '@business/entity/data'; |
| | |
| | | import { AlarmConfigService } from '@business/services/http/alarm-config.service'; |
| | | import { AlarmStyle } from '@business/enum/types.enum'; |
| | | import { patterns } from '@business/enum/patterns.enum'; |
| | | import { ToolsService } from '@business/services/util/tools.service'; |
| | | |
| | | @Component({ |
| | | selector: 'app-organization-config', |
| | |
| | | ngOnDestroy(): void { |
| | | this.backToList(); |
| | | } |
| | | private organization: Organization; |
| | | public organization: Organization; |
| | | grid: Grid<object> = new Grid<object>(null); |
| | | validateForm: FormGroup; |
| | | constructor( |
| | |
| | | [degression[1], Validators.pattern(patterns.num)], |
| | | [degression[2], Validators.pattern(patterns.num)] |
| | | ]); |
| | | //判断逆向是否启用 |
| | | // let _degressionEnable = degression[0] != 0 || degression[1] != 0 || degression[2] != 0; |
| | | |
| | | const alarmSensorGroup = this.formBuilder.group( |
| | | { |
| | | degressEnable:[alarmLevels[key].degressEnable], |
| | | enable: [alarmLevels[key].enable], |
| | | increment: incrementArray, |
| | | degression: degressionArray, |
| | |
| | | ) |
| | | this.refreshIndeterminate(); |
| | | } |
| | | this.refreshHasDegression(false); |
| | | // 延时加载避免ExpressionChangedAfterItHasBeenCheckedError |
| | | setTimeout(() => { |
| | | this.grid.loading = false; |
| | |
| | | checkAll(param) { |
| | | const keys = this.grid.data.map( |
| | | item => { |
| | | return item['key']; |
| | | return item['sensorKey']; |
| | | } |
| | | ); |
| | | this._allCheckTriggers = 0; |
| | |
| | | refreshIndeterminate() { |
| | | const keys = this.grid.data.map( |
| | | item => { |
| | | return item['key']; |
| | | return item['sensorKey']; |
| | | } |
| | | ); |
| | | const allChecked = keys.every(key => this.validateForm.get('alarmLevels.' + key + '.enable').value); |
| | | const allUnChecked = keys.every(key => !this.validateForm.get('alarmLevels.' + key + '.enable').value); |
| | | this.indeterminate = (!allChecked) && (!allUnChecked); |
| | | } |
| | | isSaving:boolean = false; |
| | | save($event, value, valid) { |
| | | $event.preventDefault(); |
| | | if (valid) { |
| | | this.isSaving = true; |
| | | const data:AlarmConfig = { |
| | | id:value._id, |
| | | organizationId:this.organization.id, |
| | | value:value |
| | | } |
| | | // ToolsService.removePrivate(data.value); |
| | | this.alarmConfigService.save(data).subscribe( |
| | | result => { |
| | | if(result!=null&&result.code==1){ |
| | |
| | | } |
| | | ); |
| | | } |
| | | debugger; |
| | | } |
| | | |
| | | setErrorMessage(){ |
| | |
| | | } |
| | | } |
| | | errorMessage:string = ''; |
| | | private removeError(error:'increment'|'degression',...controls:AbstractControl[]){ |
| | | controls.forEach( |
| | | item => { |
| | | if(item.hasError(error)&&Object.keys(item.errors).length==1){ |
| | | item.setErrors(null); |
| | | } |
| | | } |
| | | ); |
| | | } |
| | | private addError(error:'increment'|'degression',...controls:AbstractControl[]){ |
| | | controls.forEach( |
| | | item => { |
| | | let errs = {}; |
| | | errs[error] = true; |
| | | item.setErrors(errs); |
| | | item.markAsDirty(); |
| | | } |
| | | ); |
| | | } |
| | | private alarmLevelValidator = (control: FormControl): { [s: string]: boolean } => { |
| | | let result = {}; |
| | | const i0 = control.get("increment.0"); |
| | | const i1 = control.get("increment.1"); |
| | | const i2 = control.get("increment.2"); |
| | | if ((i0.value != 0 || i1.value != 0 || i2.value != 0)&&(i0.value >= i1.value||i1.value >= i2.value)) { |
| | | i0.setErrors({ increment: true }); |
| | | i0.markAsDirty(); |
| | | i1.setErrors({ increment: true }); |
| | | i1.markAsDirty(); |
| | | i2.setErrors({ increment: true }); |
| | | i2.markAsDirty(); |
| | | result["increment"] = true; |
| | | }else{ |
| | | if(i0.hasError('increment')&&Object.keys(i0.errors).length==1){ |
| | | i0.setErrors(null); |
| | | const enable = control.get("enable").value; |
| | | const degressEnable = control.get("degressEnable").value; |
| | | const i0 = control.get("increment.0"); |
| | | const i1 = control.get("increment.1"); |
| | | const i2 = control.get("increment.2"); |
| | | const d0 = control.get("degression.0"); |
| | | const d1 = control.get("degression.1"); |
| | | const d2 = control.get("degression.2"); |
| | | // 如果未启用 删除错误信息,不验证 |
| | | if(!enable){ |
| | | this.removeError('increment',i0,i1,i2); |
| | | this.removeError('degression',d0,d1,d2); |
| | | return null; |
| | | } |
| | | if(i1.hasError('increment')&&Object.keys(i1.errors).length==1){ |
| | | i1.setErrors(null); |
| | | } |
| | | if(i2.hasError('increment')&&Object.keys(i2.errors).length==1){ |
| | | i2.setErrors(null); |
| | | } |
| | | } |
| | | const d0 = control.get("degression.0"); |
| | | const d1 = control.get("degression.1"); |
| | | const d2 = control.get("degression.2"); |
| | | if ((d0.value != 0 || d1.value != 0 || d2.value != 0)&&(d1.value >= d0.value||d2.value >= d1.value)) { |
| | | d0.setErrors({ degression: true }); |
| | | d0.markAsDirty(); |
| | | d1.setErrors({ degression: true }); |
| | | d1.markAsDirty(); |
| | | d2.setErrors({ degression: true }); |
| | | d2.markAsDirty(); |
| | | result["degression"] = true; |
| | | }else{ |
| | | if(d0.hasError('degression')&&Object.keys(d0.errors).length==1){ |
| | | d0.setErrors(null); |
| | | } |
| | | if(d1.hasError('degression')&&Object.keys(d1.errors).length==1){ |
| | | d1.setErrors(null); |
| | | } |
| | | if(d2.hasError('degression')&&Object.keys(d2.errors).length==1){ |
| | | d2.setErrors(null); |
| | | } |
| | | } |
| | | return Object.keys(result).length == 0?null:result; |
| | | let result = {}; |
| | | if ((i0.value != 0 || i1.value != 0 || i2.value != 0)&&(i0.value >= i1.value||i1.value >= i2.value)) { |
| | | this.addError('increment',i0,i1,i2); |
| | | result["increment"] = true; |
| | | }else{ |
| | | this.removeError('increment',i0,i1,i2); |
| | | } |
| | | debugger; |
| | | if(degressEnable&&(d0.value != 0 || d1.value != 0 || d2.value != 0)&&(d1.value >= d0.value||d2.value >= d1.value)) { |
| | | this.addError('degression',d0,d1,d2); |
| | | result["degression"] = true; |
| | | }else{ |
| | | this.removeError('degression',d0,d1,d2); |
| | | } |
| | | return Object.keys(result).length == 0?null:result; |
| | | }; |
| | | getLastError(control:FormGroup|FormArray){ |
| | | if(control.errors!=null){ |
| | |
| | | } |
| | | } |
| | | } |
| | | public hasDegression = false; |
| | | public refreshHasDegression(value){ |
| | | if(value){ |
| | | this.hasDegression = true; |
| | | }else{ |
| | | this.hasDegression = Object.values(this.validateForm.value['alarmLevels']).some( |
| | | ( item:any) => { |
| | | return item['degressEnable']; |
| | | } |
| | | ) |
| | | } |
| | | } |
| | | } |