祖安之光
5 天以前 d4534c231f7c25ca64d40c8bcaadf83f64b640c3
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
<template>
  <div class="app-container">
    <div style="margin-bottom: 10px">
      <el-form style="display: flex;flex-wrap: wrap;">
        <el-form-item>
          <el-button
              type="primary"
              plain
              icon="Plus"
              @click="openDialog('add',{})"
              v-hasPermi="['qualityFinancialAnalysis:list:add']"
          >新增</el-button>
        </el-form-item>
        <el-form-item label="单位名称:" v-if="data.isAdmin" style="margin-left: 20px">
          <el-select v-model="data.queryParams.companyId" placeholder="请选择" filterable clearable>
            <el-option
                v-for="item in data.companyList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年份:" style="margin-left: 20px">
          <!--            <el-input v-model="data.queryParams.year" placeholder="请输入年份"></el-input>-->
          <el-select
              v-model="data.queryParams.year"
              placeholder="请选择年份"
              style="width: 240px"
              filterable
              allow-create
              default-first-option
              :reserve-keyword="false"
              @change="handleChangeNum"
          >
            <el-option
                v-for="item in data.yearList"
                :key="item.value"
                :label="item.label"
                :value="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 30px" @click="searchClick">查询</el-button>
          <el-button plain @click="reset">重置</el-button>
        </el-form-item>
        <el-form-item style="margin-left: 15px">
          <el-button
              type="primary"
              @click="exportData"
          >导出</el-button>
        </el-form-item>
 
      </el-form>
    </div>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="dataList" :border="true"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
      <el-table-column label="单位名称" prop="companyName" align="center" v-if="data.isAdmin" />
      <el-table-column label="名称" prop="year" align="center"  >
        <template #default="scope">
          <span>{{scope.row.year}}年质量经济性分析报告</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" >
        <template #default="scope">
          <el-button link type="primary"  @click="openDialog('review',scope.row)">查看</el-button>
          <el-button link type="primary"  @click="openDialog('edit',scope.row)" v-hasPermi="['qualityFinancialAnalysis:list:edit']">编辑</el-button>
          <el-button link type="danger"  @click="handleDelete(scope.row)" v-hasPermi="['qualityFinancialAnalysis:list:del']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
 
    <div class="pag-container">
      <el-pagination
          v-model:current-page="data.queryParams.pageNum"
          v-model:page-size="data.queryParams.pageSize"
          :page-sizes="[10,15,20,25]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
    <editDialog ref="noticeRef" @getList = "getList"></editDialog>
  </div>
</template>
 
<script setup>
import {getCurrentInstance, onMounted, reactive, ref, toRefs} from "vue";
import editDialog from "./components/editDialog.vue"
import {ElMessage, ElMessageBox} from "element-plus";
import {getCompany} from "@/api/onlineEducation/company";
import Cookies from "js-cookie";
import { Document, Paragraph, TextRun, Table, TableRow, TableCell,BorderStyle , Packer, AlignmentType, HeadingLevel } from "docx";
import { saveAs } from "file-saver";
import {delEconomy, getEconomy} from "@/api/qualityFinancialAnalysis";
const { proxy } = getCurrentInstance();
const loading = ref(false);
const noticeRef = ref();
const loadingCompany = ref(false)
const choosedData = ref([])
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    companyId: null,
    year: '',
    type: ''
  },
  companyList: [],
  isAdmin: false,
  yearList: [
    {
      value: 1,
      label: '2025'
    },
    {
      value: 2,
      label: '2024'
    },
    {
      value: 3,
      label: '2023'
    },
    {
      value: 4,
      label: '2022'
    },
    {
      value: 5,
      label: '2021'
    },
  ],
});
const dataList = ref([]);
const total = ref(0);
 
const { queryParams } = toRefs(data);
 
