fengxiang
2018-06-25 27cd36be226ca2434f06b1ae9e4d43f1fea639ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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 = '组织列表';
    }
}