fengxiang
2018-07-06 309d1f9d649daa08bb9b068af014749f6d4a5bce
src/app/routes/devices/basic-info/device-edit/device-edit.component.ts
@@ -6,13 +6,13 @@
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';
import { _Validators } from '@delon/abc';
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';
@Component({
  selector: 'app-device-edit',
@@ -25,6 +25,8 @@
  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,
@@ -32,13 +34,16 @@
    private versionService: VersionService,
    private operateUserService: OperateUserService,
    private deviceService: DeviceService,
    private  http: _HttpClient
    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);
    }    
@@ -50,11 +55,19 @@
    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],
         monitorPointId: [data.monitorPointId],
         deviceVersionId: [data.deviceVersionId, [Validators.required]],
         monitorPointId: [data.monitorPointId, [Validators.required]],
         professionId: [data.professionId],
         operateUserId: [data.operateUserId],
         address: [data.address],
@@ -67,6 +80,12 @@
    this.validateForm = this.formBuilder.group(
      validates
    );
    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 => {
@@ -79,7 +98,6 @@
            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 {
@@ -96,7 +114,7 @@
     this.subject.destroy();
   }
   save($event, value, valid) {
    $event.preventDefault();
    const _prevent =  !!$event ? $event.preventDefault() : null ;
    if (valid) {
      this.isSaving = true;
      this.data = value;
@@ -114,9 +132,15 @@
         }
     );
   }
   monitorPointChange(text) {
    const pageBean: PageBean = {pageIndex: 0, pageSize: 20};
    this.monitorPointService.getPagingList(pageBean, text).subscribe(
   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;
@@ -177,4 +201,29 @@
      }
   );
   }
   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);
  }
}