he wei
2025-04-03 c6a2d2debf161b987ff91c6ac9560d10fc98c54b
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<script setup name="Dashboard">
    import { defineComponent } from 'vue';
    import HdwCard from '@/components/HdwCard/index.vue';
    import LedNum from '@/components/LedNum/index.vue';
    import mapChart from "@/components/echarts/mapChart.vue";
    import pie from "@/components/echarts/pie.vue";
    import bar from "@/components/echarts/bar.vue";
    import groupBar from "@/components/echarts/groupBar.vue";
 
    import { ref, watchEffect } from 'vue';
    import useWebsocket from '@/hooks/useWebsocket';
 
    const { message: listMessage } = useWebsocket('home');
 
    const stationNum = ref(0);
    const chartData = ref([]);
    const map = ref();
    const typePie = ref();
    const prodPie = ref();
    const testBar = ref();
    const useageRateChart = ref();
 
    let lockDatas = ref([]);
    const rateList = ref([]);
    const rateType = ref('month');
 
    // "offLineNum": 6,
    //       "unLoadNum": 5,
    //       "sumLinf": 7,
    //       "openNum": 0,
    //       "onlineNum": 1,
    //       "closeNum": 2
 
    const offLine_num = ref(0);
    const unLoad_num = ref(0);
    const sumLinf_num = ref(0);
    const open_num = ref(0);
    const online_num = ref(0);
    const close_num = ref(0);
    const rtData = ref([]);
    const alarmData = ref([]);
 
    watchEffect(() => {
        if (listMessage.value) {
            const { resScreenBox, resErrorCtlLog, resAllCtlLog, resLockState, resReport, resAllAinf } = JSON.parse(listMessage.value)?.data;
            console.log('resScreenBox, resErrorCtlLog, resAllCtlLog, resLockState, resReport', resScreenBox, resErrorCtlLog, resAllCtlLog, resLockState, resReport, '=============');
 
            let locks = [];
            let _offLine = 0,
                _unLoad = 0,
                _sumLinf = 0,
                _open = 0,
                _online = 0,
                _close = 0;
 
            if (resLockState.code && resLockState.data) {
                let _list = resLockState.data2;
                locks = _list.allLinfs.filter(v => v.longitude || v.latitude);
                _offLine = _list.offLineNum;
                _unLoad = _list.unLoadNum;
                _sumLinf = _list.sumLinf;
                _open = _list.openNum;
                _online = _list.onlineNum;
                _close = _list.closeNum;
            }
 
            offLine_num.value = _offLine;
            unLoad_num.value = _unLoad;
            sumLinf_num.value = _sumLinf;
            open_num.value = _open;
            online_num.value = _online;
            close_num.value = _close;
 
            lockDatas.value = locks;
 
            if (resAllCtlLog.code && resAllCtlLog.data) {
                let _list = resAllCtlLog.data2.map(v => ({
                    ...v,
                    state: v.ctlResult ? '开锁成功' : '开锁失败'
                }));
                rtData.value = _list;
            }
            if (resErrorCtlLog.code && resErrorCtlLog.data) {
                let _list = resErrorCtlLog.data2.map(v => ({
                    ...v,
                    state: v.ctlResult ? '开锁成功' : '开锁失败'
                }));
                alarmData.value = _list;
            }
 
            let _rateList = [];
            if (resReport.code && resReport.data) {
                _rateList = resReport.data2;
            }
            rateList.value = _rateList;
 
 
            let area_num = 0;
            if (resAllAinf.code && resAllAinf.data) {
                area_num = resAllAinf.data2;
            }
 
            stationNum.value = area_num;
 
            if (resScreenBox.code && resScreenBox.data) {
                let { product, type } = resScreenBox.data2;
                let prodData = formatPieData(product);
                let typeData = formatPieData(type);
 
                typePie.value.updateChart(typeData.sData);
                prodPie.value.updateChart(prodData.sData);
            }
 
            updateMap();
            updateStateChart();
            updateUseageRateChart();
        }
    });
 
    function updateStateChart() {
        let labels = ['锁具', '未安装', '开锁', '关锁', '在线', '离线'];
        let datas = [sumLinf_num.value, unLoad_num.value, open_num.value, close_num.value, online_num.value, offLine_num.value];
        testBar.value.updateChart(labels, datas);
    }
 
    /**
     * 格式化数据  取前4 剩下的为其他
     */
    function formatPieData(data) {
        let res = { sData: [] };
        let arr = Object.keys(data)
            .map((v) => ({ name: v, value: data[v] }))
            .sort((a, b) => {
                return b.value - a.value;
            });
        let total = 0;
        arr.map(v => {
            total += v.value * 1;
        });
 
        if (arr.length <= 5) {
            res.sData = arr;
        } else {
            let name = "其他";
            let value = 0;
            let otherList = [];
            arr.splice(4).forEach((v) => {
                value += v.value * 1;
                let percent = (total ? v.value / total : 0).toFixed(4);
                otherList.push({
                    name: v.name,
                    value: v.value,
                    percent: (percent * 100).toFixed(2) + "%"
                });
            });
 
 
            res.sData = arr;
            res.sData.push({ name, value });
        }
        return res;
    }
 
    function updateMap() {
        const getColor = (onLine) => ["#aaa", "#0f0"][onLine];
        let data = lockDatas.value;
        map.value.updateChart(
            data.map((v) => {
                return {
                    ...v,
                    label: v.lockName,
                    color: getColor(v.lockOnline),
                    points: [v.longitude, v.latitude],
                    // 无实际意义
                    value: 100,
                };
            })
        );
    }
 
    function updateUseageRateChart() {
        // 获取当前的日期对象
        let currentDate = new Date();
        // 获取月份数(0 表示一月,11 表示十二月)
        let month = currentDate.getMonth() + 1;
        // 获取季度数
        let quarter = Math.ceil(month / 3);
        let type = rateType.value;
        let _list = rateList.value;
        let list = [];
        switch (type) {
            case 'month':
                list = _list.map(v => ({
                    label: v.lockName, value: v['month' + month]
                })).sort((a, b) => b.value - a.value);
                break;
            case 'quarter':
                list = _list.map(v => ({
                    label: v.lockName, value: v['quarter' + quarter]
                })).sort((a, b) => b.value - a.value);
                break;
            case 'year':
                list = _list.map(v => ({
                    label: v.lockName, value: v.yearCount
                })).sort((a, b) => b.value - a.value);
                break;
        }
 
 
        let labels = list.slice(0, 5).map(v => v.label);
        let datas = list.slice(0, 5).map(v => v.value);
 
        useageRateChart.value.updateChart(labels, datas);
    }
 
