fengxiang
2018-08-03 4c99ab95e0449348d938ec0ceb949bceafb0cfda
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { MonitorPointService } from '@business/services/http/monitor-point.service';
import { CoorPicker, Device } from '@business/entity/data';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { PageBean, ResultBean } from '@business/entity/grid';
import { DeviceService } from '@business/services/http/device.service';
import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
import { SimpleTableColumn, SimpleTableComponent } from '@delon/abc';
import { Subject } from 'rxjs/Subject';
import { CoordinatesPickerComponent } from '../../map/coordinates-picker/coordinates-picker.component';
import { CoorPickerService } from '../../map/coordinates-picker/coordinates-picker.service';
import { OrganizationService } from '@business/services/http/organization.service';
import { ExampleService, Criteria } from '@business/services/util/example.service';
import { DeviceEditComponent } from '../../devices/basic-info/device-edit/device-edit.component';
import { DeviceControllerService } from '../device-controller/device-controller.service';
 
@Component({
  selector: 'app-device-status',
  templateUrl: './device-status.component.html',
})
export class DeviceStatusComponent implements OnInit {
         private getBadge(id): string  {
             return `<div>${id}</div>`;
         }
        columns: SimpleTableColumn[] = [
          { title: '编号', index: 'id', type: 'checkbox' },
          { title: '名称', index: 'name' },
          { title: 'sim', index: 'mac' },
          {
            title: '状态',
            render: 'state-badge'
          },
          { title: '型号', index: 'deviceVersion.name' },
          { title: '监控站点', index: 'monitorPoint.name' },
          { title: '维护人', index: 'operateUser.name' },
          { title: '生产时间', width: '100px', type: 'date', index: 'createTime' },
          { title: '安装时间', width: '100px', type: 'date', index: 'installTime' },
          {
            title: '操作区',
            buttons: [
              {
                text: '详细',
                type: 'none',
                click: (record: any) => this.addOrModify(record)
              },
              {
                text: '数据',
                type: 'none',
                click: (record: any) => {
                  this.deviceControllerService.data = record;
                  this.deviceControllerService.incumbent = 'data';
                }
              }
            ]
          }
        ];
        queryTextStream: Subject<string> = new Subject<string>();
        public queryMap: {orgId?: number, mpointId?: number, devMacOrName?: string}
        = {orgId: null, mpointId: null, devMacOrName: ''};
        extraParams = { queryParams: null };
        public orgOptions = [];
        public monitorPoints = [];
        constructor(
          private monitorPointService: MonitorPointService,
          private deviceService: DeviceService,
          private confirmServ: NzModalService,
          public msgSrv: NzMessageService,
          private modalHelper: ModalHelper,
          private coorPickerService: CoorPickerService,
          private organizationService: OrganizationService,
          private http: _HttpClient,
          private deviceControllerService: DeviceControllerService,
        ) { }
      
