fengxiang
2018-03-23 7f762717c4580a17377a33cf04351fa3abaea61d
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import {environment} from '../../../../environments/environment';
import {DateService} from '../../../business/services/util/date.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Router} from '@angular/router';
 
@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
  styles: []
})
 
export class DemoComponent implements OnInit {
  constructor(
    public http: HttpClient,
    public dateSrv: DateService,
    public router: Router,
    public msgSrv: NzMessageService
  ) {
    this.query.timeType = this.typeOptions[1];
    this.time.format = this.query.timeType.format.toUpperCase();
    this.query.reportType = this.reportOptions[1].value;
  }
  [x: string]: any;
  public query: any = {};
  public sensorOptions = [];
  public typeOptions = [
    {value: 'year', label: '年', format: 'yyyy', typeFormat: '%Y-%m', timeLength: 12},
    {value: 'month', label: '月', format: 'yyyy-MM', typeFormat: '%Y-%m-%d', timeLength: 28},
    {value: 'day', label: '日', format: 'yyyy-MM-dd', typeFormat: '%Y-%m-%d %H', timeLength: 24},
    {value: 'hour', label: '时', format: 'yyyy-MM-dd HH', typeFormat: '%Y-%m-%d %H:%i', timeLength: 60}
  ];
  public reportOptions = [
    {value: 'bar', label: '柱状图'},
    {value: 'line', label: '折线图'}
  ];
  public monitorPointOptions = [];
  public time: any = {};
  public items = [{
    id: 0,
    monitorPoint: null,
    monitorPointName: '',
    monitorPointAddress: '',
    mac: '',
    deviceName: '',
    time: null,
    deviceOptions: []
  }];
 
  ngOnInit() {
    this.http.get(environment.SERVER_BASH_URL + 'sensor/all').subscribe((res: any) => {
      if (res.code === 0) {
        this.msgSrv.error(res.message);
      } else {
        this.sensorOptions = res.data;
      }
    });
  }
 
  typeChange(searchText) {
    this.typeOptions.forEach(types => {
      if (types === searchText) {
        this.time.format = types.format.toUpperCase();
      }
    });
  }
 
  addItem() {
    const id = (this.items.length > 0) ? this.items[this.items.length - 1].id + 1 : 0;
    const index = this.items.push({
      id: id,
      monitorPoint: null,
      monitorPointName: '',
      monitorPointAddress: '',
      mac: '',
      deviceName: '',
      time: null,
      deviceOptions: []
    });
  }
 
  searchChange(searchText, i) {
    if (searchText) {
      const query = encodeURI(searchText);
      if (query) {
        this.http.get(environment.SERVER_BASH_URL + '/monitor-point/list/' + query).subscribe((res: any) => {
          if (res.code === 0) {
            this.msgSrv.error(res.message);
          } else {
            this.monitorPointOptions = res.data;
          }
        });
      }
    }
  }
 
  monitorPointChange(value, i) {
    this.items[i].deviceOptions = [];
    if (value) {
      this.monitorPointOptions.forEach(monitorPoint => {
        if (monitorPoint.id === value) {
          this.items[i].monitorPointName = monitorPoint.name;
          this.items[i].monitorPointAddress = monitorPoint.address;
        }
      });
      this.http.get(environment.SERVER_BASH_URL + 'device/monitorPointId', {params: {monitorPointId: value}}).subscribe((res: any) => {
        if (res.code === 0) {
          this.msgSrv.error(res.message);
        } else {
          this.items[i].deviceOptions = res.data;
          this.items[i].mac = null;
        }
      });
    } else {
      this.items[i].monitorPoint = null;
      this.items[i].mac = null;
      this.items[i].monitorPointName = '';
      this.items[i].monitorPointAddress = '';
    }
  }
 
  deviceChange(value, i) {
    if (value) {
      this.items[i].deviceOptions.forEach(device => {
        if (device.mac === value) {
          this.items[i].deviceName = device.name;
        }
      });
    } else {
      this.items[i].deviceName = '';
    }
  }
 
  _disabledDate(current: Date): boolean {
    return current && current.getTime() > Date.now();
  }
 
  reportQuery() {
    const query = this.query;
    let validate = true;
    const queryItems = [];
    for (let i = 0; i < this.items.length; i++) {
      let item = this.items[i];
      let queryItem: any = {};
      if (item.monitorPoint && item.time) {
        queryItem.formatTime = this.dateSrv.date_format(item.time, this.time.format);
        for (var key in item) {
          if (item[key]) {
            queryItem[key] = item[key];
          }
        }
        queryItem.deviceCount = queryItem.deviceOptions.length;
        delete queryItem.deviceOptions;
        delete queryItem.time;
        queryItems.push(queryItem);
      } else {
        validate = false;
        break;
      }
    }
    if (validate && query.timeType && query.reportType) {
      query.sensors = null;
      if (query.sensorKey && query.sensorKey.length > 0) {
        const sensors = [];
        query.sensorKey.forEach(sensor => {
          sensors.push(sensor.sensorKey + '-' + sensor.name + '-' + sensor.unit);
        });
        query.sensors = JSON.stringify(sensors);
      }
      query.items = JSON.stringify(queryItems);
      query.timeTypes = JSON.stringify(query.timeType);
      this.router.navigate(['report'], {queryParams: query});
    } else {
      this.msgSrv.error('请完善搜索项或删除查询条目');
    }
 
  }
}