hcong
2024-10-18 e95ca9ef89c79fbff8f0d1394311f5f18d653cdd
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
<template>
  <ChangeableColumnTable
    @search="onSearch"
    :pagination="false"
    :cols="cols"
    ref="tableRef"
    @select-updated="selectUpdated"
    @select-batch-updated="selectBatchUpdated"
  >
    <template #options>
      <!-- 区县 -->
      <FYOptionLocation
        :allOption="false"
        :level="3"
        :checkStrictly="false"
        v-model:value="formSearch.locations"
      ></FYOptionLocation>
      <!-- 场景类型 -->
      <FYOptionScene
        :allOption="false"
        :type="2"
        v-model:value="formSearch.scenetype"
      ></FYOptionScene>
      <!-- 时间 -->
      <FYOptionTime :initValue="false" type="month" v-model:value="formSearch.time"></FYOptionTime>
    </template>
    <template #buttons>
      <!-- <el-button icon="Download" size="default" type="success" @click="download"
        >规范性评估与分析报告</el-button
      > -->
 
      <CompReport
        :locations="formSearch.locations"
        :scenetype="formSearch.scenetype"
        :time="formSearch.time"
      ></CompReport>
    </template>
    <template #options-expand>
      <!-- <div class="flex-div flex-v-center">
        <el-form-item label="部分扣分">
          <el-select
            style="width: 310px"
            collapse-tags
            collapse-tags-tooltip
            v-model="partScoreHeads"
            multiple
            placeholder="部分扣分项"
            clearable
            filterable
            @change="onSelectionChange"
          >
            <el-option
              v-for="item in cols.filter((item) => 'children' in item)"
              :key="item"
              :label="item.label"
              :value="item.name"
            /> </el-select
        ></el-form-item>
        <el-tooltip placement="bottom-start" effect="light">
          <template #content>
            <el-text size="b">选中的条目对应的扣分总和将会显示在表格中的部分扣分列中</el-text><br />
          </template>
          <el-icon class="m-l-8 cursor-p" :size="16" color="var(--el-color-warning)"
            ><QuestionFilled
          /></el-icon>
        </el-tooltip>
      </div>-->
 
      <el-form :inline="true">
        <CompQuickSet @quick-set="setOptions"></CompQuickSet>
      </el-form>
    </template>
  </ChangeableColumnTable>
