fengxiang
2018-07-31 eef69fa1194f6ab215b08a8baaf402d0539362cc
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
import { ToolsService } from '@business/services/util/tools.service';
import { OperateUserService } from '@business/services/http/operate-user.service';
import { VersionService } from '@business/services/http/version.service';
import { MonitorPointService } from '@business/services/http/monitor-point.service';
import { NzModalSubject } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { DeviceService } from '@business/services/http/device.service';
import { Component, OnInit } from '@angular/core';
import { Device, MonitorPoint} from '@business/entity/data';
import { PageBean, ResultBean } from '@business/entity/grid';
import { _HttpClient } from '@delon/theme';
import { environment } from '@env/environment';
import { ExampleService } from '@business/services/util/example.service';
import { Observable } from 'rxjs/Observable';
import { OrganizationService } from '@business/services/http/organization.service';
import { debug } from 'util';
 
@Component({
  selector: 'app-device-edit',
  templateUrl: './device-edit.component.html',
  styles: []
})
export class DeviceEditComponent implements OnInit {
  public isEditState = true;
  public monitorPoints: any [] = [];
  public professions: any [] = [];
  public deviceVersions: any [] = [];
  public operateUsers: any [] = [];
  public isSaving = false;
  public configMap: {orgId?: number, mpointId?: number, mpoint?: MonitorPoint} = {orgId: null};
  public orgOptions = [];
  constructor(
    private subject: NzModalSubject,
    private formBuilder: FormBuilder,
    private monitorPointService: MonitorPointService,
    private versionService: VersionService,
    private operateUserService: OperateUserService,
    private deviceService: DeviceService,
    private  http: _HttpClient,
    private organizationService: OrganizationService
  ) { }
  data: Device;
  // 原始数据记录
  originalData: Device = {};
  orgId: number = null;
  validateForm: FormGroup;
  ngOnInit() {
    //   console.log(this.configMap);
    if (!!this.data) {
        Object.assign(this.originalData, this.data);
    }    
    const data = this.data;
    this.monitorPointChange(null);
    this.deviceVersionChange(null);
    this.operateUserChange(null);
    this.professionChange();
    if (this.data.createTime == null) {
        this.data.createTime = new Date().getTime();
    }
    if (!!this.configMap.mpointId 
        && !!this.data.monitorPoint
        && !data.longitude
        && !data.latitude) {
            data.longitude = this.data.monitorPoint.longitude;
            data.latitude = this.data.monitorPoint.latitude;
    }
    data.monitorPointId = !!data.monitorPointId ? data.monitorPointId : this.configMap.mpointId;
    const validates: Device = {
         name: [data.name, [Validators.required]],
         mac: [data.mac, [Validators.required], [this.macAsyncValidator]],
         deviceVersionId: [data.deviceVersionId, [Validators.required]],
         monitorPointId: [data.monitorPointId, [Validators.required]],
         professionId: [data.professionId],
         operateUserId: [data.operateUserId],
         address: [data.address],
         id: [data.id],
         longitude: [data.longitude],
         latitude: [data.latitude],
         createTime: [data.createTime],
         installTime: [data.installTime]
    };
    this.validateForm = this.formBuilder.group(
      validates
    );
    if(!this.isEditState){
        this.validateForm.disable();
    }
    this.validateForm
    this.orgSelectChange();
    const control = this.validateForm.controls['monitorPointId'];
    control.valueChanges.subscribe(value => {
        const mpoint = ToolsService.getObjById<MonitorPoint>(value, this.monitorPoints);
        this.resetCoor(mpoint);
    });
  }
  macAsyncValidator = (control: FormControl): any => {
    return Observable.create(observer => {
        // 编辑状态,mac未改变
        if (!!this.originalData && this.originalData.mac === control.value) {
            observer.next(null);
            observer.complete();
        } else {
            const exampleService = new ExampleService();
            exampleService.or().andEqualTo({name: 'mac', value: control.value});
            this.deviceService.countByExample(exampleService).subscribe(
                res => {
                     if (!!res.code && !!res.data) {
                         observer.next({ error: true, duplicated: true });
                     } else {
                        observer.next(null);
                     }
                     observer.complete();
                }
            );
        }
        
    });
  }
  close() {
     this.subject.destroy();
   }
   save($event, value, valid) {
    const _prevent =  !!$event ? $event.preventDefault() : null ;
    if (valid) {
      this.isSaving = true;
      this.data = value;
      this.subject.next( this );
    } else {
        ToolsService.markAsDirty(this.validateForm);
    }
   }
   professionChange() {
     this.http.get<ResultBean<any[]>>(environment.SERVER_BASH_URL + 'profession/getall').subscribe(
         result => {
             if (!!result.code) {
                 this.professions = result.data;
             }
         }
     );
   }
   monitorPointChange(text?: string) {
    const pageBean: PageBean = {pageIndex: 0, pageSize: 100};
    const orgId = this.configMap.orgId;
    const example = new ExampleService();
    text = !!text && !!text.trim() ? '%' + text + '%' : null;
    example.or()
    .andEqualTo({name: 'organizationId', value: this.configMap.orgId})
    .andLike({name: 'name', value: text});
    this.monitorPointService.getPageByExample(pageBean, example).subscribe(
      (res: PageBean) => {
           if (res != null && res.data != null) {
               this.monitorPoints = res.data;
           }
           const monitorPoint = this.data.monitorPoint;
           if (monitorPoint != null && text == null) {
               const hasSelectedValue = this.monitorPoints.some(
                   (item: any) => {
                      return item.id === monitorPoint.id;
                   }
               );
               if ( !hasSelectedValue ) {
                  this.monitorPoints.push(monitorPoint);
               }
           }
      }
   );
   }
   deviceVersionChange(text) {
    const pageBean: PageBean = {pageIndex: 0, pageSize: 20};
    this.versionService.getPagingList(pageBean, text).subscribe(
      (res: PageBean) => {
           if (res != null && res.data != null) {
               this.deviceVersions = res.data;
           }
           const deviceVersion = this.data.deviceVersion;
           if (deviceVersion != null && text == null) {
               const hasSelectedValue = this.deviceVersions.some(
                   (item: any) => {
                      return item.id === deviceVersion.id;
                   }
               );
               if ( !hasSelectedValue ) {
                  this.deviceVersions.push(deviceVersion);
               }
           }
      }
   );
   }
   operateUserChange(text) {
    const pageBean: PageBean = {pageIndex: 0, pageSize: 20};
    this.operateUserService.getPagingList(pageBean, text).subscribe(
      (res: PageBean) => {
           if (res != null && res.data != null) {
               this.operateUsers = res.data;
           }
           const operateUser = this.data.operateUser;
           if (operateUser != null && text == null) {
               const hasSelectedValue = this.operateUsers.some(
                   (item: any) => {
                      return item.id === operateUser.id;
                   }
               );
               if ( !hasSelectedValue ) {
                  this.operateUsers.push(operateUser);
               }
           }
      }
   );
   }
   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;
           }
      }
   );
  }
  setOrgId(orgId: number) {
        this.configMap.mpointId = null;
        this.monitorPointChange();
        this.clearMpoint(); 
  }
  clearMpoint() {
     ToolsService.setValueToControl(this.validateForm, 'monitorPointId', null);
     this.resetCoor();
  }
  resetCoor(mpoint?: MonitorPoint) {
    const longitude =  !!mpoint ?  mpoint.longitude : null;
    const latitude =  !!mpoint ?  mpoint.latitude : null;
    ToolsService.setValueToControl(this.validateForm, 'longitude', longitude);
    ToolsService.setValueToControl(this.validateForm, 'latitude', latitude);
  }
}