fengxiang
2018-01-29 3bbe9a88d9ab747ee2e4f01128547efd1fc20038
src/app/routes/systems/organization/organization.component.ts
@@ -1,36 +1,7 @@
import { DataType, AreaNames } from './../../../core/entity/grid';
import { Version, ValueTransformer } from '@angular/compiler/src/util';
import { Subject } from 'rxjs/Subject';
import { ToolsService } from '@core/services/tools.service';
import { OrganizationService } from '@business/services/http/organization.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 { environment } from '../../../../environments/environment';
import { DateService } from '@core/services/date.service';
import { VersionService } from 'app/routes/devices/version/version.service';
import { Types } from '@core/enum/types.enum';
import { Column, Grid, PageBean } from '@core/entity/grid';
import { filter } from 'rxjs/operators/filter';
import { OrganizationEditComponent } from './organization-edit/organization-edit.component';
import { OrganizationService } from 'app/routes/systems/organization/organization.service';
export interface Organization {
  address?: Column|any;
  areaCode?: Column|any;
  cityCode?: Column|any;
  createTime?: Column|any;
  description?: Column|any;
  email?: Column|any;
  expireTime?: Column|any;
  id?: Column|any;
  isDelete?: Column|any;
  name?: Column|any;
  provinceCode?: Column|any;
  rank?: Column|any;
  telephone?: Column|any;
  areaNames?: AreaNames|any ;
}
@Component({
  selector: 'app-organization',
@@ -38,198 +9,9 @@
  styles: []
})
export class OrganizationComponent implements OnInit {
  private organization: Organization;
  grid: Grid<Organization> = new Grid(null);
  queryMap = { text: '请输入名称', value: ''};
  queryTextStream: Subject<string> = new Subject<string>();
    private initPage() {
    this.organization =  {
      name: {
        text: '名称',
        width: '120px'
      },
      rank: {
        text: '级别',
        width: '80px',
        format: (value: any, col: Column, row: any) => {
          const item = [
            { value: 0, label: '企业' },
            { value: 1, label: '乡镇科级' },
            { value: 2, label: '县处级' },
            { value: 3, label: '司厅局级' },
            { value: 4, label: '省部级' },
            { value: 5, label: '国家级' }
          ].filter( (rankItem: {value: number, label: string }) => {
            return rankItem.value === value;
          });
          return item != null && item.length === 1 ? item[0].label : '';
        }
      },
      telephone: {
        text: '电话',
        width: '180px'
      },
      address: {
        text: '地址',
        width: '300px',
        format: (value: any, col: Column, row: any) => {
              value = value == null ? '' : value ;
              if (row['areaNames'] != null) {
                return row['areaNames']['provinceName'] + row['areaNames']['cityName'] + row['areaNames']['areaName'] + value;
              } else {
                return value;
              }
        }
      },
      createTime: {
        text: '创建时间',
        width: '100px',
        type: {
         name: Types.Date,
         format: 'YYYY-MM-DD HH:mm:ss'
        },
         isSort: true
      },
      expireTime: {
        text: '到期时间',
        width: '100px',
        type: {
         name: Types.Date,
         format: 'YYYY-MM-DD HH:mm:ss'
        },
         isSort: true
      }
    };
    this.grid.title = '设备型号';
    this.grid.setColumns(this.organization);
    this.grid.pageSize = 10;
  ngOnInit(): void {
  }
  constructor(
    private organizationService: OrganizationService,
    private confirmServ: NzModalService,
    public msgSrv: NzMessageService,
    private modalHelper: ModalHelper,
    public organizationService: OrganizationService
  ) {}
  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.organizationService.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(d) {
    const data = {};
    if ( d != null) {
      Object.assign(data, d);
    }
    const cols = this.organization;
    this.modalHelper.static(OrganizationEditComponent, { cols , data }).subscribe(
      ( ret: { data: any, close: Function} ) => {
      // 修改状态
      if (ret.data['index'] != null ) {
          const index: number = ret.data['index'] ;
          const origData = this.grid.getData()[index];
          const isModified =  Object.keys(origData).some(
            (key: string) => {
                return ret.data[key] !== origData[key];
            }
          );
          // 未作修改
          if (!isModified) {
            ret.close();
            this.msgSrv.success('组织未作任何修改!');
            return;
          }
      }
      this.organizationService.save(ret.data).subscribe(
         ( res: any) => {
            if (res.code === 1) {
              this.load();
              ret.close();
              this.msgSrv.success('组织保存成功!');
            }
         }
      );
    });
  }
  delete(...id: number[]) {
    this.organizationService.delete( ...id ).subscribe(
      ( res: any) => {
         if (res.code === 1) {
           this.load();
           this.msgSrv.success('设备型号删除成功!');
         }
      }
   );
  }
   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();
  }
  reset(ls: any[]) {
    for (const item of ls) item.value = false;
    this.load(true);
  }
}