</script>
 
<template>
  <div class="dashboard-wrapper">
    <div class="dashboard-left">
      <div class="left-content-list">
        <div class="left-content-item">
          <hdw-card is-full title="屏柜类型">
            <pie ref="typePie"></pie>
          </hdw-card>
        </div>
        <div class="left-content-item">
          <hdw-card is-full title="设备工作状态">
            <bar ref="testBar" unit="套"></bar>
          </hdw-card>
        </div>
        <div class="left-content-item last">
          <hdw-card is-full title="环境状态">
            <div class="env">
              <!-- 烟感 -->
              <div class="item">
                <div class="label">烟感</div>
                <div class="value">0</div>
              </div>
              <!-- 温度 -->
              <div class="item">
                <div class="label">温度</div>
                <div class="value">0</div>
              </div>
              <!-- 湿度 -->
              <div class="item">
                <div class="label">湿度</div>
                <div class="value">0</div>
              </div>
            </div>
          </hdw-card>
        </div>
      </div>
    </div>
    <div class="dashboard-middle">
      <div class="middle-content-wrapper">
        <hdw-card>
          <div class="number-list-wrapper">
            <div class="number-item">
              <div class="number-label">区域</div>
              <div class="number-value">
                <led-num color="#f00" :num="stationNum"></led-num>
              </div>
            </div>
            <div class="number-item">
              <div class="number-label">锁具</div>
              <div class="number-value">
                <led-num color="#f00" :num="sumLinf_num"></led-num>
              </div>
            </div>
            <!-- <div class="number-item">
                                  <div class="number-label">电子卡</div>
                                  <div class="number-value">
                                    <led-num color="#f00" :num="cardNum"></led-num>
                                  </div>
                                </div> -->
          </div>
        </hdw-card>
        <div class="map-wrapper">
          <hdw-card is-full>
            <map-chart ref="map" @mapMounted="updateMap">
              <template #tools>
                <div class="map-mark">
                  <div class="mark online">
                    <el-icon class="ico"></el-icon>在线
                    <div class="count">{{ online_num }}</div>
                  </div>
                  <div class="mark offline">
                    <el-icon class="ico"></el-icon>离线
                    <div class="count">{{ offLine_num }}</div>
                  </div>
                </div>
              </template>
            </map-chart>
          </hdw-card>
        </div>
        <div class="dev-real-info">
          <hdw-card title="实时开锁信息" is-full>
            <el-table :data="rtData" style="width: 100%; height: 100%">
              <el-table-column prop="ctlTime" label="时间" width="180" />
              <el-table-column prop="lockId" label="锁具ID" width="180" />
              <el-table-column prop="lockName" label="锁具名称" />
              <el-table-column prop="state" label="操作状态" />
            </el-table>
          </hdw-card>
        </div>
      </div>
    </div>
    <div class="dashboard-right">
      <div class="left-content-list">
        <div class="left-content-item">
          <hdw-card is-full title="屏柜品牌">
            <pie ref="prodPie"></pie>
          </hdw-card>
  
        </div>
        <div class="left-content-item">
          <hdw-card is-full title="使用频次">
            <template #tools>
              <el-radio-group v-model="rateType" size="small">
                <el-radio-button value="month" label="本月"></el-radio-button>
                <el-radio-button value="quarter" label="本季度"></el-radio-button>
                <el-radio-button value="year" label="本年"></el-radio-button>
              </el-radio-group>
            </template>
            <bar ref="useageRateChart" :rotate="30" unit="次"></bar>
          </hdw-card>
        </div>
        <div class="left-content-item last">
          <hdw-card is-full title="告警统计">
            <el-table :data="alarmData" style="width: 100%; height: 100%">
              <el-table-column prop="ctlTime" label="时间" width="160" />
              <el-table-column prop="lockId" label="锁具ID" width="90" />
              <el-table-column prop="lockName" label="锁具名称" />
              <el-table-column prop="state" label="操作状态" />
            </el-table>
          </hdw-card>
        </div>
      </div>
    </div>
  </div>
