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
<template>
  <el-row align="top">
    <el-upload
      ref="upload"
      class="upload-file"
      :limit="1"
      accept=".xls,.xlsx"
      :on-change="handleChange"
      :on-exceed="handleExceed"
      :auto-upload="false"
    >
      <template #trigger>
        <el-button type="primary" :loading="tableLoading">导入文件</el-button>
      </template>
      <template #tip>
        <div>
          <el-text type="danger">{{ tips }}</el-text>
        </div>
      </template>
    </el-upload>
    <div v-if="tableLoading">
      <el-icon class="is-loading"><Loading /></el-icon>
      <el-text>{{ loadTxt }}</el-text>
    </div>
  </el-row>
  <el-table
    ref="tableRef"
    :data="data"
    v-loading="tableLoading"
    table-layout="fixed"
    row-key="id"
    :expand-row-keys="expandRowKeys"
    :row-class-name="tableRowClassName"
    size="small"
    height="60vh"
    border
  >
    <!-- <el-table-column type="expand">
      <template #default="{ row }">
        {{ row.drSceneName }}
      </template>
    </el-table-column> -->
    <el-table-column
      v-if="isUploadNewFile"
      prop="isFound"
      label="合规"
      width="30"
    >
      <template #default="{ row }">
        <el-icon class="is-loading" v-if="row.loading">
          <Loading color="#409eff" />
        </el-icon>
        <el-icon v-else>
          <Check v-if="row.isFound" />
          <Close v-else />
        </el-icon>
      </template>
    </el-table-column>
    <el-table-column
      :show-overflow-tooltip="true"
      prop="drSceneName"
      label="名称"
      width="300"
    >
      <template #default="{ row }">
        <el-input
          v-if="isUploadNewFile && !row.isFound"
          size="small"
          v-model="row.drSceneName"
          @change="(e) => handleSceneNameChange(e, row)"
        />
        <span v-else>{{ row.drSceneName }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="drDeviceCode" label="设备号" width="130">
      <template #default="{ row }">
        <el-input
          v-if="isUploadNewFile && !row.isFound"
          size="small"
          v-model="row.drDeviceCode"
        />
        <span v-else>{{ row.drDeviceCode }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="drTime" label="时间" width="100">
      <template #default="{ row }">
        <span>{{ $fm.formatYM(row.drTime) }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="drExceedTimes" label="超标次数">
      <template #default="{ row }">
        <el-input
          v-if="isUploadNewFile && !row.isFound"
          size="small"
          v-model="row.drExceedTimes"
        />
        <span v-else>{{ row.drExceedTimes }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="drAvg" label="平均值">
      <template #default="{ row }">
        <el-input
          v-if="isUploadNewFile && !row.isFound"
          size="small"
          v-model="row.drAvg"
        />
        <span v-else>{{ row.drAvg }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="drMax" label="最大值">
      <template #default="{ row }">
        <el-input
          v-if="isUploadNewFile && !row.isFound"
          size="small"
          v-model="row.drMax"
        />
        <span v-else>{{ row.drMax }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="drMin" label="最小值">
      <template #default="{ row }">
        <el-input
          v-if="isUploadNewFile && !row.isFound"
          size="small"
          v-model="row.drMin"
        />
        <span v-else>{{ row.drMin }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="drOverAvgPer" label="超区均值百分比">
      <template #default="{ row }">
        <el-input
          v-if="isUploadNewFile && !row.isFound"
          size="small"
          v-model="row.drOverAvgPer"
        />
        <span v-else>{{ row.drOverAvgPer }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="drDataNum" label="数据量">
      <template #default="{ row }">
        <el-input
          v-if="isUploadNewFile && !row.isFound"
          size="small"
          v-model="row.drDataNum"
        />
        <span v-else>{{ row.drDataNum }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="drEffectiveRate" label="有效率">
      <template #default="{ row }">
        <el-input
          v-if="isUploadNewFile && !row.isFound"
          size="small"
          v-model="row.drEffectiveRate"
          placeholder="场景名称"
        />
        <span v-else>{{ row.drEffectiveRate }}</span>
      </template>
    </el-table-column>
    <el-table-column v-if="isUploadNewFile" type="expand">
      <template #default="{ row }">
        <div class="p-v-4">
          <div v-if="!row.isFound" class="p-h-16">
            <div v-if="row.notSure">
              <el-text type="warning" size="small"
                >未找到该场景,但找到了有相似名称的场景,请确定是哪个场景</el-text
              >
              <div class="m-t-8">
                <el-button
                  v-for="(v, i) in row.sourceScene"
                  :key="v.guid"
                  type="primary"
                  text
                  bg
                  size="small"
                  @click="handleRadioChange(v, row)"
                >
                  {{ v.name }}
                </el-button>
                <!-- <el-radio-group v-model="row.radioValue">
                <el-radio
                  v-for="(v, i) in row.sourceScene"
                  :key="v.guid"
                  :value="i"
                  size="small"
                  border
                  @change="handleRadioChange(v, row)"
                  >{{ v.name }}</el-radio
                >
              </el-radio-group> -->
              </div>
            </div>
            <div v-else>
              <el-text type="danger" size="small"
                >未找到该场景,也没有任何相似名称的场景,请修改场景名称或去除该场景</el-text
              >
            </div>
          </div>
          <div v-else class="p-h-16">
            <el-text type="success" size="small"> 已正确匹配到该场景 </el-text>
          </div>
        </div>
      </template>
    </el-table-column>
  </el-table>
  <el-button
    type="primary"
    :loading="uploadLoading"
    icon="upload"
    @click="uploadFile"
    >上传统计结果</el-button
  >
</template>
<script setup>
import { ref, reactive, watch, onMounted, getCurrentInstance } from 'vue';
import { genFileId } from 'element-plus';
import monitordataApi from '@/api/fysp/monitordataApi';
import sceneApi from '@/api/fysp/sceneApi';
import * as XLSX from 'xlsx';
 
const cns = getCurrentInstance();
const $fm = cns.appContext.config.globalProperties.$fm;
 
const props = defineProps({
  areaInfo: { type: Object }
});
 
let workbook;
const isUploadNewFile = ref(false);
const data = ref([]);
const expandRowKeys = ref([]);
const upload = ref();
const tableLoading = ref(false);
const loadTxt = ref('');
const tips = ref('');
const uploadLoading = ref(false);
 
const tableRowClassName = ({ row, rowIndex }) => {
  if (row.loading) {
    return 'loading-row';
  } else if (row.isFound == undefined) {
    return '';
  } else {
    return row.isFound
      ? 'success-row'
      : row.notSure
        ? 'warning-row'
        : 'danger-row';
  }
};
 
// 获取历史统计结果
function fetchDustDataResult() {
  monitordataApi.fetchDustDataResult(props.areaInfo).then((res) => {
    data.value = res.data;
  });
}
 
function handleExceed(files, uploadFiles) {
  upload.value.clearFiles();
  const file = files[0];
  file.uid = genFileId();
  upload.value.handleStart(file);
}
 
function handleChange(uploadFile, uploadFiles) {
  expandRowKeys.value = [];
  tableLoading.value = true;
  loadTxt.value = '文件解析中...';
  // console.log(uploadFile, uploadFiles);
  const fileReader = new FileReader();
  fileReader.onload = (file) => {
    const fileData = file.target.result;
    workbook = XLSX.read(fileData, { type: 'array' });
    console.log(workbook.SheetNames);
    if (workbook.SheetNames.length == 0) {
      tips.value = 'excel文件错误,没有sheet表单';
      return;
    }
    const worksheet = workbook.Sheets[workbook.SheetNames[0]];
    const tableData = XLSX.utils.sheet_to_json(worksheet);
    data.value = tableData.map((v, i) => {
      return reactive({
        id: i,
        drSceneName: v['名称'],
        drDeviceCode: v['设备号'],
        drTime: $fm.formatDateFromExcel(v['时间'], '-'),
        drExceedTimes: v['超标次数'],
        drAvg: v['平均值'],
        drMax: v['最大值'],
        drMin: v['最小值'],
        drOverAvgPer: v['超区均值百分比'],
        drDataNum: v['数据量'],
        drEffectiveRate: v['有效率']
      });
    });
    // console.log(tableData);
    setTimeout(() => {
      tableLoading.value = false;
      isUploadNewFile.value = true;
      data.value.forEach((d) => {
        searchScene(d);
      });
    }, 1000);
  };
  fileReader.readAsArrayBuffer(uploadFile.raw);
}
 
// 查询从文件上传的每个场景是否能在系统中找到对应的场景信息
function searchScene(d) {
  d.loading = true;
  if (!d.drSceneName) {
    d.isFound = false;
    d.loading = false;
    return Promise;
  } else {
    sceneApi
      .findScene({ name: d.drSceneName })
      .then((res) => {
        setTimeout(() => {
          if (res.length > 0) {
            const findRes = res.find((v) => v.name == d.drSceneName);
            if (findRes) {
              d.drSceneId = res[0].guid;
              d.isFound = true;
            } else {
              d.isFound = false;
              d.notSure = true;
              expandRowKeys.value.push(d.id);
            }
            d.sourceScene = res;
          } else {
            d.isFound = false;
            expandRowKeys.value.push(d.id);
          }
          d.loading = false;
        }, 1000);
      })
      .finally(() => {
        setTimeout(() => {
          d.loading = false;
        }, 1000);
      });
  }
}
 
function handleSceneNameChange(newName, row) {
  searchScene(row);
}
 
function handleRadioChange(value, row) {
  const scene = value;
  row.drSceneId = scene.guid;
  row.drSceneName = scene.name;
  searchScene(row);
}
 
// 上传统计结果文档
function uploadFile() {
  monitordataApi.uploadDustDataResult(data.value).then((res) => {});
}
 
onMounted(() => {
  fetchDustDataResult();
});
</script>
<style scoped>
.upload-file {
  /* background-color: aliceblue; */
  width: 300px;
  min-height: 60px;
}
 
:deep(.el-text) {
  align-self: auto;
}
 
:deep(.el-table__expanded-cell) {
  padding: 0;
  /* background-color: var(--el-bg-color-page); */
}
 
/* :deep(.el-table__body tr>td.hover-cell) {
  background-color: red !important;
} */
/* .el-table--enable-row-hover
  .el-table__body
  tr:hover
  > td
  :deep(.el-table__cell) {
  background-color: red !important;
} */
</style>
<style>
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-5);
}
.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-7);
}
.el-table .danger-row {
  --el-table-tr-bg-color: var(--el-color-danger-light-5);
}
.el-table .loading-row {
  color: var(--el-text-color-disabled);
  /* --el-table-tr-bg-color: var(--el-text-color-placeholder); */
  --el-table-tr-bg-color: var(--el-bg-color);
}
/* .el-table__body tr>td.hover-cell {
  background-color: red !important;
} */
/* .el-table--enable-row-hover .el-table__body tr:hover>td.el-table__cell {
  background-color: unset;
} */
</style>