zhouwx
2024-12-04 80c8993820fc3f958397bebe9dbd72be6a449c55
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<template>
    <div class="system-gas-container">
        <el-card shadow="hover">
            <div class="system-menu-search mb15">
                <el-form :inline="true" style="display: flex;align-items: flex-start;flex-wrap: wrap" >
                    <el-form-item label="日期:" >
                        <el-date-picker
                            v-model="state.tableData.listQuery.searchParams.time"
                            type="datetimerange"
                            format="YYYY-MM-DD HH:mm:ss"
                            range-separator="~"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间"
                            @change = "chooseTime"
                        />
                    </el-form-item>
                    <el-form-item label="区域:">
                        <el-select
                            v-model="state.tableData.listQuery.searchParams.area"
                            class="w100"
                            style="max-width: 180px"
                            size="default"
                        >
                            <el-option v-for="item in state.tableData.areaList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="气体:">
                        <el-select
                            v-model="state.tableData.listQuery.searchParams.gas"
                            class="w100"
                            style="max-width: 180px"
                            size="default"
                        >
                            <el-option v-for="item in state.tableData.gasList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-button size="default" type="primary" class="ml10" @click="search()">
                        <el-icon>
                            <ele-Search />
                        </el-icon>
                        查询
                    </el-button>
                    <el-button size="default" class="ml10" @click="reset()">
                        <el-icon>
                            <RefreshLeft />
                        </el-icon>
                        重置
                    </el-button>
                  <vue3-json-excel
                      class="ml10"
                      :json-data="state.tableData.excelData"
                      :fields="fields"
                      name="通量数据.xls"
                  >
                    <el-button  type="primary" :icon="Download" size="default">导出</el-button>
                  </vue3-json-excel>
                </el-form>
            </div>
            <div :id="fluxChart" style="height: 500px;width: auto"></div>
            <el-table :data="state.tableData.data" style="width: 100%" v-loading="loading">
                <el-table-column type="index" label="序号" width="80" />
                <el-table-column align="center" prop="time" label="采集时间"/>
                <el-table-column align="center" prop="regionName" label="区域名称"/>
                <el-table-column align="center" prop="windSpeed" label="风速"/>
                <el-table-column align="center" prop="windDirection" label="风向"/>
                <el-table-column align="center" prop="name" label="气体名称"/>
                <el-table-column align="center" prop="gasValue" label="气体浓度"/>
            </el-table>
            <br />
            <el-pagination
                @size-change="onHandleSizeChange"
                @current-change="onHandleCurrentChange"
                class="page-position"
                :pager-count="5"
                :page-sizes="[10, 20, 30]"
                v-model:current-page="state.tableData.listQuery.pageIndex"
                background
                v-model:page-size="state.tableData.listQuery.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="state.tableData.total">
            </el-pagination>
            <br />
            <br />
        </el-card>
    </div>
</template>
 
<script setup lang="ts">
import {reactive, ref, onMounted, shallowRef} from "vue";
import * as echarts from "echarts";
import { ElMessage, ElMessageBox } from 'element-plus'
import {TableFluxState} from "/@/types/monitorData";
import moment from "moment/moment";
import { gasManageApi } from "/@/api/basicDataManage/gasManage";
import { areaManageApi } from "/@/api/basicDataManage/areaManage";
import { fluxDataApi } from "/@/api/monitorData/fluxData";
import {Download} from "@element-plus/icons-vue";
import {gasDataApi} from "/@/api/monitorData/gasData";
 