onMounted(() => {
  const userInfo = JSON.parse(Cookies.get('userInfo'))
  console.log("userInfo",userInfo)
  data.isAdmin = userInfo.userType === 0;
  if(data.isAdmin){
    data.queryParams.companyId = null
  }else {
    data.queryParams.companyId = userInfo.companyId
  }
  getList();
  if(data.isAdmin){
    getCompanyList()
  }
});
const getList = async () => {
  loading.value = true;
  const res = await getEconomy(data.queryParams);
  if(res.code === 200){
    dataList.value = res.data.list
    total.value = res.data.total
  }else{
    ElMessage.warning(res.message)
  }
  loading.value = false;
}
 
const searchClick = () => {
  getList();
}
const openDialog = (type, value) => {
  noticeRef.value.openDialog(type, value,data.companyList);
}
const selectValue = (val) => {
  data.companyList.forEach(item => {
    if(item.name === val){
      data.queryParams.companyId = item.id
    }
  })
}
 
const getCompanyList = async ()=>{
  const queryParams = {
    pageNum: 1,
    pageSize: 999
  }
  const res = await getCompany(queryParams)
  if (res.code == 200) {
    data.companyList = res.data.list?res.data.list:[]
    // data.queryParams.companyId = data.companyList[0].id
  } else {
    ElMessage.warning(res.message)
  }
}
 
const handleSizeChange = (val) => {
  data.queryParams.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  data.queryParams.pageNum = val
  getList()
}
 
/** 重置新增的表单以及其他数据  */
function reset() {
  if(data.isAdmin){
    data.queryParams = {
      companyId: '',
      pageNum: 1,
      pageSize: 10,
      year: '',
      type: ''
    }
    choosedData.value = []
    data.companyList = [];
    getCompanyList()
  }else {
    data.queryParams = {
      companyId: data.queryParams.companyId,
      pageNum: 1,
      pageSize: 10,
      year: '',
      type: ''
    }
  }
  getList();
 
}
const exportData = () => {
  if(choosedData.value && choosedData.value.length === 0){
    ElMessage.warning('请选择需要导出的数据')
  }else {
    startGeneration()
  }
}
 
const templatePath = ref('/qualityFinancialAnalysisExample.docx')
const startGeneration = async () => {
  let name = ''
  choosedData.value.forEach(item => {
    // 执行导出
    createQualityEconomicReport(item).catch(console.error);
  })
}
 
