| | |
| | | import { Component, OnInit } from '@angular/core'; |
| | | import { _HttpClient } from '@delon/theme'; |
| | | import { NzModalSubject } from 'ng-zorro-antd'; |
| | | import { FormGroup } from '@angular/forms'; |
| | | import { FormGroup, Validators, FormBuilder, FormControl } from '@angular/forms'; |
| | | import { Sensor, SensorUnit, OperatorRule } from '@business/entity/data'; |
| | | import { ToolsService } from '@business/services/util/tools.service'; |
| | | import { Operator, ResultCode } from '@business/enum/types.enum'; |
| | | import { ResultBean } from '@business/entity/grid'; |
| | | import { SensorUnitService } from '@business/services/http/sensor-unit.service'; |
| | | |
| | | @Component({ |
| | | selector: 'app-sensor-unit', |
| | |
| | | export class SensorUnitComponent implements OnInit { |
| | | public isSaving = false; |
| | | public validateForm: FormGroup; |
| | | public data: any; |
| | | public operatorOptions: {label: string, value: Operator} [] = [ |
| | | {label: '加', value: Operator.PLUS}, |
| | | {label: '减', value: Operator.MINUS}, |
| | | {label: '乘', value: Operator.MULTIPLY}, |
| | | {label: '除', value: Operator.DIVIDE} |
| | | ]; |
| | | // public rule = '乘'; |
| | | public data: Sensor; |
| | | public dataList: SensorUnit[] = []; |
| | | constructor( |
| | | private subject: NzModalSubject, |
| | | private http: _HttpClient |
| | | private formBuilder: FormBuilder, |
| | | private http: _HttpClient, |
| | | private sensorUnitService: SensorUnitService |
| | | ) { } |
| | | |
| | | loadDataList() { |
| | | this.sensorUnitService.getListBySensorId(this.data.id).subscribe( |
| | | res => { |
| | | if (res.code === 1 ) { |
| | | this.dataList = res.data; |
| | | } |
| | | } |
| | | ); |
| | | } |
| | | public rulesToShow(rules: string) { |
| | | return rules.replace( /d/g, '').replace('{0}', '初值'); |
| | | } |
| | | ngOnInit() { |
| | | this.loadDataList(); |
| | | const validates: SensorUnit = { |
| | | id: [null ], |
| | | sensorId: [this.data.id, [Validators.required] ], |
| | | name: [null, [Validators.required] ] |
| | | }; |
| | | validates['_rule-operator'] = [Operator.MULTIPLY]; |
| | | validates['_rule-value'] = 1000; |
| | | validates['_rules'] = [null, [Validators.required]]; |
| | | this.validateForm = this.formBuilder.group( |
| | | validates |
| | | ); |
| | | // this.validateForm.controls['_rules'].disable(); |
| | | } |
| | | save($event, value, valid) { |
| | | $event.preventDefault(); |
| | |
| | | this.validateForm.controls[ i ].disable(); |
| | | } |
| | | this.isSaving = true; |
| | | Object.keys(value).forEach( (key: string) => { |
| | | // '_'为前缀的为自定义属性 |
| | | if (!key.startsWith('_') && value[key] != null) { |
| | | this.data[key] = value[key]; |
| | | ToolsService.removePrivate(value); |
| | | const sensorUnit: SensorUnit = {}; |
| | | Object.assign(sensorUnit, value); |
| | | sensorUnit['rules'] = this.rulesToString(this.rules); |
| | | sensorUnit['originalUnitName'] = this.data.unit; |
| | | console.log(JSON.stringify(sensorUnit)); |
| | | this.http.post('sensor-unit/add-or-modify', sensorUnit).subscribe( |
| | | (res: ResultBean<any>) => { |
| | | if (!!res.code) { |
| | | for (const i in this.validateForm.controls) { |
| | | this.validateForm.controls[ i ].enable(); |
| | | } |
| | | this.isSaving = false; |
| | | this.loadDataList(); |
| | | this.reset(); |
| | | } |
| | | } ); |
| | | } |
| | | ); |
| | | } else { |
| | | this.validate(); |
| | | } |
| | | } |
| | | toEdit(item) { |
| | | this.reset(); |
| | | this.setValueToControl('id', item.id); |
| | | this.setValueToControl('name', item.name); |
| | | } |
| | | delete(id) { |
| | | this.http.get('sensor-unit/delete', {id: id}).subscribe( |
| | | (res: ResultBean<any>) => { |
| | | if (!!res.code ) { |
| | | this.loadDataList(); |
| | | } |
| | | } |
| | | ); |
| | | } |
| | | validate() { |
| | | for (const i in this.validateForm.controls) { |
| | | this.validateForm.controls[ i ].markAsDirty(); |
| | | } |
| | | } |
| | | private rules: OperatorRule[] = []; |
| | | addRule(event) { |
| | | event.preventDefault(); |
| | | const rule: OperatorRule = { |
| | | operator: this.getValueFormControl('_rule-operator'), |
| | | value: this.getValueFormControl('_rule-value') |
| | | }; |
| | | this.rules.push(rule); |
| | | this.setValueToControl('_rules' , this.rulesToString(this.rules, '初值')); |
| | | } rulesToString(rules , dataLabel?: string) { |
| | | let rulesString = '' ; |
| | | const isDataLabel = dataLabel === undefined; |
| | | dataLabel = isDataLabel ? '{0}' : dataLabel; |
| | | if (!!this.rules && this.rules.length > 0) { |
| | | // const dataLabel = '初值'; |
| | | rulesString += dataLabel ; |
| | | this.rules.forEach(rule => { |
| | | if (!rulesString.endsWith(dataLabel)) { |
| | | rulesString = '(' + rulesString + ')'; |
| | | } |
| | | switch (rule.operator) { |
| | | case Operator.PLUS: rulesString += '+' + rule.value; break; |
| | | case Operator.MINUS: rulesString += '-' + rule.value; break; |
| | | case Operator.MULTIPLY: rulesString += '*' + rule.value; break; |
| | | case Operator.DIVIDE: rulesString += '/' + rule.value; break; |
| | | } |
| | | if ( isDataLabel ) { |
| | | rulesString += 'd'; |
| | | } |
| | | }); |
| | | } |
| | | return rulesString; |
| | | } |
| | | getValueFormControl(controlName: string) { |
| | | return ToolsService.getValueFormControl(this.validateForm, controlName); |
| | | } |
| | | setValueToControl(controlName: string, value: any) { |
| | | ToolsService.setValueToControl(this.validateForm, controlName, value); |
| | | } |
| | | clearRules(event) { |
| | | event.preventDefault(); |
| | | this.rules = []; |
| | | this.setValueToControl('_rules', this.rulesToString(this.rules, '初值')); |
| | | } |
| | | // print() { |
| | | // debugger; |
| | | // console.log(this.validateForm.controls['_rule']); |
| | | // } |
| | | reset(event?: any) { |
| | | if (!!event) { |
| | | event.preventDefault(); |
| | | } |
| | | this.setValueToControl('id', null); |
| | | this.setValueToControl('name', null); |
| | | this.setValueToControl('_rules', null); |
| | | this.setValueToControl('_rule-operator', Operator.MULTIPLY); |
| | | this.setValueToControl('_rule-value', 1000); |
| | | } |
| | | } |