From 3bbe9a88d9ab747ee2e4f01128547efd1fc20038 Mon Sep 17 00:00:00 2001 From: fengxiang <110431245@qq.com> Date: Mon, 29 Jan 2018 16:27:17 +0800 Subject: [PATCH] 编译精简完成 --- src/app/routes/systems/organization/organization-config/organization-config.component.ts | 281 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 273 insertions(+), 8 deletions(-) diff --git a/src/app/routes/systems/organization/organization-config/organization-config.component.ts b/src/app/routes/systems/organization/organization-config/organization-config.component.ts index f9fdffa..34f4089 100644 --- a/src/app/routes/systems/organization/organization-config/organization-config.component.ts +++ b/src/app/routes/systems/organization/organization-config/organization-config.component.ts @@ -1,24 +1,289 @@ -import { Organization } from '@business/entity/data'; -import { Component, OnInit } from '@angular/core'; +import { NzMessageService } from 'ng-zorro-antd'; +import { filter } from 'rxjs/operators'; +import { HttpClient } from '@angular/common/http'; +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 { Component, OnInit, OnDestroy } from '@angular/core'; import { OrganizationService } from '@business/services/http/organization.service'; +import { _HttpClient } from '@delon/theme'; +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', templateUrl: './organization-config.component.html', - styles: [] + styles: [], + providers: [AlarmConfigService] }) -export class OrganizationConfigComponent implements OnInit { - organization: Organization; +export class OrganizationConfigComponent implements OnInit, OnDestroy { + ngOnDestroy(): void { + this.backToList(); + } + public organization: Organization; + grid: Grid<object> = new Grid<object>(null); + validateForm: FormGroup; constructor( - private organizationService: OrganizationService - ) { - console.log(this.organizationService.data); + private organizationService: OrganizationService, + private sensorsService: SensorsService, + private alarmConfigService: AlarmConfigService, + private formBuilder: FormBuilder, + private http: _HttpClient, + public msgSrv: NzMessageService + + ) { + this.organization = this.organizationService.data; } ngOnInit() { + this.load(); } + load(reload: boolean = false) { + if (reload) { + this.grid.pageIndex = 1; + } + // ������������������ExpressionChangedAfterItHasBeenCheckedError + setTimeout(() => { + this.grid.loading = true; + }, 1); + let pageBean = this.organizationService.config.pageBean; + let resultBean = this.organizationService.config.resultBean; + resultBean = resultBean == null ? {} : resultBean; + if (pageBean != null && pageBean.data != null) { + this.grid.initData(pageBean); + let alarmConfig: AlarmConfig = null; + if (resultBean == null || resultBean.data == null) { + alarmConfig = this.alarmConfigService.generateAlarmConfig(pageBean.data); + } else { + // ������ ������ ��������� + alarmConfig = this.alarmConfigService.generateAlarmConfig(pageBean.data, resultBean.data); + } + const alarmConfigValue = alarmConfig.value; + + const alarmLevels = alarmConfigValue.alarmLevels; + let alarmLevelsGroupsConfig = {}; + Object.keys(alarmLevels).forEach(key => { + + let increment = alarmLevels[key].increment; + increment = increment == null || increment.length != 3 ? [0, 0, 0] : increment; + const incrementArray = this.formBuilder.array([ + [increment[0], Validators.pattern(patterns.num)], + [increment[1], Validators.pattern(patterns.num)], + [increment[2], Validators.pattern(patterns.num)] + ]); + let degression = alarmLevels[key].degression; + degression = degression == null || degression.length != 3 ? [0, 0, 0] : degression; + const degressionArray = + this.formBuilder.array([ + [degression[0], Validators.pattern(patterns.num)], + [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, + }, { validator: this.alarmLevelValidator } + ); + alarmSensorGroup.get('enable').valueChanges.subscribe( + (value: any) => { + if (this._allCheckTriggers <= 0) { + this.refreshIndeterminate(); + } else { + this._allCheckTriggers--; + } + } + ); + alarmLevelsGroupsConfig[key] = alarmSensorGroup; + }); + this.alarmModes.push( + { label: '������', value: AlarmStyle.email, disabled: false }, + { label: '������', value: AlarmStyle.sms, disabled: false }, + { label: '������', value: AlarmStyle.voice, disabled: false }, + { label: '������', value: AlarmStyle.weixin, disabled: false } + ); + const alarmMode = alarmConfigValue.alarmMode; + this.validateForm = this.formBuilder.group({ + alarmLevels: this.formBuilder.group(alarmLevelsGroupsConfig), + alarmMode: this.formBuilder.group( + { + enable: [alarmMode.enable], + level1: [alarmMode.level1], + level2: [alarmMode.level2], + level3: [alarmMode.level3] + } + ), + '_id':alarmConfig.id, + '_allChecked': [] + }); + this.validateForm.statusChanges.subscribe( + item => { + this.setErrorMessage(); + } + ) + this.refreshIndeterminate(); + } + this.refreshHasDegression(false); + // ������������������ExpressionChangedAfterItHasBeenCheckedError + setTimeout(() => { + this.grid.loading = false; + }, 1); + } + alarmModes: { label: string, value: AlarmStyle, disabled: boolean }[] = []; backToList() { this.organizationService.handle = 'list'; this.organizationService.title = '������������'; } + indeterminate: boolean; + checkAll(param) { + const keys = this.grid.data.map( + item => { + return item['sensorKey']; + } + ); + this._allCheckTriggers = 0; + keys.forEach( + (key: string) => { + this._allCheckTriggers++; + this.validateForm.get('alarmLevels.' + key + '.enable').setValue(param); + } + ); + this.refreshIndeterminate(); + } + private _allCheckTriggers: number = 0; + refreshIndeterminate() { + const keys = this.grid.data.map( + item => { + 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){ + this.msgSrv.success(this.organization.name+' ���������������'); + this.backToList(); + } + } + ); + } + debugger; + } + + setErrorMessage(){ + this.errorMessage = ''; + const errObj = this.getLastError(this.validateForm); + if(errObj!=null&&Object.keys(errObj).length>0){ + if(errObj['increment']){ + this.errorMessage = '������ ������ ������ ������������'; + } else if(errObj['degression']){ + this.errorMessage = '������������ ������������ ������������ ������������'; + } else if(errObj['pattern']!=null&&errObj['pattern']['requiredPattern'] == '^'+patterns.num.toString()+'$'){ + this.errorMessage = '���������������������'; + } + } +} + 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 } => { + 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; + } + 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){ + return control.errors; + }else{ + const controls = Object.values(control.controls); + for(let i = controls.length-1;i>=0;i--){ + const c = controls[i]; + if(c.errors!=null){ + return c.errors; + }else if(c instanceof FormArray||c instanceof FormGroup){ + const result = this.getLastError(c); + if(result != null) { + return result; + } + } + } + } + } + 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']; + } + ) + } + } } -- Gitblit v1.8.0