fengxiang
2018-06-25 27cd36be226ca2434f06b1ae9e4d43f1fea639ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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);
    }
}