const state = reactive<TableFluxState>({
    tableData: {
        data: [],
        total: 0,
        loading: false,
        listQuery: {
            pageIndex: 1,
            pageSize: 10,
            searchParams:{
                time: [],
                gas: '',
                area: ''
            }
        },
        gasList: [],
        areaList: [],
        excelData: []
    },
});
const fields = ref({
  'time':'dataReceivingTime',
  '设备ID':'equipmentId',
  '区域': 'areaId',
  '风速':'windSpeed',
  '风向':'windDirection',
  '甲烷CH4':'gasName01',
  'gasValue01':'gasValue01',
  '乙烷C2H6':'gasName02',
  'gasValue02':'gasValue02',
  '丙烷C3H8':'gasName03',
  'gasValue03':'gasValue03',
  '丁烷C4H10':'gasName04',
  'gasValue04':'gasValue04',
  '硫化氢H2S':'gasName05',
  'gasValue05':'gasValue05',
  '乙烯C2H4':'gasName06',
  'gasValue06':'gasValue06',
  '异丁烷C4H10':'gasName07',
  'gasValue07':'gasValue07',
});
 
const loading = ref(false);
const myChart = shallowRef(null)
const fluxChart =ref("eChartfluxN" + Date .now() + Math .random())
const chooseTime = (val:any) => {
    console.log("val",val)
    let sTime = Date.parse(new Date(val[0]));
    let eTime = Date.parse(new Date(val[1]));
    console.log(sTime, eTime)
    // 2.时间戳间隔 再加上一天
    const datadiff = eTime - sTime + 86400000;
    // 3.七天间隔
    const time = 7 * 24 * 60 * 60 * 1000;
    // 4.比较两个日期的远近,date2为最近的日期
    if (sTime > eTime) {
        return false;
    } else {
        if (datadiff > time) {
            ElMessage({
                type: 'error',
                message: '查询时间范围7天内',
            })
            getNowTime();
            return false;
        } else {
            console.log('七天内数据')
        }
    }
}
 
onMounted(
    () => {
        getNowTime();
        getArea();
        getAllGas();
    }
);
 
const getNowTime = () => {
    let isDate = new Date()
    let sTime = `${isDate.getFullYear()}-${isDate.getMonth() + 1}-${isDate.getDate()}`
    let eTime = `${isDate.getFullYear()}-${isDate.getMonth() + 1}-${isDate.getDate()}`
    sTime = `${sTime} 00:00:00`
    eTime = `${eTime} ` + moment().format('HH:mm:ss')
    state.tableData.listQuery.searchParams.time = [sTime ,eTime];
}
 
const dataZoomEnd = ref();
const xData = ref([] as any);
const yData = ref([]);
const areaChart = ref([]as any);
const initInfoData = async () => {
    await exportFluxData()
    //柱状图
    const chartParam = {
        startTime: moment(state.tableData.listQuery.searchParams.time[0]).format('YYYY-MM-DD HH:mm:ss'),
        endTime: moment(state.tableData.listQuery.searchParams.time[1]).format('YYYY-MM-DD HH:mm:ss'),
        gasName: state.tableData.listQuery.searchParams.gas,
        areaId: state.tableData.listQuery.searchParams.area
    }
    let resChart = await fluxDataApi().getFluxLineChart(chartParam);
    if(resChart.data.code == 100) {
        if(resChart.data.data) {
            // yData.value = mapArray(resChart.data.data);
          const gasTime = resChart.data.data.map(i=>i.time?.slice(11))
          const gasTlTime = [...new Set(gasTime)]
          let areaData = state.tableData.areaList.map((item)=>{
            return {
              areaId: item.id,
              name: item.name,
              dotList: []
            }
          })
          for(let i of gasTlTime){
            const sameTimeData = resChart.data.data.filter(it=>it.time?.slice(11) == i)
            for(let j of areaData){
              const foundData = sameTimeData.find(k=>k.areaId == j.areaId)
              if(foundData){
                j.dotList.push(foundData.gasValue)
              }else{
                j.dotList.push(0)
              }
            }
          }
          yData.value = areaData.map((i)=>{
            return {
              name: i.name,
              type: 'bar',
              data: i.dotList,
              label: labelOption,
              emphasis:{
                focus: 'series'
              }
            }
          })
            areaChart.value = yData.value.map((item: any) => {
                return item.name
            });
 
            dataZoomEnd.value = gasTlTime.length > 25 ? 5 : 100;
            console.log(yData,'y')
 
            initCharts(gasTlTime,yData.value)
        }else {
            yData.value = [];
            xData.value = [];
            dataZoomEnd.value = 100;
          initCharts([],[]);
        }
 
        // initCharts();
    }else {
        ElMessage({
            type: 'warning',
            message: resChart.data.msg
        });
    }
 
    loading.value = true;
    //表格
    const pageParam = {
        pageIndex: state.tableData.listQuery.pageIndex,
        pageSize: state.tableData.listQuery.pageSize,
        searchParams: {
            startTime: moment(state.tableData.listQuery.searchParams.time[0]).format('YYYY-MM-DD HH:mm:ss'),
            endTime: moment(state.tableData.listQuery.searchParams.time[1]).format('YYYY-MM-DD HH:mm:ss'),
            gasName: state.tableData.listQuery.searchParams.gas,
            areaId: state.tableData.listQuery.searchParams.area
        }
    }
    let res = await fluxDataApi().getFluxLinePage(pageParam);
    if(res.data.code == 100) {
        state.tableData.data = res.data.data;
        state.tableData.total = res.data.total == null ? 0 :res.data.total;
        state.tableData.listQuery.pageIndex = res.data.pageIndex;
        state.tableData.listQuery.pageSize = res.data.pageSize;
        loading.value = false;
    }else {
        ElMessage({
            type: 'warning',
            message: res.data.msg
        });
    }
};
 
