import { Component, OnInit } from '@angular/core';
|
import { _HttpClient } from '@delon/theme';
|
import { NzModalSubject } from 'ng-zorro-antd';
|
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',
|
templateUrl: './sensor-unit.component.html',
|
})
|
export class SensorUnitComponent implements OnInit {
|
public isSaving = false;
|
public validateForm: FormGroup;
|
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 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;
|
}
|
}
|
);
|
}
|
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();
|
if (valid) {
|
for (const i in this.validateForm.controls) {
|
this.validateForm.controls[ i ].disable();
|
}
|
this.isSaving = true;
|
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 = '' ;
|
dataLabel = dataLabel === undefined ? '{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 + 'd'; break;
|
case Operator.MINUS: rulesString += '-' + rule.value + 'd'; break;
|
case Operator.MULTIPLY: rulesString += '*' + rule.value + 'd'; break;
|
case Operator.DIVIDE: rulesString += '/' + rule.value + 'd'; break;
|
}
|
});
|
}
|
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);
|
}
|
}
|