import {environment} from '../../../../environments/environment';
|
import {DateService} from '../../../business/services/util/date.service';
|
import {NzMessageService} from 'ng-zorro-antd';
|
import {Component, OnInit} from '@angular/core';
|
import {HttpClient} from '@angular/common/http';
|
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',
|
templateUrl: './demo.component.html',
|
styles: []
|
})
|
|
export class DemoComponent implements OnInit {
|
[x: string]: any;
|
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 professionOptions = [];
|
public dimensionOptions = [
|
{value: 'monitorPoint', label: '监测站点'},
|
{value: 'profession', label: '行业'}
|
];
|
monitorPointShow = true;
|
professionShow = false;
|
public items: any[] = [{
|
id: 0,
|
profession: null,
|
monitorPoint: null,
|
device: null,
|
time: new Date(),
|
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,
|
private areacodeService: AreacodeService,
|
public msgSrv: NzMessageService
|
) {
|
this.timeType = this.typeOptions[1];
|
this.query.reportType = this.reportOptions[1].value;
|
this.query.dimension = this.dimensionOptions[0].value;
|
}
|
|
ngOnInit() {
|
this.http.get(environment.SERVER_BASH_URL + 'sensor/all').subscribe((res: any) => {
|
if (res.code === 0) {
|
this.msgSrv.error(res.message);
|
} else {
|
this.sensorOptions.push({id: -1, name: '全部', isExpanded: true, children: res.data});
|
}
|
});
|
|
this.http.get(environment.SERVER_BASH_URL + 'mobile/profession').subscribe((res: any) => {
|
if (res.errno !== 0) {
|
this.msgSrv.error(res.message);
|
} else {
|
this.professionOptions = res.data;
|
}
|
});
|
}
|
|
dimensionChange(value) {
|
this.items.forEach((item, index) => {
|
item.profession = null;
|
item.monitorPoint = null;
|
this.getDevices(index);
|
});
|
if (value === 'monitorPoint') {
|
this.professionShow = false;
|
this.monitorPointShow = true;
|
} else {
|
this.professionShow = true;
|
this.monitorPointShow = false;
|
}
|
}
|
|
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,
|
profession: null,
|
monitorPoint: null,
|
device: null,
|
time: new Date(),
|
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].profession = null;
|
this.items[i].areaCode = 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;
|
}
|
});
|
this.getDevices(i);
|
}
|
|
getDevices(i) {
|
this.items[i].device = null;
|
if (!!this.items[i].profession) {
|
this.items[i].professionId = this.items[i].profession.id;
|
} else {
|
this.items[i].professionId = null;
|
}
|
if (!!this.items[i].monitorPoint) {
|
this.items[i].monitorPointId = this.items[i].monitorPoint.id;
|
} else {
|
this.items[i].monitorPointId = null;
|
}
|
return this.http.get(environment.SERVER_BASH_URL + 'device/professionId', {params: this.items[i]}).subscribe((res: any) => {
|
if (res.code === 0) {
|
this.msgSrv.error(res.message);
|
} else {
|
this.items[i].deviceOptions = res.data;
|
}
|
});
|
}
|
|
_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++) {
|
const areaName = '';
|
const item = this.items[i];
|
const queryItem: any = {};
|
if (item.monitorPointOptions.length > 0 && item.deviceOptions.length > 0) {
|
for (const key in item) {
|
if (item[key]) {
|
queryItem[key] = item[key];
|
}
|
}
|
if (queryItem.profession) {
|
queryItem.professionId = queryItem.profession.id;
|
queryItem.professionName = queryItem.profession.name;
|
delete queryItem.profession;
|
}
|
if (queryItem.monitorPoint) {
|
queryItem.monitorPointId = queryItem.monitorPoint.id;
|
queryItem.monitorPointName = queryItem.monitorPoint.name;
|
queryItem.monitorPointAddress = queryItem.monitorPoint.address;
|
delete queryItem.monitorPoint;
|
} else {
|
queryItem.monitorPointName = queryItem.areaName;
|
}
|
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 (validate) {
|
delete query.sensors;
|
if (this._sensors) {
|
const sensors = [];
|
for (const 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;
|
}
|
}
|
}
|