| | |
| | | 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 { 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 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]], |
| | | [increment[1]], |
| | | [increment[2]] |
| | | ]); |
| | | let degression = alarmLevels[key].degression; |
| | | degression = degression == null || degression.length != 3 ? [0, 0, 0] : degression; |
| | | const degressionArray = |
| | | this.formBuilder.array([ |
| | | [degression[0]], |
| | | [degression[1]], |
| | | [degression[2]] |
| | | ]); |
| | | //判断逆向是否启用 |
| | | // 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 = {}; |
| | | const i0_value = parseInt(i0.value); |
| | | const i1_value = parseInt(i1.value); |
| | | const i2_value = parseInt(i2.value); |
| | | 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); |
| | | } |
| | | const d0_value = parseInt(d0.value); |
| | | const d1_value = parseInt(d1.value); |
| | | const d2_value = parseInt(d2.value); |
| | | 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']; |
| | | } |
| | | ) |
| | | } |
| | | } |
| | | } |