From fdde9a5cb356933bac26b64b67b53167ce2f41bd Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Thu, 17 May 2018 10:03:29 +0800
Subject: [PATCH] 行业 优化

---
 src/app/routes/reports/excel/excel.component.ts |  209 +++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 141 insertions(+), 68 deletions(-)

diff --git a/src/app/routes/reports/excel/excel.component.ts b/src/app/routes/reports/excel/excel.component.ts
index 24d869b..8e4dba4 100644
--- a/src/app/routes/reports/excel/excel.component.ts
+++ b/src/app/routes/reports/excel/excel.component.ts
@@ -3,7 +3,11 @@
 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 {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',
@@ -12,25 +16,34 @@
 })
 export class ExcelComponent implements OnInit {
   [x: string]: any;
-
-  query: any = {
-  };
-
-  time: any = {
-    format: 'YYYY',
-    mode: 'month'
-  };
-
+  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'}
+  ];
+  public professionOptions = [];
+
+  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,
-    @Inject(DA_SERVICE_TOKEN)public tokenService: ITokenService
-  ) {}
+    private areacodeService: AreacodeService,
+    @Inject(DA_SERVICE_TOKEN) public tokenService: ITokenService
+  ) {
+    this.timeType = this.typeOptions[1];
+  }
 
   ngOnInit() {
     this.query.time = null;
@@ -39,95 +52,155 @@
       if (res.code === 0) {
         this.msgSrv.error(res.message);
       } else {
-        this.sensorOptions = res.data;
+        this.sensorOptions.push({id: -1, name: '������', isExpanded: true, children: res.data});
+      }
+    });
+    this.http.get(environment.SERVER_BASH_URL + 'mobile/profession').subscribe((res: any) => {
+      if (res.errno !== 0) {
+        this.msgSrv.error(res.message);
+      } else {
+        this.professionOptions = res.data;
       }
     });
   }
 
-  sensorChange(value) {
-    this.sensorOptions.forEach(sensor => {
-      if (sensor.sensorKey === value) {
-        this.sensor = sensor;
-      }
-    });
+  public onTreeClickSelect(event): void {
+    this.treeClickStream.next(event);
   }
 
-  searchChange(searchText) {
-    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;
+  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);
   }
 
-  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;
-        }
-      });
-    } else {
-      this.monitorPointa = null;
+  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;
+    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;
+      }
+    });
+   this.getDevices();
 
-  public typeOptions = [
-    {value: 'year', label: '���', mode: 'month', xAxisName: '���', format: 'yyyy', typeFormat: '%Y-%m'},
-    {value: 'month', label: '���', mode: 'month', xAxisName: '���', format: 'yyyy-MM', typeFormat: '%Y-%m-%d'},
-    {value: 'day', label: '���', mode: 'day', xAxisName: '���', format: 'yyyy-MM-dd', typeFormat: '%Y-%m-%d %H'},
-    {value: 'hour', label: '���', mode: 'day', xAxisName: '���', format: 'yyyy-MM-dd HH', typeFormat: '%Y-%m-%d %H:%i'}
-  ];
+  }
 
-
-  typeChange(searchText) {
-    this.typeOptions.forEach(types => {
-      if (types.value === searchText) {
-        this.timeType = types;
-        this.time.format = types.format.toUpperCase();
-        this.time.mode = types.mode;
+  getDevices() {
+    this.query.mac = null;
+    return this.http.get(environment.SERVER_BASH_URL + 'device/professionId', {params: this.query}).subscribe((res: any) => {
+      if (res.code === 0) {
+        this.msgSrv.error(res.message);
+      } else {
+        this.deviceOptions = res.data;
       }
     });
   }
 
   reportQuery() {
     const query = this.query;
-    if (query.type && query.monitorPoint && query.time) {
-      if (query.time) {
-        query.time = this.dateSrv.date_format(query.time, this.time.format);
-      }
+    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.time.format);
+        query.timeb = this.dateSrv.date_format(query.timeb, this.timeType.format);
       }
-      if (query.sensorKey) {
-        query.sensorName = this.sensor.name;
+      query.type = this.timeType.value;
+      delete query.sensors;
+      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);
+        }
       }
-      query.format = this.timeType.format;
-      query.typeFormat = this.timeType.typeFormat;
+
       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('������������������');
+      this.msgSrv.error('���������������������������������������������������������������');
     }
-
   }
-
 }

--
Gitblit v1.8.0