</template>
<script>
import ChangeableColumnTable from './components/ChangeableColumnTable.vue';
import CompReport from './components/CompReport.vue';
import evaluateApi from '@/api/fysp/evaluateApi';
import { useCloned } from '@vueuse/core';
import dayjs from 'dayjs';
import { ElMessage } from 'element-plus';
import { useMessageBox, useMessageBoxTip } from '../../../composables/messageBox';
export default {
  components: {
    ChangeableColumnTable,
    CompReport,
  },
  data() {
    return {
      partScoreHeads: [],
      editRecordSwitch: false,
      dataReady: false,
      area: {},
      formSearch: {
        locations: {},
        scenetype: {},
        time: dayjs().add(-1, 'M').date(1).toDate()
      },
      refreshTable: true,
      cols: [],
      tableData: []
    };
  },
  methods: {
    onSelectionChange() {
      for (const tableItem of this.tableData) {
        tableItem.partSum = 0;
        for (const headItem of this.partScoreHeads) {
          if (headItem in tableItem) {
            tableItem.partSum += tableItem[headItem];
          } else {
            tableItem.partSum = '';
          }
        }
      }
    },
    // 指定添加的表头顺序
    addHead(headObj, index) {
      this.cols.splice(index, 0, headObj);
    },
    async selectBatchUpdated(rows, name) {
      var promises = [];
      for (const row of rows) {
        if (!row.subTaskId || row.subTaskId == null) {
          for (var attr in row) {
            if (attr.endsWith('ScoreSelect')) {
              row[attr] = false;
            }
          }
          continue;
        }
        var checkedUpdatedList = [];
        for (const attr in row) {
          if (attr.endsWith('ScoreSelect') && Boolean(row[attr])) {
            var id = attr.split('ScoreSelect')[0];
            checkedUpdatedList.push(id);
          }
        }
        promises.push(
          evaluateApi
            .updateScore({
              subTaskId: row.subTaskId,
              itemList: checkedUpdatedList
            })
            .then(() => {
              evaluateApi.fetchItemEvaluation(row.subTaskId).then((res) => {
                this.updateChangeableData(res.data.details, res.data.score, row.subTaskId);
              });
            })
        );
      }
      var reSend = true;
      while (promises.length !== 0 && reSend) {
        reSend = false;
        await Promise.allSettled(promises).then((results) => {
          results.forEach((result, index) => {
            if (result.status === 'fulfilled') {
            } else {
              // 成功了删掉这个promise
              promises.splice(index, 1);
            }
          });
        });
 
        ElMessage({
          message: '全部成功',
          type: 'success',
          plain: true
        });
        if (promises.length == 0) {
          // ElMessage({
          //   message: '全部成功',
          //   type: 'success',
          //   plain: true
          // });
        } else {
          // useMessageBoxTip({
          //   confirmMsg: '是否重试',
          //   confirmTitle: '批量更新失败',
          //   onConfirm: async () => {
          //     reSend = true;
          //   }
          // });
        }
      }
    },
    // 选中更改的回调
    selectUpdated(row) {
      if (!row.subTaskId || row.subTaskId == null) {
        for (var attr in row) {
          if (attr.endsWith('ScoreSelect')) {
            row[attr] = false;
          }
        }
        return;
      }
      var checkedUpdatedList = [];
      for (const attr in row) {
        if (attr.endsWith('ScoreSelect') && Boolean(row[attr])) {
          var id = attr.split('ScoreSelect')[0];
          checkedUpdatedList.push(id);
        }
      }
      evaluateApi
        .updateScore({
          subTaskId: row.subTaskId,
          itemList: checkedUpdatedList
        })
        .then(() => {
          evaluateApi.fetchItemEvaluation(row.subTaskId).then((res) => {
            this.updateChangeableData(res.data.details, res.data.score, row.subTaskId);
          });
        });
    },
    _getParam() {
      const { locations, scenetype, time } = this.formSearch;
      return {
        provincecode: locations.pCode,
        provincename: locations.pName,
        citycode: locations.cCode,
        cityname: locations.cName,
        districtcode: locations.dCode,
        districtname: locations.dName,
        starttime: dayjs(time).format('YYYY-MM-DD HH:mm:ss'),
        scensetypeid: scenetype.value
      };
    },
    // 更新一行数据
    updateChangeableData(data, score, subTaskId) {
      for (let index = 0; index < this.tableData.length; index++) {
        const element = this.tableData[index];
        if (element.subTaskId == subTaskId) {
          element.resultscorebef = score;
          this.genChangeableData(data, element);
          // this.tableData[index] = element;
        }
      }
    },
    // 生成得分条目的数据
    genChangeableData(data, tableItem) {
      for (let index = 0; index < data.length; index++) {
        const element = data[index];
        tableItem[`${element.id}Score`] = element.score;
        if (element.select) {
          tableItem[`${element.id}ScoreSelect`] = element.select;
        }
        var twoLevel = element.subList;
 
        if (!twoLevel) {
          continue;
        }
        for (let index = 0; index < twoLevel.length; index++) {
          const threeElement = twoLevel[index];
          tableItem[`${threeElement.id}Score`] = threeElement.score;
          if (threeElement.select) {
            tableItem[`${threeElement.id}ScoreSelect`] = threeElement.select;
          }
          var threeLevel = threeElement.subList;
          if (!threeLevel) {
            continue;
          }
          for (let index = 0; index < threeLevel.length; index++) {
            const target = threeLevel[index];
            // 展示当前子规则的分值
            // tableItem[`${target.id}Score`] = target.maxScore;
            // 展示当前项的当前得分
            tableItem[`${target.id}Score`] = target.score;
            if (target.select) {
              tableItem[`${target.id}ScoreSelect`] = target.select;
            }
          }
        }
      }
    },
    // 生成得分条目的表头
    genChangeableHead(data) {
      // 一级表头
      for (let i = 0; i < data.length; i++) {
        const element = data[i];
        const col = {
          fixed: false
        };
        this.cols.push(col);
        col.label = element.title;
        col.name = `${element.id}Score`;
 
        for (const attr in element) {
          // 二级表头
          let children = [];
          col.children = children;
          if (attr === 'subList') {
            for (let j = 0; j < element[attr].length; j++) {
              const twoElement = element[attr][j];
              const child = {
                fixed: false
              };
              children.push(child);
              child.label = twoElement.title;
              child.name = `${twoElement.id}Score`;
 
              for (const attr2 in twoElement) {
                // 三级指标
                let children2 = [];
                child.children = children2;
                if (attr2 === 'subList') {
                  for (let k = 0; k < twoElement[attr2].length; k++) {
                    const threeElement = twoElement[attr2][k];
                    const child2 = {
                      fixed: false
                    };
                    children2.push(child2);
                    child2.label = threeElement.title;
                    child2.name = `${threeElement.id}Score`;
                    if (this.editRecordSwitch) {
                      child2.needSelect = true;
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    // 排名表头
    genRankHead() {
      var head = {
        name: 'rank',
        label: '总分排名',
        // fixed: 'left'
      };
      this.addHead(head, 0);
    },
    // 编号方法
    makeNumber(array, propName) {
      if (!(array instanceof Array)) {
        return;
      }
      // var copy = [];
      // array.forEach((item) => {
      //   copy.push(item);
      // });
 
      array.sort((a, b) => b[propName] - a[propName]);
      // var maxIndex = copy.length;
      var minIndex = 1;
      array.forEach((item) => {
        item.rank = minIndex++;
      });
    },
    // 排名数据
    genRankData() {
      this.makeNumber(this.tableData, 'resultscorebef');
    },
    // 基本信息表头
    genTableHeadBaseInfo(data) {
      var cols = [
        {
          name: 'sceneIndex',
          label: '编号',
          fixed: false,
          noncloseable: true
        },
        {
          name: 'createdate',
          label: '巡查日期',
          fixed: 'left',
        },
        {
          name: 'scensename',
          label: '名称',
          fixed: 'left',
        },
        {
          name: 'resultscorebef',
          label: '得分',
          fixed: false
        },
        {
          name: 'districtname',
          label: '区县',
          fixed: false
        },
        {
          name: 'townname',
          label: '街道',
          fixed: false
        },
        {
          name: 'scenseaddress',
          label: '地址',
          fixed: false
        }
      ];
      for (var col of cols) {
        this.cols.push(col);
      }
    },
    // 生成基本数据信息
    genTableDataBaseInfo(data, tableItem) {
      try {
        tableItem.sceneIndex = data.sceneIndex;
        tableItem.scensename = data.sceneName;
        tableItem.districtname = data.dname;
        tableItem.townname = data.tname;
        if (!data.evaluation) {
          tableItem.createdate = '';
          tableItem.resultscorebef = '';
          tableItem.scenseaddress = '';
          return;
        }
        tableItem.createdate = data.evaluation.createdate
          ? dayjs(data.evaluation.createdate).format('MM-DD')
          : '';
        tableItem.resultscorebef = data.evaluation.resultscorebef || 0;
        tableItem.scenseaddress = data.evaluation.scenseaddress || '';
      } catch (e) {
        console.log(e.message, data);
      }
    },
    onSearch(page, func) {
      this.dataReady = false;
      this.tableData = [];
      this.cols = [];
      const area = this._getParam();
      var expandHeadOk = false;
      var baseHeadOk = false;
      var showExpand = true;
      evaluateApi
        .fetchAutoEvaluation(area)
        .then((res) => {
          for (const element of res.data) {
            if (element.subTaskId == null) {
              showExpand = false;
              // continue;
              // console.log('subTask为空');
            } else {
              showExpand = true;
            }
            var tableItem = {};
 
            if (!baseHeadOk) {
              this.genTableHeadBaseInfo(element);
              baseHeadOk = true;
            }
            if (showExpand) {
              evaluateApi
                .fetchItemEvaluation(element.subTaskId)
                .then((res) => {
                  const data = res.data.details;
                  tableItem = {
                    subTaskId: element.subTaskId
                  };
                  this.tableData.push(tableItem);
                  try {
                    if (!expandHeadOk) {
                      this.genChangeableHead(data);
                      expandHeadOk = true;
                    }
                  } catch (e) {
                    console.log('表头准备失败', e.message);
                  }
                  this.genTableDataBaseInfo(element, tableItem);
                  this.genChangeableData(data, tableItem);
                })
                .catch((error) => {
                  console.error('Error fetching item evaluation:', error);
                })
                .finally(() => {});
            } else {
              this.genTableDataBaseInfo(element, tableItem);
              this.tableData.push(tableItem);
            }
          }
        })
        .finally(() => {
          setTimeout(() => {
            this.dataReady = true;
            // 生成排名表头
            this.genRankHead();
            this.genRankData();
            func({ data: this.tableData });
          }, 1000);
        });
    },
    // 初始化调整状态
    initEditRecord() {
      if (!Boolean(this.$route.meta.readonly)) {
        this.editRecordSwitch = true;
      }
    },
    // 初始化
    init() {
      this.initEditRecord();
    },
 
    setOptions(param) {
      this.formSearch.locations = param.locations;
      this.formSearch.scenetype = param.scenetype;
      this.formSearch.sourceType = param.sourceType;
      this.$refs.tableRef.onSearch();
    }
  },
  created() {
    this.init();
  },
  mounted() {
    this.$refs.tableRef.onSearch();
  }
};
</script>
<style scoped>
.flex-div {
  display: flex;
}
.flex-v-center {
  align-items: center;
}
</style>