From 27cd36be226ca2434f06b1ae9e4d43f1fea639ab Mon Sep 17 00:00:00 2001
From: fengxiang <110431245@qq.com>
Date: Mon, 25 Jun 2018 16:40:28 +0800
Subject: [PATCH] 组织配置单位和页面显示

---
 src/app/routes/devices/basic-info/device-edit/device-edit.component.ts |   47 ++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 38 insertions(+), 9 deletions(-)

diff --git a/src/app/routes/devices/basic-info/device-edit/device-edit.component.ts b/src/app/routes/devices/basic-info/device-edit/device-edit.component.ts
index 3fa4dfb..47ec2f0 100644
--- a/src/app/routes/devices/basic-info/device-edit/device-edit.component.ts
+++ b/src/app/routes/devices/basic-info/device-edit/device-edit.component.ts
@@ -3,7 +3,7 @@
 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 { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
 import { DeviceService } from '@business/services/http/device.service';
 import { Component, OnInit } from '@angular/core';
 import { Device} from '@business/entity/data';
@@ -11,7 +11,8 @@
 import { PageBean, ResultBean } from '@business/entity/grid';
 import { _HttpClient } from '@delon/theme';
 import { environment } from '@env/environment';
-import { debug } from 'util';
+import { ExampleService } from '@business/services/util/example.service';
+import { Observable } from 'rxjs/Observable';
 
 @Component({
   selector: 'app-device-edit',
@@ -30,11 +31,17 @@
     private monitorPointService: MonitorPointService,
     private versionService: VersionService,
     private operateUserService: OperateUserService,
-    private  http:_HttpClient
+    private deviceService: DeviceService,
+    private  http: _HttpClient
   ) { }
   data: Device;
+  //������������������
+  originalData: Device = {};
   validateForm: FormGroup;
   ngOnInit() {
+    if (!!this.data) {
+        Object.assign(this.originalData, this.data);
+    }    
     const data = this.data;
     this.monitorPointChange(null);
     this.deviceVersionChange(null);
@@ -43,10 +50,9 @@
     if (this.data.createTime == null) {
         this.data.createTime = new Date().getTime();
     }
-    debugger;
     const validates: Device = {
          name: [data.name, [Validators.required]],
-         mac: [data.mac, [Validators.required]],
+         mac: [data.mac, [Validators.required], [this.macAsyncValidator]],
          deviceVersionId: [data.deviceVersionId],
          monitorPointId: [data.monitorPointId],
          professionId: [data.professionId],
@@ -62,6 +68,30 @@
       validates
     );
   }
+  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 => {
+                    debugger;
+                     if (!!res.code && !!res.data) {
+                         observer.next({ error: true, duplicated: true });
+                     } else {
+                        observer.next(null);
+                     }
+                     observer.complete();
+                }
+            );
+        }
+        
+    });
+  }
   close() {
      this.subject.destroy();
    }
@@ -75,11 +105,10 @@
         ToolsService.markAsDirty(this.validateForm);
     }
    }
-   professionChange(){
-     this.http.get<ResultBean<any[]>>(environment.SERVER_BASH_URL+"profession/getall").subscribe(
+   professionChange() {
+     this.http.get<ResultBean<any[]>>(environment.SERVER_BASH_URL + 'profession/getall').subscribe(
          result => {
-             if(!!result.code){
-                 debugger;
+             if (!!result.code) {
                  this.professions = result.data;
              }
          }

--
Gitblit v1.8.0