fengxiang
2018-01-29 3bbe9a88d9ab747ee2e4f01128547efd1fc20038
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
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 } from '@angular/forms';
import { DeviceService } from '@business/services/http/device.service';
import { Component, OnInit } from '@angular/core';
import { Device} from '@business/entity/data';
import { _Validators } from '@delon/abc';
import { PageBean } from '@business/entity/grid';
 
@Component({
  selector: 'app-device-edit',
  templateUrl: './device-edit.component.html',
  styles: []
})
export class DeviceEditComponent implements OnInit {
  public monitorPoints:any [] = [];
  public deviceVersions:any [] = [];
  public operateUsers:any [] = [];
  public isSaving = false;
  constructor(
    private subject: NzModalSubject,
    private formBuilder: FormBuilder,
    private monitorPointService:MonitorPointService,
    private versionService:VersionService,
    private operateUserService:OperateUserService
  ) { }
  data:Device;
  validateForm:FormGroup;
  ngOnInit() {
    const data = this.data;
    this.monitorPointChange(null);
    this.deviceVersionChange(null);
    this.operateUserChange(null);
    if (this.data.createTime == null) {
        this.data.createTime = new Date().getTime();
    }
    const validates:Device = {
         name:[data.name,[Validators.required]],
         mac:[data.mac],
         deviceVersionId:[data.deviceVersionId],
         monitorPointId:[data.monitorPointId],
         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
    );
  }
  close(){
     this.subject.destroy();
   }
   save($event, value, valid){
    $event.preventDefault();
    if(valid){
      this.isSaving = true;
      this.data = value;
      this.subject.next( this );
    }else{
        ToolsService.markAsDirty(this.validateForm);
    }
   }
   monitorPointChange(text){
    const pageBean: PageBean = {pageIndex: 0, pageSize: 20};
    this.monitorPointService.getPagingList(pageBean, text).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.monitorPoints.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.monitorPoints.push(operateUser);
               }                     
           }
      }
   );
   }
}