import { Component, OnInit } from '@angular/core';
|
import { _HttpClient } from '@delon/theme';
|
import { NzMessageService } from 'ng-zorro-antd';
|
import { OrganizationService } from '@business/services/http/organization.service';
|
import { Sensor, Organization } from '@business/entity/data';
|
import { ResultBean } from '@business/entity/grid';
|
|
@Component({
|
selector: 'app-organization-config-screen',
|
templateUrl: './organization-config-screen.component.html',
|
})
|
export class OrganizationConfigScreenComponent implements OnInit {
|
|
constructor(
|
private http: _HttpClient,
|
private organizationService: OrganizationService,
|
public msgSrv: NzMessageService
|
) { }
|
public selectedIndex = 0;
|
public defaultSwapFlag: string;
|
public coreSwapFlag: string;
|
public organization: Organization;
|
public deviceVersions: any[] = [];
|
public addedSensors: any[] = [];
|
// 待添加传感器
|
public surplusSensors: any[] = [];
|
public defaultMonitorItems: any[] = [];
|
public coreMonitorItems: any[] = [];
|
public optionalFixedItems: any[] = [];
|
public fixedMonitorItems: any[] = [];
|
public chartSensorKey: string;
|
|
ngOnInit() {
|
this.organization = this.organizationService.data;
|
this.deviceVersions =
|
!!this.organizationService.getResultBeanData('deviceVersions') ?
|
this.organizationService.getResultBeanData('deviceVersions') : [];
|
const rtdLayout = this.organizationService.getResultBeanData('rtdLayout');
|
const sensorCombs = <any[]>this.organizationService.getResultBeanData('sensorCombs');
|
const optionalFixedItems = <any[]>this.organizationService.getResultBeanData('optionalFixedItems');
|
this.loadPage(rtdLayout, sensorCombs, optionalFixedItems);
|
}
|
swapTab() {
|
const version = this.deviceVersions[this.selectedIndex];
|
this.isSaving = true;
|
this.isSpinning = true;
|
this.http.get('org-layout/rtd-act-config', {orgId: this.organization.id, versionNo: version.version }).subscribe(
|
(res: ResultBean<any>) => {
|
if ( res.code > 0 ) {
|
const data = res.data;
|
const rtdLayout = data.rtdLayout;
|
const sensorCombs = data.sensorCombs;
|
const optionalFixedItems = data.optionalFixedItems;
|
this.loadPage(rtdLayout, sensorCombs, optionalFixedItems);
|
this.isSaving = false;
|
this.isSpinning = false;
|
}
|
}
|
);
|
}
|
loadPage(rtdLayout:
|
{chartSensorKey: string,
|
coreMonitorItems: any[],
|
defaultMonitorItems: any [],
|
optionalFixedItems: any[],
|
fixedMonitorItems: any[]}
|
, sensorCombs: any[], optionalFixedItems: any[]) {
|
this.chartSensorKey = '';
|
this.coreMonitorItems = [];
|
this.defaultMonitorItems = [];
|
this.addedSensors = [];
|
this.surplusSensors = [];
|
this.fixedMonitorItems = [];
|
this.optionalFixedItems = !!optionalFixedItems ? optionalFixedItems : [];
|
if (!!rtdLayout && !!sensorCombs.length) {
|
this.chartSensorKey = rtdLayout.chartSensorKey;
|
this.coreMonitorItems = rtdLayout.coreMonitorItems;
|
this.defaultMonitorItems = rtdLayout.defaultMonitorItems;
|
this.fixedMonitorItems = rtdLayout.fixedMonitorItems;
|
this.addedSensors = [...rtdLayout.coreMonitorItems, ...rtdLayout.defaultMonitorItems];
|
this.surplusSensors = sensorCombs.filter(
|
( item: any ) => {
|
return !this.addedSensors.some(
|
(sen: any) => {
|
return item.sensorKey === sen.sensorKey;
|
}
|
);
|
}
|
);
|
} else {
|
this.surplusSensors = sensorCombs;
|
}
|
}
|
public get haveTaps(): Boolean {
|
return !!this.deviceVersions && !!this.deviceVersions.length;
|
}
|
public get isCoreItemsFull(): Boolean {
|
return this.coreMonitorItems.length >= 6;
|
}
|
public setChartSensorKey(sensorKey) {
|
this.chartSensorKey = sensorKey;
|
}
|
public itemUp(items: any[], index) {
|
if (index !== 0 ) {
|
const tempIndex = index - 1;
|
const temp = items[tempIndex];
|
items[tempIndex] = items[index];
|
items[index] = temp;
|
}
|
}
|
public itemDown(items: any[], index) {
|
if (index < (items.length - 1) ) {
|
const tempIndex = index + 1;
|
const temp = items[tempIndex];
|
items[tempIndex] = items[index];
|
items[index] = temp;
|
}
|
}
|
|
moveTo(source: any[], sIndex: number, target: any[]) {
|
const temp = source[sIndex];
|
source.splice(sIndex, 1);
|
target.push(temp);
|
// 顺序不要颠倒
|
this.refreshAddedList();
|
this.refreshChartSensorKey();
|
}
|
moveAllto(source: any[], target: any[]) {
|
const length = source.length;
|
target.push(...source);
|
source.splice(0, length);
|
this.refreshAddedList();
|
this.refreshChartSensorKey();
|
}
|
public refreshChartSensorKey() {
|
const isChartKeyRemoved = this.surplusSensors.some(
|
item => {
|
return item.sensorKey === this.chartSensorKey;
|
}
|
);
|
if (isChartKeyRemoved || !this.chartSensorKey) {
|
this.chartSensorKey = null;
|
const length = this.addedSensors.length;
|
if (length > 0) {
|
this.chartSensorKey = this.addedSensors[length - 1].sensorKey;
|
}
|
}
|
}
|
public get chartSensorName() {
|
const sensor = this.addedSensors.find(
|
item => {
|
return item.sensorKey === this.chartSensorKey;
|
}
|
);
|
return !!sensor ? sensor.name : '';
|
}
|
refreshAddedList() {
|
this.addedSensors = [...this.defaultMonitorItems, ...this.coreMonitorItems];
|
}
|
public isSaving = false;
|
public isSpinning = false;
|
save() {
|
this.isSaving = true;
|
const version = this.deviceVersions[this.selectedIndex].version;
|
const orgId = this.organization.id;
|
if ( !!this.addedSensors.length ) {
|
const rtdLayout = {
|
chartSensorKey: this.chartSensorKey,
|
defaultMonitorItems: this.defaultMonitorItems,
|
coreMonitorItems: this.coreMonitorItems,
|
fixedMonitorItems: this.fixedMonitorItems
|
};
|
const rtdLayoutUpload = {
|
realTimeDeviceLayout : rtdLayout,
|
organizationId: orgId,
|
version: version
|
};
|
this.http.post('org-layout/rtd-save', rtdLayoutUpload).subscribe(
|
(res: ResultBean<any>) => {
|
if (res.code > 0 ) {
|
this.isSaving = false;
|
this.msgSrv.success('保存成功');
|
}
|
}
|
);
|
} else {
|
this.http.get('org-layout/rtd-remove', {orgId: orgId, version: version}).subscribe(
|
(res: ResultBean<any>) => {
|
if (res.code > 0 ) {
|
this.isSaving = false;
|
this.msgSrv.success('保存成功');
|
}
|
}
|
);
|
}
|
}
|
isFixedSensorSelected(sensorKey: string) {
|
const isSelected = this.fixedMonitorItems.some( item => {
|
return item.sensorKey === sensorKey;
|
});
|
return isSelected;
|
}
|
|
changeFixedItem(event, sensor) {
|
if (event) {
|
this.fixedMonitorItems.push(sensor);
|
} else {
|
const delteIndex = this.fixedMonitorItems.findIndex(
|
item => {
|
return item.sensorKey === sensor.sensorKey;
|
}
|
);
|
if (delteIndex > 0 ) {
|
this.fixedMonitorItems.splice(delteIndex, 1);
|
}
|
}
|
}
|
backToList() {
|
this.organizationService.handle = 'list';
|
this.organizationService.title = '组织列表';
|
}
|
}
|