const exportFluxData = async () => {
  const pageParam = {
    startTime: moment(state.tableData.listQuery.searchParams.time[0]).format('YYYY-MM-DD HH:mm:ss'),
    endTime: moment(state.tableData.listQuery.searchParams.time[1]).format('YYYY-MM-DD HH:mm:ss'),
    ch4: state.tableData.listQuery.searchParams.gas == 1 ? 1 : 0,
    c2h6: state.tableData.listQuery.searchParams.gas == 2 ? 1:0,
    c3H8: state.tableData.listQuery.searchParams.gas == 3 ? 1:0,
    c4h101: state.tableData.listQuery.searchParams.gas == 4 ? 1:0,
    h2s: state.tableData.listQuery.searchParams.gas == 5 ? 1:0,
    c2h4: state.tableData.listQuery.searchParams.gas == 6 ? 1:0,
    c4h102: state.tableData.listQuery.searchParams.gas == 7 ? 1:0,
 
// 发对应的编号,查询数据;null,不查询
    areaId1: state.tableData.listQuery.searchParams.area == 1 ? '1':null,
    areaId2: state.tableData.listQuery.searchParams.area == 2 ? '2':null,
    areaId3: state.tableData.listQuery.searchParams.area == 3 ? '3':null,
    areaId4: state.tableData.listQuery.searchParams.area == 4 ? '4':null,
    areaId5: state.tableData.listQuery.searchParams.area == 5 ? '5':null,
    areaId6: state.tableData.listQuery.searchParams.area == 6 ? '6':null,
  }
  let res = await fluxDataApi().exportFlux(pageParam);
  if(res.data.code == 200) {
    state.tableData.excelData = res.data.data
    console.log('excelData',state.tableData.excelData)
  }else {
    ElMessage({
      type: 'warning',
      message: res.data.msg
    });
  }
}
 