const handleSelectionChange = (val) => {
  choosedData.value = val
}
const handleDelete = (val) => {
  ElMessageBox.confirm(
      '确定删除此条数据?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      .then( async() => {
        const res = await delEconomy(val.id);
        if(res.code === 200){
          ElMessage({
            type: 'success',
            message: '删除成功'
          });
          getList();
        }else{
          ElMessage.warning(res.message)
        }
      })
}
const handleChangeNum = (value) => {
  if (!/^\d+$/.test(value)) { // 验证是否为数字
    ElMessage.warning('只能输入数字')
    data.queryParams.year = '' // 重置选择,避免非法值被添加到options中
  } else if (!data.yearList.some(option => option.label == value)) { // 确保不是已存在的选项
    data.yearList.push({ value, label: value }); // 添加新选项(这里简单地将值和标签设为相同)
  }
}
 
 
function processHtmlTable(htmlTable) {
  const docxRows = [];
 
  htmlTable.querySelectorAll("tr").forEach(htmlRow => {
    const cells = [];
    let colIndex = 0;
 
    htmlRow.querySelectorAll("th, td").forEach(htmlCell => {
      // 处理合并单元格
      const colspan = parseInt(htmlCell.getAttribute('colspan')) || 1;
      const rowspan = parseInt(htmlCell.getAttribute('rowspan')) || 1;
 
      // 处理单元格内容(包括空格的转换)
      const cellText = htmlCell.innerHTML === '&nbsp;' ? ' ' : htmlCell.textContent;
 
      // 创建Word表格单元格
      cells.push(
          new TableCell({
            children: [
              new Paragraph({
                children: [new TextRun(cellText)],
              })
            ],
            columnSpan: colspan,
            rowSpan: rowspan,
            margins: { top: 100, bottom: 100, left: 100, right: 100 },
            borders: {
              top: { style: BorderStyle.SINGLE, size: 4, color: "000000" },
              bottom: { style: BorderStyle.SINGLE, size: 4, color: "000000" },
              left: { style: BorderStyle.SINGLE, size: 4, color: "000000" },
              right: { style: BorderStyle.SINGLE, size: 4, color: "000000" }
            }
          })
      );
 
      colIndex += colspan;
    });
 
    docxRows.push(new TableRow({ children: cells }));
  });
 
  return docxRows;
}
function convertHtmlToDocx(htmlString) {
  const parser = new DOMParser();
  const doc = parser.parseFromString(htmlString, "text/html");
  const elements = [];
 
  Array.from(doc.body.childNodes).forEach(node => {
    if (node.nodeType === Node.ELEMENT_NODE) {
      const tagName = node.tagName.toUpperCase();
 
      if (tagName === 'TABLE') {
        elements.push(new Table({
          rows: processHtmlTable(node),
          width: { size: 100, type: "pct" },  // 宽度100%
          margins: { top: 400, bottom: 400 },
          layout: "fixed",  // 固定布局
          borders: {
            top: { style: BorderStyle.SINGLE, size: 4, color: "000000" },
            bottom: { style: BorderStyle.SINGLE, size: 4, color: "000000" },
            left: { style: BorderStyle.SINGLE, size: 4, color: "000000" },
            right: { style: BorderStyle.SINGLE, size: 4, color: "000000" },
            insideHorizontal: { style: BorderStyle.SINGLE, size: 4, color: "000000" },
            insideVertical: { style: BorderStyle.SINGLE, size: 4, color: "000000" }
          }
        }));
      } else if (tagName === 'P') {
        elements.push(new Paragraph({
          text: node.textContent,
          spacing: { after: 200 }
        }));
      }
    }
  });
 
  return elements;
}
 
