fengxiang
2018-07-04 7ca521e4267b987270f6ccbb9a6c076aeb467d96
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
import { CoorPickerService } from 'app/routes/map/coordinates-picker/coordinates-picker.service';
import { AreaNames, MonitorPoint } from '@business/entity/data';
import { Version, ValueTransformer } from '@angular/compiler/src/util';
import { Subject } from 'rxjs/Subject';
import { Component, OnInit } from '@angular/core';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ModalHelper } from '@delon/theme';
import { HttpClient } from '@angular/common/http';
import { environment } from 'environments/environment';
import { VersionService } from '@business/services/http/version.service';
import { Types } from '@business/enum/types.enum';
import { Column, Grid, PageBean, DataType } from '@business/entity/grid';
import { filter } from 'rxjs/operators/filter';
import { MonitorPointEditComponent } from 'app/routes/devices/monitor-point/monitor-point-edit/monitor-point-edit.component';
import { MonitorPointService } from '@business/services/http/monitor-point.service';
import { CoordinatesPickerComponent } from 'app/routes/map/coordinates-picker/coordinates-picker.component';
import { OrganizationService } from '@business/services/http/organization.service';
import { ExampleService } from '@business/services/util/example.service';
 
 
@Component({
  selector: 'app-monitor-point',
  templateUrl: './monitor-point.component.html',
  styles: []
})
export class MonitorPointComponent implements OnInit {
 
  public orgOptions = [];
  private monitorPoint: MonitorPoint;
  grid: Grid<MonitorPoint> = new Grid(null);
  queryMap: { mpointName?: string, organizationId?: number } = { mpointName: null, organizationId: null };
  queryTextStream: Subject<string> = new Subject<string>();
  private initPage() {
    this.monitorPoint = {
      name: {
        text: '名称',
        width: '300px'
      },
      address: {
        text: '地址',
        width: '300px',
        format: (value: any, col: Column, row: any) => {
          value = value == null ? '' : value;
          if (row['areaNames'] != null) {
            return row['areaNames']['provinceName'] 
              + (!!row['areaNames']['cityName'] ? row['areaNames']['cityName'] : '')
              + (!!row['areaNames']['areaName'] ? row['areaNames']['areaName'] : '')
              + (!!row['areaNames']['townName'] ? row['areaNames']['townName'] : '')
              + (!!row['areaNames']['villageName'] ? row['areaNames']['villageName'] : '')
              + value;
          } else {
            return value;
          }
 
        }
      },
      description: {
        text: '描述',
        width: '300px'
      }
    };
    this.grid.title = '监控站点';
    this.grid.setColumns(this.monitorPoint);
    this.grid.pageSize = 10;
  }
  constructor(
    private coorPickerService: CoorPickerService,
    private monitorPointService: MonitorPointService,
    private confirmServ: NzModalService,
    public msgSrv: NzMessageService,
    private modalHelper: ModalHelper,
    private organizationService: OrganizationService
  ) { }
 
  ngOnInit() {
    this.initPage();
    this.queryTextStream
      .debounceTime(500)
      .distinctUntilChanged()
      .subscribe(queryText => {
        this.load();
      });
    this.orgSelectChange();
  }
 public setOrganizationId(orgId) {
    this.queryMap.organizationId = orgId;
    this.load();
  }
  queryTextChanged($event) {
    this.queryTextStream.next(this.queryMap.mpointName);
  }
  load(reload: boolean = false) {
    if (reload) {
      this.grid.pageIndex = 1;
    }
    // 延时加载避免ExpressionChangedAfterItHasBeenCheckedError
    setTimeout(() => {
      this.grid.loading = true;
    }, 1);
    const example = new ExampleService();
    const organizationId = !!this.queryMap.organizationId ? this.queryMap.organizationId : null;
    const mpointName = !!this.queryMap.mpointName ? '%' + this.queryMap.mpointName + '%' : null;
    example.or()
    .andEqualTo({name: 'organizationId', value: organizationId })
    .andLike({name: 'name', value: mpointName  });
    this.monitorPointService.getPageByExample(this.grid, example).subscribe(
      (res: PageBean) => {
        this.grid.loading = true;
        if (res != null && res.data != null) {
          this.grid.initData(res);
          this.grid.refreshStatus();
          setTimeout(() => {
            this.grid.loading = false;
          }, 1);
        }
      }
    );
  }
 
  // rowData为null时,为新增
  addOrModify(d) {
    const data = {};
    if (d != null) {
      Object.assign(data, d);
    }
    const configMap = this.queryMap;
    this.modalHelper.static(MonitorPointEditComponent, { data, configMap }).subscribe(
      (ret: { data: any, close: Function }) => {
        // 修改状态
        if (ret.data['index'] != null) {
          const index: number = ret.data['index'];
          const origData = this.grid.data[index];
          const isModified = Object.keys(origData).some(
            (key: string) => {
              return ret.data[key] !== origData[key];
            }
          );
          // 未作修改
          if (!isModified) {
            ret.close();
            this.msgSrv.success(this.grid.title + '未作任何修改!');
            return;
          }
        }
        this.monitorPointService.save(ret.data).subscribe(
          (res: any) => {
            if (res.code === 1) {
              this.load();
              ret.close();
              this.msgSrv.success(this.grid.title + '保存成功!');
            }
          }
        );
      });
  }
 
  delete(...id: number[]) {
    this.monitorPointService.delete(...id).subscribe(
      (res: any) => {
        if (res.code === 1) {
          this.load();
          this.msgSrv.success(this.grid.title + '删除成功!');
        }
      }
    );
  }
 
  deleteSelected() {
    this.confirmServ.confirm({
      title: '批量删除',
      content: '注意:数据一旦删除,将不可恢复!',
      okText: '确定',
      cancelText: '取消'
    }).on('onOk', () => {
      if (this.grid.selectedIndexs != null && this.grid.selectedIndexs.length > 0) {
        const ids = this.grid.selectedIndexs.map(
          (index: number) => {
            const id = this.grid.data[index].id;
            return Number.parseInt(id);
          }
        );
        this.delete(...ids);
      }
    });
  }
  sort(field: string, value: string) {
    // 删除当前field
    this.grid.sorts = this.grid.sorts.filter(
      (fn: string) => {
        return fn !== field;
      }
    );
    // 如果value不为null,在排序数组最后加上filed
    if (value != null) {
      this.grid.sorts.push(field);
    }
    this.load();
  }
  configCoord(record: MonitorPoint): void {
    Object.assign(this.coorPickerService.data, record);
    const _data = this.coorPickerService.data;
    const areaNames = record.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 += !!record.address ? record.address : '';
    }
    this.coorPickerService.data.address = adress;
    this.coorPickerService.data['describe'] = '监控站点名称';
    this.modalHelper.static(CoordinatesPickerComponent).subscribe(
      (staticComp) => {
        const data: MonitorPoint = {
          id: record.id,
          longitude: _data.longitude,
          latitude: _data.latitude,
        };
        this.monitorPointService.save(data).subscribe(
          (res: any) => {
            if (res.code === 1) {
              this.load();
              this.msgSrv.success('坐标配置成功!');
            }
          }
        );
 
      }
    );
  }
  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;
           }
      }
   );
  }
}