</template>
 
<style scoped lang="less">
.dashboard-wrapper {
  display: flex;
  flex-direction: row;
  height: 100%;
 
  .dashboard-left {
    width: 500px;
    height: 100%;
  }
 
  .dashboard-middle {
    flex: 1;
    height: 100%;
  }
 
  .dashboard-right {
    width: 500px;
    height: 100%;
  }
}
 
.left-content-list {
  padding: 8px;
  height: 100%;
 
  .left-content-item {
    height: 33%;
    padding-bottom: 8px;
 
    &.last {
      height: 34%;
      padding-bottom: 0;
    }
  }
}
 
.middle-content-wrapper {
  display: flex;
  flex-direction: column;
  padding: 8px 0;
  height: 100%;
 
  .number-list-wrapper {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    margin-top: 8px;
 
    .number-item {
      cursor: pointer;
      display: flex;
      // border-radius: 6px;
      align-items: center;
 
      .number-label {
        white-space: nowrap;
        background: #0ff;
        border-radius: 6px;
        color: #333;
        font-size: 16px;
        font-weight: bold;
        padding: 0 10px;
      }
 
      .number-value {
        width: 4.5em;
        height: 1.5em;
        padding: 0 10px;
      }
    }
  }
 
  .map-wrapper {
    flex: 1;
    padding: 8px 0;
  }
 
  .wrap-chart {
    flex: 1;
    // position: relative;
  }
 
  .map-mark {
    position: absolute;
    right: 10px;
    bottom: 10px;
 
    .mark {
      display: flex;
      align-items: center;
 
      .ico {
        font-size: 14px;
        margin-right: 0.4em;
      }
 
      .count {
        margin-left: 1em;
      }
 
      &.online {
        color: #0f0;
      }
 
      &.offline {
        color: #aaa;
      }
    }
  }
 
  .dev-real-info {
    height: 34%;
  }
 
}
 
.env {
  height: 100%;
  padding: 10px;
  display: flex;
  flex-direction: row;
  align-items: center;
  .item {
    flex: 1;
    border-radius: 8px;
    padding: 4px 8px;
    background: #0ff;
    & + .item {
      margin-left: 20px;
    }
      .label {
        color: #333;
       font-weight: bold; 
       margin-bottom: 0.4em;
       text-align: center;
      }
      .value {
        background: #000;
        border-radius: 6px;
        padding: 4px 8px;
        color: #fff;
        font-weight: bold;
        text-align: center;
      }
  }
}
</style>