xufenglei
2018-04-03 b8b94e846dcde8680deff869ea65caee23c3bc89
src/app/routes/reports/demo/demo.component.ts
@@ -3,8 +3,10 @@
import {NzMessageService} from 'ng-zorro-antd';
import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Router} from '@angular/router';
import {CascaderOption} from 'ng-zorro-antd/src/cascader/nz-cascader.component';
import {AreacodeService} from '@business/services/http/areacode.service';
import {NzTreeComponent} from 'ng-tree-antd';
import {Subject} from 'rxjs/Subject';
@Component({
  selector: 'app-demo',
@@ -14,195 +16,243 @@
export class DemoComponent implements OnInit {
  [x: string]: any;
  query: any = {
  };
  time: any = {
    format: 'YYYY',
    mode: 'month'
  };
  public query: any = {};
  public sensorOptions = [];
  public typeOptions = [
    {value: 'year', label: '年', format: 'YYYY'},
    {value: 'month', label: '月', format: 'YYYY-MM'},
    {value: 'day', label: '日', format: 'YYYY-MM-DD'},
    {value: 'hour', label: '时', format: 'YYYY-MM-DD HH'}
  ];
  public reportOptions = [
    {value: 'bar', label: '柱状图'},
    {value: 'line', label: '折线图'}
  ];
  public monitorPointOptions = [];
  public deviceOptions = [];
  public monitorPointbOptions = [];
  public devicebOptions = [];
  public items: any[] = [{
    id: 0,
    monitorPoint: null,
    device: null,
    time: null,
    monitorPointOptions: [],
    deviceOptions: []
  }];
  private treeClickStream: Subject<any> = new Subject<any>();
  private _sensors: {[key: string]: string} = {};
  private _sensorNames: string;
  get sensorNames(): string {
    return this._sensorNames;
  }
  constructor(
    public http: HttpClient,
    public dateSrv: DateService,
    public router: Router,
    private areacodeService: AreacodeService,
    public msgSrv: NzMessageService
  ) {}
  ) {
    this.timeType = this.typeOptions[1];
    this.query.reportType = this.reportOptions[1].value;
  }
  ngOnInit() {
    this.query.time = null;
    this.query.timeb = null;
    this.http.get(environment.SERVER_BASH_URL + 'sensor/all').subscribe((res: any) => {
      if (res.code === 0) {
        this.msgSrv.error(res.message);
      } else {
        this.sensorOptions = res.data;
        this.sensorOptions.push({id: -1, name: '全部', isExpanded: true, children: res.data});
      }
    });
  }
  public onTreeClickSelect(event): void {
    this.treeClickStream.next(event);
  }
  public onSensorSelect(event): void {
    const data = event.node.data;
    if (data.id === -1 && data.halfChecked === false) {
      if (!!data.checked) {
        this.sensorOptions[0].children.forEach(
          sensor => {
            this._sensors[sensor.id] = sensor.sensorKey + '-' + sensor.name + '-' + sensor.unit;
          }
        );
      } else {
        this._sensors = {};
      }
    } else {
      if (!!data.checked) {
        this._sensors[data.id] = data.sensorKey + '-' + data.name + '-' + data.unit;
      } else {
        delete this._sensors[data.id];
      }
    }
    this.reloadSensorNames();
  }
  private reloadSensorNames(): void {
    // 异步提升展现速度
    setTimeout(() => {
      this._sensorNames = '';
      const sensorNameList = Object.keys(this._sensors).map(
        id => {
          const sensor = this.sensorOptions[0].children.find(item => {
            return Number(id) === Number(item.id);
          });
          return sensor.name;
        }
      );
      this._sensorNames = sensorNameList.join(', ');
    }, 1);
  }
  addItem() {
    const id = (this.items.length > 0) ? this.items[this.items.length - 1].id + 1 : 0;
    const index = this.items.push({
      id: id,
      monitorPoint: null,
      device: null,
      time: null,
      monitorPointOptions: [],
      deviceOptions: []
    });
  }
  public areaLazyLoad(event: {option: CascaderOption, index: number, resolve: (children: CascaderOption[]) => void, reject: () => void}) {
    const index = event['index'];
    const option = event.option;
    switch (index) {
      case -1:
        this.areacodeService.getProvinces().subscribe(
          (res: {label: string, value: string}[]) => {
            event.resolve(res);
          }
        ); break;
      case 0:
        this.areacodeService.getCities(option.value).subscribe(
          (res: {label: string, value: string}[]) => {
            event.resolve(res);
          }
        ); break;
      case 1:
        this.areacodeService.getAreas(option.value).subscribe(
          (res: {label: string, value: string}[]) => {
            event.resolve(res);
          }
        ); break;
    }
  }
  public regionChange(event: {option: CascaderOption, index: number}, i) {
    let name = '';
    let areaName = '';
    const option = event.option;
    this.items[i].monitorPoint = null;
    this.items[i].areaCode = null;
    this.items[i].device = null;
    switch (event.index) {
      case 0:
        name = 'provinceCode';
        this.items[i].provinceCode = option.value;
        this.items[i].cityCode = null;
        areaName = option.label;
        break;
      case 1:
        name = 'cityCode';
        this.items[i].cityCode = option.value;
        areaName = option.parent.label + '/' + option.label;
        break;
      case 2:
        name = 'areaCode';
        this.items[i].areaCode = option.value;
        areaName = option.parent.parent.label + '/' + option.parent.label + '/' + option.label;
        break;
    }
    this.items[i].areaName = areaName;
    this.http.get(environment.SERVER_BASH_URL + 'monitor-point/list/region', {params: {name: name, value: option.value}}).subscribe((res: any) => {
      if (res.code === 0) {
        this.msgSrv.error(res.message);
      } else {
        this.items[i].monitorPointOptions = res.data;
      }
    });
  }
  searchChange(searchText) {
    if (searchText) {
      const query = encodeURI(searchText);
      if (query) {
        this.http.get(environment.SERVER_BASH_URL + '/monitor-point/list/' + query).subscribe((res: any) => {
          if (res.code === 0) {
            this.msgSrv.error(res.message);
          } else {
            this.monitorPointOptions = res.data;
          }
        });
      }
    }
  }
  monitorPointChange(value) {
    this.query.mac = null;
    this.deviceOptions = [];
  monitorPointChange(value, i) {
    this.items[i].deviceOptions = [];
    this.items[i].device = null;
    if (value) {
      this.monitorPointOptions.forEach(monitorPoint => {
        if (monitorPoint.id === value) {
          this.monitorPointa = monitorPoint;
        }
      });
      this.http.get(environment.SERVER_BASH_URL + 'device/monitorPointId', {params: {monitorPointId: value}}).subscribe((res: any) => {
      this.http.get(environment.SERVER_BASH_URL + 'device/monitorPointId', {params: {monitorPointId: value.id}}).subscribe((res: any) => {
        if (res.code === 0) {
          this.msgSrv.error(res.message);
        } else {
          this.deviceOptions = res.data;
        }
      });
    } else {
      this.monitorPointa = null;
    }
  }
  searchChangeb(searchText) {
    if (searchText) {
      const query = encodeURI(searchText);
      if (query) {
        this.http.get(environment.SERVER_BASH_URL + '/monitor-point/list/' + query).subscribe((res: any) => {
          if (res.code === 0) {
            this.msgSrv.error(res.message);
          } else {
            this.monitorPointbOptions = res.data;
          }
        });
      }
    }
  }
  devicebChange(value) {
    if (value) {
      this.devicebOptions.forEach(device => {
        if (device.mac === value) {
          this.deviceb = device;
        }
      });
    } else {
      this.deviceb = null;
    }
  }
  deviceChange(value) {
    this.devicea = null;
    if (value) {
      this.deviceOptions.forEach(device => {
        if (device.mac === value) {
          this.devicea = device;
        }
      });
    } else {
      this.devicea = null;
    }
  }
  monitorPointbChange(searchText) {
    this.query.macb = null;
    this.devicebOptions = [];
    if (searchText) {
      this.monitorPointbOptions.forEach(monitorPoint => {
        if (monitorPoint.id === searchText) {
          this.monitorPointb = monitorPoint;
        }
      });
      this.http.get(environment.SERVER_BASH_URL + 'device/monitorPointId', {params: {monitorPointId: searchText}}).subscribe((res: any) => {
        if (res.code === 0) {
          this.msgSrv.error(res.message);
        } else {
          this.devicebOptions = res.data;
          this.items[i].deviceOptions = res.data;
        }
      });
    }
  }
  public typeOptions = [
    {value: 'year', label: '年', mode: 'month', xAxisName: '月', format: 'yyyy', typeFormat: '%Y-%m'},
    {value: 'month', label: '月', mode: 'month', xAxisName: '日', format: 'yyyy-MM', typeFormat: '%Y-%m-%d'},
    {value: 'day', label: '日', mode: 'day', xAxisName: '时', format: 'yyyy-MM-dd', typeFormat: '%Y-%m-%d %H'},
    {value: 'hour', label: '时', mode: 'day', xAxisName: '分', format: 'yyyy-MM-dd HH', typeFormat: '%Y-%m-%d %H:%i'}
  ];
  typeChange(searchText) {
    this.typeOptions.forEach(types => {
      if (types.value === searchText) {
        this.timeType = types;
        this.time.format = types.format.toUpperCase();
        this.time.mode = types.mode;
      }
    });
  _disabledDate(current: Date): boolean {
    return current && current.getTime() > Date.now();
  }
  reportQuery() {
    const query = this.query;
    if (query.sensorKey && query.type && ((query.monitorPoint && query.time) || (query.monitorPointb && query.timeb))) {
      if (query.time) {
        query.time = this.dateSrv.date_format(query.time, this.time.format);
    let validate = true;
    const queryItems = [];
    for (let i = 0; i < this.items.length; i++) {
      let areaName = '';
      let item = this.items[i];
      let queryItem: any = {};
      if ((!!item.time) && (item.monitorPointOptions.length > 0)) {
        for (var key in item) {
          if (item[key]) {
            queryItem[key] = item[key];
          }
        }
        if (queryItem.monitorPoint) {
          queryItem.monitorPointId = queryItem.monitorPoint.id;
          queryItem.monitorPointName = queryItem.monitorPoint.name;
          queryItem.monitorPointAddress = queryItem.monitorPoint.address;
          delete queryItem.monitorPoint;
        }
        delete queryItem.monitorPointOptions;
        if (queryItem.device) {
          queryItem.mac = queryItem.device.mac;
          queryItem.deviceName = queryItem.device.name;
          delete queryItem.device;
        }
        if (queryItem.deviceOptions && queryItem.deviceOptions.length > 0) {
          queryItem.deviceCount = queryItem.deviceOptions.length;
        }
        delete queryItem.deviceOptions;
        queryItem.formatTime = this.dateSrv.date_format(queryItem.time, this.timeType.format);
        delete queryItem.time;
        queryItems.push(queryItem);
      } else {
        validate = false;
        break;
      }
      if (query.timeb) {
        query.timeb = this.dateSrv.date_format(query.timeb, this.time.format);
      }
      query.format = this.timeType.format;
      query.typeFormat = this.timeType.typeFormat;
      query.xAxisName = this.timeType.xAxisName;
      query.label = this.timeType.label;
      query.sensorName = this.sensor.name;
      query.sensorUnit = this.sensor.unit;
      if (this.devicea) {
        query.deviceaName = this.devicea.name;
      }
      if (this.deviceb) {
        query.devicebName = this.deviceb.name;
      }
      if (this.monitorPointa) {
        query.monitorPointaName = this.monitorPointa.name;
      }
      if (this.monitorPointb) {
        query.monitorPointbName = this.monitorPointb.name;
      }
      this.router.navigate(['report'], {queryParams: query});
    } else {
      this.msgSrv.error('请完善搜索项');
    }
  }
  sensorChange(value) {
    this.sensorOptions.forEach(sensor => {
      if (sensor.sensorKey === value) {
        this.sensor = sensor;
    if (validate && this.timeType && query.reportType) {
      if (this._sensors) {
        const sensors = [];
        for (var key in this._sensors) {
          sensors.push(this._sensors[key]);
        }
        if (sensors.length > 0) {
          query.sensors = JSON.stringify(sensors);
        }
      }
    });
      query.items = JSON.stringify(queryItems);
      query.type = this.timeType.value;
      sessionStorage.setItem("queryParams", JSON.stringify(query));
    } else {
      this.msgSrv.error('请完善搜索项或删除查询条目');
      return false;
    }
  }
}