fengxiang
2018-07-06 309d1f9d649daa08bb9b068af014749f6d4a5bce
src/app/routes/devices/version/version.component.ts
@@ -1,4 +1,19 @@
import { VersionSensorConfigComponent } from './version-sensor-config/version-sensor-config.component';
import { DataType } from '@business/entity/grid';
import { DeviceVersion, Sensor } from '@business/entity/data';
import { Version } from '@angular/compiler/src/util';
import { Subject } from 'rxjs/Subject';
import { ToolsService } from '@business/services/util/tools.service';
import { Component, OnInit } from '@angular/core';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ModalHelper } from '@delon/theme';
import { HttpClient } from '@angular/common/http';
import { DateService } from '@business/services/util/date.service';
import { VersionService } from '@business/services/http/version.service';
import { Types } from '@business/enum/types.enum';
import { Column, Grid, PageBean } from '@business/entity/grid';
import { VersionEditComponent } from 'app/routes/devices/version/version-edit/version-edit.component';
@Component({
  selector: 'app-version',
@@ -6,10 +21,192 @@
  styles: []
})
export class VersionComponent implements OnInit {
  constructor() { }
  private version: DeviceVersion;
  grid: Grid<DeviceVersion> = new Grid(null);
  queryMap = { text: '请输入名称或型号', value: ''};
  queryTextStream: Subject<string> = new Subject<string>();
    private initPage() {
    this.version = {
       name: {
        text: '名称',
        width: '18%'
       },
       version: {
        text: '型号',
        width: '18%',
        isSort: true
      },
      createTime: {
          text: '创建时间',
          width: '18%',
          type: {
            name: Types.Date,
            format: 'YYYY-MM-DD HH:mm:ss'
          },
          isSort: true
      },
      description: {
          text: '备注',
          width: '18%'
      }
    };
    this.grid.title = '设备型号';
    this.grid.setColumns(this.version);
    this.grid.pageSize = 10;
  }
  constructor(
    private versionService: VersionService,
    private confirmServ: NzModalService,
    public msgSrv: NzMessageService,
    private modalHelper: ModalHelper,
  ) {}
  ngOnInit() {
    this.initPage();
    this.queryTextStream
    .debounceTime(500)
    .distinctUntilChanged()
    .subscribe(queryText => {
        this.load();
    });
  }
  queryTextChanged($event) {
      this.queryTextStream.next(this.queryMap.value);
  }
  load(reload: boolean = false) {
    if (reload) {
      this.grid.pageIndex = 1 ;
    }
    // 延时加载避免ExpressionChangedAfterItHasBeenCheckedError
    setTimeout(() => {
      this.grid.loading = true;
     }, 1);
    this.versionService.getPagingList(this.grid, this.queryMap.value).subscribe(
       (res: PageBean) => {
            this.grid.loading = true;
            if (res != null && res.data != null) {
              this.grid.initData(res);
              this.grid.refreshStatus();
              setTimeout(() => {
                this.grid.loading = false;
               }, 1);
            }
       }
    );
  }
// rowData为null时,为新增
  addOrModify(data) {
    if ( data == null) {
       data = {};
    }
    const cols = this.version;
    this.modalHelper.static(VersionEditComponent, { cols , data }).subscribe(
      ( ret: { data: any, close: Function} ) => {
      this.versionService.save(ret.data).subscribe(
         ( res: any) => {
            if (res.code === 1) {
              this.load();
              ret.close();
              this.msgSrv.success(this.grid.title + '保存成功!');
            }
         }
      );
    });
  }
  delete(...id: number[]) {
    this.versionService.delete( ...id ).subscribe(
      ( res: any) => {
         if (res.code === 1) {
           this.load();
           this.msgSrv.success(this.grid.title + '删除成功!');
         }
      }
   );
  }
   deleteSelected() {
    this.confirmServ.confirm({
      title: '批量删除',
      content: '注意:数据一旦删除,将不可恢复!',
      okText: '确定',
      cancelText: '取消'
    }).on('onOk', () => {
       if (this.grid.selectedIndexs != null && this.grid.selectedIndexs.length > 0) {
          const ids = this.grid.selectedIndexs.map(
              (index: number) => {
                  const id = this.grid.data[index].id;
                  return Number.parseInt(id);
              }
          );
          this.delete( ...ids );
       }
    });
  }
  sort(field: string, value: string) {
    // 删除当前field
    this.grid.sorts = this.grid.sorts.filter(
       (fn: string) => {
          return fn !== field;
       }
     );
     // 如果value不为null,在排序数组最后加上filed
     if ( value != null ) {
        this.grid.sorts.push(field);
     }
     this.load();
  }
    configSensor(data) {
     const deviceVersionId = data.id;
     this.versionService.getSensorIds(deviceVersionId).subscribe(
        (selectedSensorIds: number[]) => {
          this.modalHelper.static(VersionSensorConfigComponent, { deviceVersionId, selectedSensorIds }).subscribe(
            ( ret: { grid: Grid<Sensor>, close: Function} ) => {
            // 获取选中id
            const selectedIds = ret.grid.data.filter(
              (r: any) => {
                 return r['checked'] === true;
              }
            ).map(
              (r: any) => {
                return r['id'];
             }
            );
            // 长度不一样,已经修改
            let isModified = selectedIds.length !== selectedSensorIds.length;
            // 长度一样再比较内容
            if (!isModified) {
              isModified = !selectedIds.every(
                (id: number) => {
                    // console.log('id:' + id);
                    const result = selectedSensorIds.some(
                      (sid: number) => {
                          return id === sid ;
                      }
                     );
                    // console.log('result:' + result);
                    return result;
                }
               );
            }
            if ( !isModified ) {
              this.msgSrv.success(this.grid.title + '配置未修改!');
              ret.close();
              return ;
            }
            this.versionService.versionSensorConfig(deviceVersionId, selectedIds).subscribe(
               ( res: any) => {
                  if (res.code === 1) {
                    ret.close();
                    this.msgSrv.success(this.grid.title + '配置成功!');
                  }
               }
            );
          });
        }
     );
    }
}