fengxiang
2018-04-17 39ea49edce74ae063ca0afd9b2966f861e256a3e
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {environment} from '../../../../environments/environment';
import {DateService} from '../../../business/services/util/date.service';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {Component, OnInit, Inject} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {ITokenService, DA_SERVICE_TOKEN} from '@delon/auth';
import {CascaderOption} from 'ng-zorro-antd/src/cascader/nz-cascader.component';
import {AreacodeService} from '@business/services/http/areacode.service';
import {NzTreeComponent} from 'ng-tree-antd';
import {Subject} from 'rxjs/Subject';
 
@Component({
  selector: 'app-excel',
  templateUrl: './excel.component.html',
  styles: []
})
export class ExcelComponent implements OnInit {
  [x: string]: any;
  public query: any = {};
  public sensorOptions = [];
  public monitorPointOptions = [];
  public deviceOptions = [];
  public typeOptions = [
    {value: 'year', label: '年', format: 'YYYY'},
    {value: 'month', label: '月', format: 'YYYY-MM'},
    {value: 'day', label: '日', format: 'YYYY-MM-DD'},
    {value: 'hour', label: '时', format: 'YYYY-MM-DD HH'}
  ];
 
  private treeClickStream: Subject<any> = new Subject<any>();
  private _sensors: {[key: string]: string} = {};
  private _sensorNames: string;
  get sensorNames(): string {
    return this._sensorNames;
  }
 
  constructor(
    public http: HttpClient,
    public dateSrv: DateService,
    public msgSrv: NzMessageService,
    private areacodeService: AreacodeService,
    @Inject(DA_SERVICE_TOKEN) public tokenService: ITokenService
  ) {
    this.timeType = this.typeOptions[1];
  }
 
  ngOnInit() {
    this.query.time = null;
    this.query.timeb = null;
    this.http.get(environment.SERVER_BASH_URL + 'sensor/all').subscribe((res: any) => {
      if (res.code === 0) {
        this.msgSrv.error(res.message);
      } else {
        this.sensorOptions.push({id: -1, name: '全部', isExpanded: true, children: res.data});
      }
    });
  }
 
  public onTreeClickSelect(event): void {
    this.treeClickStream.next(event);
  }
 
  public onSensorSelect(event): void {
    const data = event.node.data;
    if (data.id === -1 && data.halfChecked === false) {
      if (!!data.checked) {
        this.sensorOptions[0].children.forEach(
          sensor => {
            this._sensors[sensor.id] = sensor.sensorKey + '-' + sensor.name + '-' + sensor.unit;
          }
        );
      } else {
        this._sensors = {};
      }
    } else {
      if (!!data.checked) {
        this._sensors[data.id] = data.sensorKey + '-' + data.name + '-' + data.unit;
      } else {
        delete this._sensors[data.id];
      }
    }
    this.reloadSensorNames();
 
  }
  private reloadSensorNames(): void {
    // 异步提升展现速度
    setTimeout(() => {
      this._sensorNames = '';
      const sensorNameList = Object.keys(this._sensors).map(
        id => {
          const sensor = this.sensorOptions[0].children.find(item => {
            return Number(id) === Number(item.id);
          });
          return sensor.name;
        }
      );
      this._sensorNames = sensorNameList.join(', ');
    }, 1);
  }
 
  public areaLazyLoad(event: {option: CascaderOption, index: number, resolve: (children: CascaderOption[]) => void, reject: () => void}) {
    const index = event['index'];
    const option = event.option;
    switch (index) {
      case -1:
        this.areacodeService.getProvinces().subscribe(
          (res: {label: string, value: string}[]) => {
            event.resolve(res);
          }
        ); break;
      case 0:
        this.areacodeService.getCities(option.value).subscribe(
          (res: {label: string, value: string}[]) => {
            event.resolve(res);
          }
        ); break;
      case 1:
        this.areacodeService.getAreas(option.value).subscribe(
          (res: {label: string, value: string}[]) => {
            event.resolve(res);
          }
        ); break;
    }
  }
 
  public regionChange(event: {option: CascaderOption, index: number}) {
    let name = '';
    const option = event.option;
    this.query.areaCode = null;
    this.query.monitorPointId = null;
    this.query.mac = null;
    switch (event.index) {
      case 0:
        name = 'provinceCode';
        this.query.provinceCode = option.value;
        this.query.cityCode = null;
        break;
      case 1:
        name = 'cityCode';
        this.query.cityCode = option.value;
        break;
      case 2:
        name = 'areaCode';
        this.query.areaCode = option.value;
        break;
    }
    this.http.get(environment.SERVER_BASH_URL + 'monitor-point/list/region', {params: {name: name, value: option.value}}).subscribe((res: any) => {
      if (res.code === 0) {
        this.msgSrv.error(res.message);
      } else {
        this.monitorPointOptions = res.data;
      }
    });
  }
 
  monitorPointChange(value) {
    this.query.mac = null;
    this.deviceOptions = [];
    if (value) {
      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.deviceOptions = res.data;
        }
      });
    }
  }
 
  reportQuery() {
    const query = this.query;
    if (this.monitorPointOptions.length > 0 && query.time) {
      query.time = this.dateSrv.date_format(query.time, this.timeType.format);
      if (query.timeb) {
        query.timeb = this.dateSrv.date_format(query.timeb, this.timeType.format);
      }
      query.type = this.timeType.value;
      if (this._sensors) {
        const sensors = [];
        for (const key in this._sensors) {
          sensors.push(this._sensors[key]);
        }
        if (sensors.length > 0) {
          query.sensors = JSON.stringify(sensors);
        }
      }
 
      let url = environment.SERVER_BASH_URL + 'report/excel?';
      for (const a in query) {
        if (query[a]) {
          url += encodeURI(a) + '=' + encodeURI(query[a]) + '&';
        }
      }
      window.location.href = url + '_token=' + this.tokenService.get().token;
    } else {
      this.msgSrv.error('开始时间为空或者所选区域没有监控点数据!');
    }
  }
}