fengxiang
2018-07-04 7ca521e4267b987270f6ccbb9a6c076aeb467d96
src/app/routes/devices/version/version-edit/version-edit.component.ts
@@ -1,8 +1,11 @@
import { DeviceVersion } from '@business/entity/data';
import { Component, OnInit } from '@angular/core';
import { NzMessageService, NzModalSubject } from 'ng-zorro-antd';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { DeviceVersion } from 'app/routes/devices/version/version.component';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { _HttpClient } from '@delon/theme';
import { ResultBean } from '@business/entity/grid';
import { Observable } from 'rxjs/Observable';
import { ToolsService } from '@business/services/util/tools.service';
@Component({
  selector: 'app-version-edit',
  templateUrl: './version-edit.component.html',
@@ -10,26 +13,47 @@
})
export class VersionEditComponent implements OnInit {
 
  private originalData: DeviceVersion = {};
  cols: DeviceVersion;
  data: DeviceVersion;
  isSaving = false;
  validateForm: FormGroup;
  constructor(    
    private subject: NzModalSubject,
    private formBuilder: FormBuilder
    private formBuilder: FormBuilder,
    private http: _HttpClient
    ) { }
  ngOnInit() {
     if (this.data.createTime == null) {
      this.data.createTime = new Date().getTime();
     }
     if (!this.data.version) {
       this.setMaxVersionNo();
     }
     const validates: DeviceVersion = {
          name: [this.data.name, [Validators.required] ],
          version: [this.data.version == null ? 1 : this.data.version, [Validators.required] ],
          version: [this.data.version, [Validators.required], [this.versionAsyncValidator] ],
          createTime: [this.data.createTime, [Validators.required] ],
          description: [this.data.description]
     };
     Object.assign(this.originalData, this.data);
     this.validateForm = this.formBuilder.group(
      validates
     );
  }
  private setMaxVersionNo() {
      this.isSaving = true;
      this.http.get('device-version/get-maxverno').subscribe(
         (res: ResultBean<number>) => {
              if (!!res.code && res.data) {
                 ToolsService.setValueToControl(this.validateForm, 'version', res.data + 1);
              }else {
                this.data.version = 1;
              }
          this.isSaving = false;
         }
      );
  }
  close() {
       this.subject.destroy();
@@ -57,10 +81,37 @@
              }
        } );
        this.subject.next( this );
      }else {
      } else {
        this.validate(); 
      }
  }
  versionAsyncValidator = (control: FormControl): any => {
    return Observable.create(observer => {
        if (!!control && !!control.value) {
              // 编辑状态,version未改变
              if (!!this.originalData && this.originalData.version === control.value) {
                observer.next(null);
                observer.complete();
            } else {
                this.isSaving = true;
                this.http.get('device-version/get-byversion', {version: control.value} ).subscribe(
                  (res: ResultBean<any>) => {
                      if (!!res.code && !!res.data) {
                        observer.next({ error: true, duplicated: true });
                      } else {
                        observer.next(null);
                      }
                      observer.complete();
                      this.isSaving = false;
                  }
                );
            }
        }else {
          observer.next(null);
          observer.complete();
        }
    });
  }
  validate() {
     for (const i in this.validateForm.controls) {
       this.validateForm.controls[ i ].markAsDirty();