// 2. 示例HTML表格数据
const sampleTableHTML = `
b
`;
// 3. 创建完整报告文档
async function createQualityEconomicReport(data) {
 
  const doc = new Document({
    sections: [{
      properties: {},
      children: [
        // 报告标题
        new Paragraph({
          children: [
            new TextRun({
              text: "质量经济性分析报告",
              font: "宋体",
              size: 26,
              bold: true,
              color: "000000"
            })
          ],
          heading: HeadingLevel.HEADING_1,
          alignment: AlignmentType.CENTER,
          spacing: { after: 300 }
        }),
 
        // // 报告时间范围
        // new Paragraph({
        //   text: data.dutyTime,
        //   alignment: AlignmentType.CENTER,
        //   indent: { left: 3700 },
        //   spacing: { after: 500 }
        // }),
 
        // 1. 基本情况
        new Paragraph({
          children: [
            new TextRun({
              text: "一、基本情况",
              font: "宋体",
              size: 22,
              bold: true,
              color: "000000"
            })
          ],
          heading: HeadingLevel.HEADING_1,
          alignment: AlignmentType.LEFT,
          spacing: { after: 100 }
        }),
        //基本情况内容
        new Paragraph({
          children: [
            new TextRun({
              text: data.basic, // 替换为实际内容
              font: "宋体", // 与标题一致
              size: 20, // 小四号(20 half-points)
              bold: false, // 常规
              color: "000000" // 纯黑
            })
          ],
          alignment: AlignmentType.LEFT,
          spacing: { after: 300 } // 和下一段的间距
        }),
        // 二、综合分析
        new Paragraph({
          children: [
            new TextRun({
              text: "二、综合分析",
              font: "宋体",
              size: 22,
              bold: true,
              color: "000000"
            })
          ],
          heading: HeadingLevel.HEADING_1,
          alignment: AlignmentType.LEFT,
          spacing: { after: 100 }
        }),
        // 二、综合分析内容
        ...convertHtmlToDocx(data.synthesize),
        new Paragraph({
          children: [
          ],
          heading: HeadingLevel.HEADING_1,
          alignment: AlignmentType.LEFT,
          spacing: { after: 100 }
        }),
 
 
        // 三、内外部损失
        new Paragraph({
          children: [
            new TextRun({
              text: "三、内外部损失",
              font: "宋体",
              size: 22,
              bold: true,
              color: "000000"
            })
          ],
          heading: HeadingLevel.HEADING_1,
          alignment: AlignmentType.LEFT,
          spacing: { after: 100 }
        }),
        //内外部损失内容
        new Paragraph({
          children: [
            new TextRun({
              text: data.damage, // 替换为实际内容
              font: "宋体", // 与标题一致
              size: 20, // 小四号(20 half-points)
              bold: false, // 常规
              color: "000000" // 纯黑
            })
          ],
          alignment: AlignmentType.LEFT,
          spacing: { after: 300 } // 和下一段的间距
        }),
 
        // 四、纠正措施
        new Paragraph({
          children: [
            new TextRun({
              text: "四、纠正措施",
              font: "宋体",
              size: 22,
              bold: true,
              color: "000000"
            })
          ],
          heading: HeadingLevel.HEADING_1,
          alignment: AlignmentType.LEFT,
          spacing: { after: 100 }
        }),
        //纠正措施内容
        new Paragraph({
          children: [
            new TextRun({
              text: data.measure, // 替换为实际内容
              font: "宋体", // 与标题一致
              size: 20, // 小四号(20 half-points)
              bold: false, // 常规
              color: "000000" // 纯黑
            })
          ],
          alignment: AlignmentType.LEFT,
          spacing: { after: 300 } // 和下一段的间距
        }),
          //五、分析结果
        new Paragraph({
          children: [
            new TextRun({
              text: "六、分析结果",
              font: "宋体",
              size: 22,
              bold: true,
              color: "000000"
            })
          ],
          heading: HeadingLevel.HEADING_1,
          alignment: AlignmentType.LEFT,
          spacing: { after: 100 }
        }),
        //分析结果内容
        new Paragraph({
          children: [
            new TextRun({
              text: data.suggest, // 替换为实际内容
              font: "宋体", // 与标题一致
              size: 20, // 小四号(20 half-points)
              bold: false, // 常规
              color: "000000" // 纯黑
            })
          ],
          alignment: AlignmentType.LEFT,
          spacing: { after: 300 } // 和下一段的间距
        }),
 
        // 二、改进建议
        new Paragraph({
          children: [
            new TextRun({
              text: "六、改进建议",
              font: "宋体",
              size: 22,
              bold: true,
              color: "000000"
            })
          ],
          heading: HeadingLevel.HEADING_1,
          alignment: AlignmentType.LEFT,
          spacing: { after: 100 }
        }),
        //改进建议内容
        new Paragraph({
          children: [
            new TextRun({
              text: data.suggest, // 替换为实际内容
              font: "宋体", // 与标题一致
              size: 20, // 小四号(20 half-points)
              bold: false, // 常规
              color: "000000" // 纯黑
            })
          ],
          alignment: AlignmentType.LEFT,
          spacing: { after: 300 } // 和下一段的间距
        }),
 
        // 10. 综合部落款
        new Paragraph({
          text: data.dutyName,
          alignment: AlignmentType.RIGHT,
          spacing: { before: 1200 }
        }),
 
        // 11. 日期落款
        new Paragraph({
          text: data.dutyTime,
          alignment: AlignmentType.RIGHT,
          spacing: { before: 200 }
        })
      ]
    }]
  });
  // 导出文件
  const blob = await Packer.toBlob(doc);
  saveAs(blob, data.companyName + "_质量经济性分析报告.docx");
}
 
 
 
 
</script>
<style lang="scss">
.pag-container{
  float: right;
  margin-top: 10px;
}
</style>