From ba15aacf079d1a3fd13e4594f5a94cf25dacb036 Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Mon, 26 Mar 2018 14:25:26 +0800
Subject: [PATCH] 报表 优化

---
 src/app/routes/reports/demo/demo.component.ts |  114 ++++++++++++++++++++++----------------------------------
 1 files changed, 45 insertions(+), 69 deletions(-)

diff --git a/src/app/routes/reports/demo/demo.component.ts b/src/app/routes/reports/demo/demo.component.ts
index 54f635f..c64baca 100644
--- a/src/app/routes/reports/demo/demo.component.ts
+++ b/src/app/routes/reports/demo/demo.component.ts
@@ -12,45 +12,37 @@
 })
 
 export class DemoComponent implements OnInit {
-  constructor(
-    public http: HttpClient,
-    public dateSrv: DateService,
-    public router: Router,
-    public msgSrv: NzMessageService
-  ) {
-    const timeType = this.timeType = this.typeOptions[1];
-    this.query.type = timeType.value;
-    this.time.format = timeType.format.toUpperCase();
-    this.query.reportType = this.reportOptions[1].value;
-  }
   [x: string]: any;
   public query: any = {};
   public sensorOptions = [];
   public typeOptions = [
-    {value: 'year', label: '���', xAxisName: '���', format: 'yyyy', typeFormat: '%Y-%m', timeLength: 12},
-    {value: 'month', label: '���', xAxisName: '���', format: 'yyyy-MM', typeFormat: '%Y-%m-%d', timeLength: 28},
-    {value: 'day', label: '���', xAxisName: '���', format: 'yyyy-MM-dd', typeFormat: '%Y-%m-%d %H', timeLength: 24},
-    {value: 'hour', label: '���', xAxisName: '���', format: 'yyyy-MM-dd HH', typeFormat: '%Y-%m-%d %H:%i', timeLength: 60}
+    {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 deviceOptions = [];
-  public time: any = {};
-  public timeType: any = {};
   public items = [{
     id: 0,
     monitorPoint: null,
-    mac: '',
+    device: null,
     time: null,
-    formatTime: null,
-    monitorPointName: '',
-    deviceName: '',
-    monitorPointAddress: '',
-    deviceCount: ''
+    deviceOptions: []
   }];
+
+  constructor(
+    public http: HttpClient,
+    public dateSrv: DateService,
+    public router: Router,
+    public msgSrv: NzMessageService
+  ) {
+    this.timeType = this.typeOptions[1];
+    this.query.reportType = this.reportOptions[1].value;
+  }
 
   ngOnInit() {
     this.http.get(environment.SERVER_BASH_URL + 'sensor/all').subscribe((res: any) => {
@@ -62,27 +54,14 @@
     });
   }
 
-  typeChange(searchText) {
-    this.typeOptions.forEach(types => {
-      if (types.value === searchText) {
-        this.timeType = types;
-        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,
-      mac: '',
+      device: null,
       time: null,
-      formatTime: null,
-      monitorPointName: '',
-      deviceName: '',
-      monitorPointAddress: '',
-      deviceCount: ''
+      deviceOptions: []
     });
   }
 
@@ -102,38 +81,16 @@
   }
 
   monitorPointChange(value, i) {
-    this.deviceOptions = [];
+    this.items[i].deviceOptions = [];
+    this.items[i].device = null;
     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) => {
+      this.http.get(environment.SERVER_BASH_URL + 'device/monitorPointId', {params: {monitorPointId: value.id}}).subscribe((res: any) => {
         if (res.code === 0) {
           this.msgSrv.error(res.message);
         } else {
-          this.deviceOptions = res.data;
-          this.items[i].deviceCount = res.data.length;
+          this.items[i].deviceOptions = res.data;
         }
       });
-    } else {
-      this.items[i].monitorPoint = null;
-      this.items[i].mac = '';
-      this.items[i].monitorPointName = '';
-    }
-  }
-
-  deviceChange(value, i) {
-    if (value) {
-      this.deviceOptions.forEach(device => {
-        if (device.mac === value) {
-          this.items[i].deviceName = device.name;
-        }
-      });
-    } else {
-      this.items[i].deviceName = '';
     }
   }
 
@@ -144,16 +101,36 @@
   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) {
-        item.formatTime = this.dateSrv.date_format(item.time, this.time.format);
+        for (var key in item) {
+          if (item[key]) {
+            queryItem[key] = item[key];
+          }
+        }
+        queryItem.monitorPointId = queryItem.monitorPoint.id;
+        queryItem.monitorPointName = queryItem.monitorPoint.name;
+        queryItem.monitorPointAddress = queryItem.monitorPoint.address;
+        delete queryItem.monitorPoint;
+        if (queryItem.device) {
+          queryItem.mac = queryItem.device.mac;
+          queryItem.deviceName = queryItem.device.name;
+          delete queryItem.device;
+        }
+        queryItem.deviceCount = queryItem.deviceOptions.length;
+        delete queryItem.deviceOptions;
+        queryItem.formatTime = this.dateSrv.date_format(queryItem.time, this.timeType.format.toUpperCase());
+        delete queryItem.time;
+        queryItems.push(queryItem);
       } else {
         validate = false;
         break;
       }
     }
-    if (validate && query.type && query.reportType) {
+    if (validate && this.timeType && query.reportType) {
       query.sensors = null;
       if (query.sensorKey && query.sensorKey.length > 0) {
         const sensors = [];
@@ -162,12 +139,11 @@
         });
         query.sensors = JSON.stringify(sensors);
       }
+      query.items = JSON.stringify(queryItems);
       query.timeType = JSON.stringify(this.timeType);
-      query.items = JSON.stringify(this.items);
       this.router.navigate(['report'], {queryParams: query});
     } else {
       this.msgSrv.error('���������������������������������������');
     }
-
   }
 }

--
Gitblit v1.8.0