xufenglei
2018-03-26 ba15aacf079d1a3fd13e4594f5a94cf25dacb036
src/app/routes/reports/demo/demo.component.ts
@@ -13,25 +13,26 @@
export class DemoComponent implements OnInit {
  [x: string]: any;
  query: any = {
  };
  time: any = {
    format: 'YYYY',
    mode: 'month'
  };
  items = [{
  public query: any = {};
  public sensorOptions = [];
  public typeOptions = [
    {value: 'year', label: '年', format: 'yyyy', typeFormat: '%Y-%m', timeLength: 12},
    {value: 'month', label: '月', format: 'yyyy-MM', typeFormat: '%Y-%m-%d', timeLength: 28},
    {value: 'day', label: '日', format: 'yyyy-MM-dd', typeFormat: '%Y-%m-%d %H', timeLength: 24},
    {value: 'hour', label: '时', format: 'yyyy-MM-dd HH', typeFormat: '%Y-%m-%d %H:%i', timeLength: 60}
  ];
  public reportOptions = [
    {value: 'bar', label: '柱状图'},
    {value: 'line', label: '折线图'}
  ];
  public monitorPointOptions = [];
  public items = [{
    id: 0,
    monitorPoint: null,
    mac: '',
    device: null,
    time: null,
    formatTime: null,
    monitorPointName: '',
    deviceName: ''
    deviceOptions: []
  }];
  public sensorOptions = [];
  public monitorPointOptions = [];
  public deviceOptions = [];
  constructor(
    public http: HttpClient,
@@ -39,6 +40,8 @@
    public router: Router,
    public msgSrv: NzMessageService
  ) {
    this.timeType = this.typeOptions[1];
    this.query.reportType = this.reportOptions[1].value;
  }
  ngOnInit() {
@@ -54,16 +57,13 @@
  addItem() {
    const id = (this.items.length > 0) ? this.items[this.items.length - 1].id + 1 : 0;
    const index = this.items.push({
      id,
      id: id,
      monitorPoint: null,
      mac: '',
      device: null,
      time: null,
      formatTime: null,
      monitorPointName: '',
      deviceName: ''
      deviceOptions: []
    });
  }
  searchChange(searchText, i) {
    if (searchText) {
@@ -81,85 +81,69 @@
  }
  monitorPointChange(value, i) {
    this.deviceOptions = [];
    this.items[i].deviceOptions = [];
    this.items[i].device = null;
    if (value) {
      this.monitorPointOptions.forEach(monitorPoint => {
        if (monitorPoint.id === value) {
          this.items[i].monitorPointName = monitorPoint.name;
        }
      });
      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;
          this.items[i].deviceOptions = res.data;
        }
      });
    } else {
      this.items[i].monitorPoint = null;
      this.items[i].mac = '';
      this.items[i].monitorPointName = '';
    }
  }
  deviceChange(value, i) {
    if (value) {
      this.deviceOptions.forEach(device => {
        if (device.mac === value) {
          this.items[i].deviceName = device.name;
        }
      });
    } else {
      this.items[i].deviceName = '';
    }
  }
  public typeOptions = [
    {value: 'year', label: '年', mode: 'month', xAxisName: '月', format: 'yyyy', typeFormat: '%Y-%m', timeLength: 12},
    {value: 'month', label: '月', mode: 'month', xAxisName: '日', format: 'yyyy-MM', typeFormat: '%Y-%m-%d', timeLength: 28},
    {value: 'day', label: '日', mode: 'day', xAxisName: '时', format: 'yyyy-MM-dd', typeFormat: '%Y-%m-%d %H', timeLength: 24},
    {value: 'hour', label: '时', mode: 'day', xAxisName: '分', format: 'yyyy-MM-dd HH', typeFormat: '%Y-%m-%d %H:%i', timeLength: 60}
  ];
  timeType = this.typeOptions[0];
  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;
    let validate = true;
    const queryItems = [];
    for (let i = 0; i < this.items.length; i++) {
      let item = this.items[i];
      let queryItem: any = {};
      if (item.monitorPoint && item.time) {
        item.formatTime = this.dateSrv.date_format(item.time, this.time.format);
        for (var key in item) {
          if (item[key]) {
            queryItem[key] = item[key];
          }
        }
        queryItem.monitorPointId = queryItem.monitorPoint.id;
        queryItem.monitorPointName = queryItem.monitorPoint.name;
        queryItem.monitorPointAddress = queryItem.monitorPoint.address;
        delete queryItem.monitorPoint;
        if (queryItem.device) {
          queryItem.mac = queryItem.device.mac;
          queryItem.deviceName = queryItem.device.name;
          delete queryItem.device;
        }
        queryItem.deviceCount = queryItem.deviceOptions.length;
        delete queryItem.deviceOptions;
        queryItem.formatTime = this.dateSrv.date_format(queryItem.time, this.timeType.format.toUpperCase());
        delete queryItem.time;
        queryItems.push(queryItem);
      } else {
        validate = false;
        break;
      }
    }
    if (validate && query.type) {
      query.format = this.timeType.format;
      query.typeFormat = this.timeType.typeFormat;
      query.xAxisName = this.timeType.xAxisName;
      query.label = this.timeType.label;
      query.timeLength = this.timeType.timeLength;
      query.items = JSON.stringify(this.items);
    if (validate && this.timeType && query.reportType) {
      query.sensors = null;
      if (query.sensorKey && query.sensorKey.length > 0) {
        const sensors = [];
        query.sensorKey.forEach(sensor => {
          sensors.push(sensor.sensorKey + '-' + sensor.name + '-' + sensor.unit);
        });
        query.sensors = JSON.stringify(sensors);
      }
      query.items = JSON.stringify(queryItems);
      query.timeType = JSON.stringify(this.timeType);
      this.router.navigate(['report'], {queryParams: query});
    } else {
      this.msgSrv.error('请完善搜索项或删除查询条目');
    }
  }
  _disabledDate(current: Date): boolean {
    return current && current.getTime() > Date.now() ;
  }
}