const labelOption = {
    rotate: 90,
    align: 'left',
    verticalAlign: 'middle',
    position: 'insideBottom',
    distance: 15,
};
const mapArray = (target: any) => {
    let obj = {}as any;
    let result = [];
    let finalResult = []as any;
    target.map((item: any) => {
        let key = item.regionName;
        if (obj[key]) {
            obj[key].push(item);
        } else {
            obj[key] = [item];
        }
    });
    //根据obj中有几个对象,挨个push进去,组成最后的数组
    for (const key in obj) {
        const element = obj[key];
        result.push(element);
    }
    xData.value = result[0].map((item:any) => {
        return item.time;
    })
    result.forEach(item => {
        let obj = {
            name: '',
            data: [],
            type: 'bar',
            label: labelOption,
            emphasis:{
                focus: 'series'
            }
        };
       const data = item.map((i:any) => {
            return i.gasValue
        });
       obj.name = item[0].regionName;
       obj.data = data;
        finalResult.push(obj);
    })
    return finalResult;
}
 
 
const getArea = async () => {
    const param = {
        pageIndex: 1,
        pageSize: 999,
        searchParams:{
            name: ''
        }
    }
    let res = await areaManageApi().getAreaPage(param);
    if(res.data.code == 100) {
        state.tableData.areaList = res.data.data;
        // state.tableData.listQuery.searchParams.area = res.data.data[0].id;
    }else {
        ElMessage({
            type: 'warning',
            message: res.data.msg
        });
    }
}
const getAllGas = async () => {
    let res = await gasManageApi().getGas({});
    if(res.data.code == 100) {
        state.tableData.gasList = res.data.data;
        state.tableData.listQuery.searchParams.gas = res.data.data[0].id;
        await initInfoData();
    }else{
        ElMessage({
            type: 'warning',
            message: res.data.msg
        });
    }
};
 
const onHandleSizeChange = (val: number) => {
    state.tableData.listQuery.pageSize = val;
    initInfoData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.tableData.listQuery.pageIndex = val;
    initInfoData();
};
const search = () => {
    initInfoData();
    console.log("22",state.tableData.listQuery.searchParams)
}
const reset = () => {
    getNowTime();
    state.tableData.listQuery.searchParams.area = '';
    state.tableData.listQuery.searchParams.gas = state.tableData.gasList[0].id;
    state.tableData.listQuery.pageIndex = 1;
    initInfoData();
}
 
 
const initCharts = (time: Array<string>,data: Array<any>) => {
    if (myChart.value != null && myChart.value != "" && myChart.value != undefined) {
        myChart.value.dispose();
    }
    console.log(yData.value.length,'xxxx')
 
    myChart.value = echarts.init(document.getElementById(fluxChart.value));
    // 指定图表的配置项和数据
    const option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        legend: {
            data: areaChart.value
        },
        toolbox: {
            show: true,
            orient: 'vertical',
            left: 'right',
            top: 'center',
            feature: {
                mark: { show: true },
                dataView: { show: true, readOnly: false },
                magicType: { show: true, type: ['line', 'bar', 'stack'] },
                restore: { show: true },
                saveAsImage: { show: true }
            }
        },
        graphic: {
            type: 'text',     // 类型:文本
            left: 'center',
            top: 'middle',
            silent: true,     // 不响应事件
            invisible: yData.value.length > 0,   // 有数据就隐藏
            style: {
                fill: '#9d9d9d',
                fontWeight: 'bold',
                text: '暂无数据',
                fontFamily: 'Microsoft YaHei',
                fontSize: '22px'
            }
        },
        xAxis: [
            {
                type: 'category',
                axisTick: { show: false },
                data: time
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: data,
 
        dataZoom: [
            {
                type: 'slider',
                show: dataZoomEnd.value == 100 ? false : true,
                realtime: true,
                start: 0,
                end: dataZoomEnd.value
            },
            {
                type: 'inside',
                realtime: true,
                start: 0,
                end: dataZoomEnd.value
            },
        ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart.value.setOption(option,true);
    //自适应宽度
    window.addEventListener('resize', function () {
        myChart.value.resize();
    })
}
</script>
<style scoped lang="scss">
.yellow{
    width: 80px;
    height: 30px;
    background-color: rgb(255,223,37);
    line-height: 30px;
    color: white;
    box-shadow: 4px 4px 4px rgba(0, 0, 0, 0.2);
    padding: 3px
}
.red{
    width: 80px;
    height: 30px;
    background-color: rgb(239,90,161);
    line-height: 30px;
    color: white;
    box-shadow: 4px 4px 4px rgba(0, 0, 0, 0.2);
    padding: 3px
}
</style>