        ngOnInit() {
          this.queryTextStream
            .debounceTime(900)
            .distinctUntilChanged()
            .subscribe(value => {
              this.queryMap.devMacOrName = value;
              this.load();
            });
            this.orgSelectChange();
        }
        get listUrl() {
          return this.deviceService.getListUrl();
        }
        queryText: string;
        selectedRows: any[] = [];
        checkboxChange(list: any[]) {
          this.selectedRows = list;
        }
        deleteSelected() {
          this.confirmServ.confirm({
            title: '批量删除',
            content: '注意:数据一旦删除,将不可恢复!',
            okText: '确定',
            cancelText: '取消'
          }).on('onOk', () => {
            if (this.selectedRows != null && this.selectedRows.length > 0) {
              const ids = this.selectedRows.map(
                (row: any) => {
      
                  return Number.parseInt(row.id);
                }
              );
              this.delete(...ids);
            }
          });
        }
        load() {
          const example  = this.initQueryExample();
          this.extraParams.queryParams = example.getSqlParam();
          this.selectedRows = [];
          this.deviceService.countByExample(example).subscribe(
            res => {
                 if (!!res && !!res.code) {
                      const count = res.data;
                      const pages = Math.ceil(count / this.simpleTable.ps);
                      const pi = this.simpleTable.pi > pages ? pages : this.simpleTable.pi;
                      this.simpleTable.load(pi);
                 }
            }
          );
          
        }
        private initQueryExample() {
          const example = new ExampleService();
          const orgId = !!this.queryMap.orgId ? this.queryMap.orgId : null;
          const mpointId = !!this.queryMap.mpointId ? this.queryMap.mpointId : null;
          const devMacOrName = !!this.queryMap.devMacOrName && !!(<string>this.queryMap.devMacOrName).trim() ? this.queryMap.devMacOrName : null;
          let  criWithMac: Criteria = null;
          let  criWithName: Criteria  = null;
          if (!!devMacOrName) {
            criWithName = example.or().andLike({name: 'name', value: '%' + devMacOrName + '%'});
            criWithMac = example.or().andLike({name: 'mac', value: '%' + devMacOrName + '%'});
          }
          if (!!mpointId) {
              if (!!devMacOrName) {
                criWithName.andEqualTo({name: 'monitorPointId', value: mpointId});
                criWithMac.andEqualTo({name: 'monitorPointId', value: mpointId});
              }else {
                example.or().andEqualTo({name: 'monitorPointId', value: mpointId});
              }
          } else if (!!orgId) {
            if (!!devMacOrName) {
              criWithName.andCondition(`monitor_point_id in (select id from monitor_point where organization_id = ${orgId})`);
              criWithMac.andCondition(`monitor_point_id in (select id from monitor_point where organization_id = ${orgId})`);
            }else {
              example.or().andCondition(`monitor_point_id in (select id from monitor_point where organization_id = ${orgId})`);
            }
          }
          return example;
        }
        delete(...id: number[]) {
          this.deviceService.delete(...id).subscribe(
            (res: any) => {
              if (res.code === 1) {
                this.load();
                this.msgSrv.success('删除成功!');
              }
            }
          );
        }
        @ViewChild('simpleTable') simpleTable: SimpleTableComponent;
        queryTextChanged(event) {
          this.queryTextStream.next(this.queryText);
        }
        addOrModify(d) {
          const data = {};
          if (d != null) {  
            Object.assign(data, d);
          }
          let configMap = {};
          configMap = Object.assign(configMap, this.queryMap);
          // Object.assign(configMap, this.queryMap);
          if (!!configMap['mpointId'] && !d['monitorPoint']) {
            data['monitorPoint'] = this.getMonitorPoint(configMap['mpointId']);
          }
          const isEditState = false;
          this.modalHelper.static(DeviceEditComponent, { data,configMap,isEditState }).subscribe(
            (ret: { data: any, close: Function }) => {
              // // 修改状态
              // if (ret.data['id'] != null) {
              //   const origData = d;
              //   const isModified = Object.keys(ret.data).some(
              //     (key: string) => {
              //       return ret.data[key] !== origData[key];
              //     }
              //   );
              //   // 未作修改
              //   if (!isModified) {
              //     ret.close();
              //     this.msgSrv.success('数据未作任何修改!');
              //     return;
              //   }
              // }
              // this.deviceService.save(ret.data).subscribe(
              //   (res: any) => {
              //     if (res.code === 1) {
              //       this.load();
              //       ret.close();
              //       this.msgSrv.success('数据保存成功!');
              //     }
              //   }
              // );
            });
        }
        private getMonitorPoint(mpointId: number) {
           return this.monitorPoints.find(
              mpoint => mpoint.id === mpointId
           );
        }
        configCoord(record: Device): void {
           // 复制数据放在此处,放在后面,adress会被覆盖
           Object.assign(this.coorPickerService.data, record);
           this.coorPickerService.data['describe'] = '设备名称';
            // 当前 坐标未设置 取监控点地址
            if ((!record.latitude || !record.longitude) && !!record.monitorPointId) {
                  this.monitorPointService.getEntity(record.monitorPointId).subscribe(
                    res => {
                      if (res != null && res.code === 1 && res.data != null) {
                          const areaNames = res.data.areaNames;
                          let adress = '';
                          if (areaNames != null) {
                            adress += !!areaNames.provinceName ? areaNames.provinceName : '';
                            adress += ' ';
                            adress += !!areaNames.cityName ? areaNames.cityName : '';
                            adress += ' ';
                            adress += !!areaNames.areaName ? areaNames.areaName : '';
                            adress += ' ';
                            adress += !!res.data.address ? res.data.address : '';                  
                          }
                          this.coorPickerService.data.address = adress;
                          this.openMap(record);              
                      }
                    }
              );
            } else {
              this.openMap(record);
            }
        }
        private openMap(record: Device) {
          const _data = this.coorPickerService.data;    
          this.modalHelper.static(CoordinatesPickerComponent).subscribe(
            (staticComp) => {
                   const data: Device = {
                      id: record.id,
                      mac: record.mac,
                      longitude: _data.longitude,
                      latitude: _data.latitude,
                   };
                   this.deviceService.save(data).subscribe(
                    (resp: any) => {
                      if (resp.code === 1) {
                        this.load();
                        this.msgSrv.success('坐标配置成功!');
                      }
                    }
                   );                     
            }
         );
        }
        public setOrgId(orgId) {
          this.queryMap.orgId = orgId;
          this.queryMap.mpointId = null;
          // this.queryMap.devMacOrName = null;
          this.monitorPointChange();
          this.load();
        }
        public setMpointId(mpointId) {
          this.queryMap.mpointId = mpointId;
          this.load();
        }
        orgSelectChange(text?: string) {
          const pageBean: PageBean = {pageIndex: 0, pageSize: 20};
          this.organizationService.getPagingList(pageBean, text).subscribe(
            (res: PageBean) => {
                 if (res != null && res.data != null) {               
                     this.orgOptions = res.data;
                 }
            }
         );
        }
          monitorPointChange(text?: string) {
            const pageBean: PageBean = {pageIndex: 0, pageSize: 20};
            const example = new ExampleService();
            const organizationId = !!this.queryMap.orgId ? this.queryMap.orgId : null;
            const mpointName = !!text ? '%' + text + '%' : null;
            example.or()
            .andEqualTo({name: 'organizationId', value: organizationId })
            .andLike({name: 'name', value: mpointName  });
            this.monitorPointService.getPageByExample(pageBean, example).subscribe(
              (res: PageBean) => {
                  if (res != null && res.data != null) {
                      this.monitorPoints = res.data;
                  }
              }
          );
          }
      }