|
|
@@ -18,10 +18,7 @@
|
|
|
<!-- 序号列 -->
|
|
|
<el-table-column prop="seq" label="序号" width="80" align="center">
|
|
|
<template slot-scope="scope">
|
|
|
- <span v-if="scope.row.isCategory" class="category-seq">
|
|
|
- {{ scope.row.categorySeq }}
|
|
|
- </span>
|
|
|
- <span v-else>{{ scope.row.seq }}</span>
|
|
|
+ <span>{{ scope.row.seq }}</span>
|
|
|
</template>
|
|
|
</el-table-column>
|
|
|
|
|
|
@@ -90,6 +87,7 @@
|
|
|
|
|
|
<script>
|
|
|
import { Message } from 'element-ui'
|
|
|
+ import { saveSingleRecordSurvey, getSurveyDetail } from '@/api/audit/survey'
|
|
|
|
|
|
export default {
|
|
|
name: 'FixedTableDialog',
|
|
|
@@ -146,6 +144,26 @@
|
|
|
type: Boolean,
|
|
|
default: false,
|
|
|
},
|
|
|
+ // 被监审单位ID
|
|
|
+ auditedUnitId: {
|
|
|
+ type: String,
|
|
|
+ default: '',
|
|
|
+ },
|
|
|
+ // 上传记录ID
|
|
|
+ uploadId: {
|
|
|
+ type: String,
|
|
|
+ default: '',
|
|
|
+ },
|
|
|
+ // 成本调查表模板ID
|
|
|
+ surveyTemplateId: {
|
|
|
+ type: String,
|
|
|
+ default: '',
|
|
|
+ },
|
|
|
+ // 目录ID
|
|
|
+ catalogId: {
|
|
|
+ type: String,
|
|
|
+ default: '',
|
|
|
+ },
|
|
|
},
|
|
|
data() {
|
|
|
return {
|
|
|
@@ -157,10 +175,14 @@
|
|
|
},
|
|
|
watch: {
|
|
|
visible: {
|
|
|
- handler(newVal) {
|
|
|
+ async handler(newVal) {
|
|
|
this.dialogVisible = newVal
|
|
|
if (newVal) {
|
|
|
+ // 先初始化表格数据
|
|
|
this.initTableData()
|
|
|
+ // 等待 DOM 更新后,如果有 uploadId,调用接口获取详情数据并回显
|
|
|
+ await this.$nextTick()
|
|
|
+ this.loadDetailData()
|
|
|
}
|
|
|
},
|
|
|
immediate: true,
|
|
|
@@ -245,96 +267,215 @@
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- // 将嵌套结构转换为扁平结构(递归处理)
|
|
|
+ // 根据 parentid 关系排列:parentid 为 '-1' 的是父项,子项的 parentid 等于父项的 rowid/id
|
|
|
const flatData = []
|
|
|
- let seq = 1
|
|
|
+ const processedIds = new Set()
|
|
|
|
|
|
- const processItem = (item, parentCategory = null) => {
|
|
|
- if (item.isCategory) {
|
|
|
- // 分类行
|
|
|
- const categoryRow = {
|
|
|
- ...item,
|
|
|
- seq: item.categorySeq || item.id,
|
|
|
- isCategory: true,
|
|
|
- categorySeq: item.categorySeq || item.id,
|
|
|
- }
|
|
|
+ // 先处理所有父项(parentid 为 '-1')
|
|
|
+ this.tableItems.forEach((item) => {
|
|
|
+ const parentid = item.parentid
|
|
|
+ const rowid = item.rowid || item.id
|
|
|
|
|
|
- // 初始化年份数据(分类行也可以有数据)
|
|
|
- this.yearColumns.forEach((year) => {
|
|
|
- categoryRow[`year_${year}`] = item[`year_${year}`] || ''
|
|
|
- })
|
|
|
+ if (parentid === '-1' || parentid === -1) {
|
|
|
+ const rowData = this.createRowData(item, false, rowid)
|
|
|
+ flatData.push(rowData)
|
|
|
+ processedIds.add(rowid)
|
|
|
+ }
|
|
|
+ })
|
|
|
|
|
|
- // 如果有传入的数据,填充值
|
|
|
- if (this.surveyData && this.surveyData[item.id]) {
|
|
|
- const savedData = this.surveyData[item.id]
|
|
|
- this.yearColumns.forEach((year) => {
|
|
|
- if (savedData[year] !== undefined) {
|
|
|
- categoryRow[`year_${year}`] = savedData[year]
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
+ // 再处理所有子项,紧跟在父项后面,按正序排列
|
|
|
+ // 先收集所有子项,按序号或顺序排序
|
|
|
+ const childItems = this.tableItems.filter((item) => {
|
|
|
+ const rowid = item.rowid || item.id
|
|
|
+ const parentid = item.parentid
|
|
|
+ return (
|
|
|
+ !processedIds.has(rowid) && parentid !== '-1' && parentid !== -1
|
|
|
+ )
|
|
|
+ })
|
|
|
|
|
|
- flatData.push(categoryRow)
|
|
|
+ // 对子项按序号正序排序
|
|
|
+ childItems.sort((a, b) => {
|
|
|
+ const seqA =
|
|
|
+ a.seq !== undefined
|
|
|
+ ? typeof a.seq === 'number'
|
|
|
+ ? a.seq
|
|
|
+ : parseInt(a.seq) || 0
|
|
|
+ : 0
|
|
|
+ const seqB =
|
|
|
+ b.seq !== undefined
|
|
|
+ ? typeof b.seq === 'number'
|
|
|
+ ? b.seq
|
|
|
+ : parseInt(b.seq) || 0
|
|
|
+ : 0
|
|
|
+ return seqA - seqB
|
|
|
+ })
|
|
|
|
|
|
- // 处理分类下的子项
|
|
|
- if (item.children && Array.isArray(item.children)) {
|
|
|
- item.children.forEach((child) => {
|
|
|
- processItem(child, item)
|
|
|
- })
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 普通行
|
|
|
- const rowData = {
|
|
|
- ...item,
|
|
|
- seq: seq++,
|
|
|
- isCategory: false,
|
|
|
- }
|
|
|
+ // 需要多次遍历,确保所有子项都能找到父项
|
|
|
+ let hasUnprocessed = true
|
|
|
+ while (hasUnprocessed && childItems.length > 0) {
|
|
|
+ hasUnprocessed = false
|
|
|
+ const remainingItems = []
|
|
|
|
|
|
- // 如果有父分类,设置分类信息
|
|
|
- if (parentCategory) {
|
|
|
- rowData.categoryId = parentCategory.id
|
|
|
- rowData.categorySeq =
|
|
|
- parentCategory.categorySeq || parentCategory.id
|
|
|
+ childItems.forEach((item) => {
|
|
|
+ const rowid = item.rowid || item.id
|
|
|
+ const parentid = item.parentid
|
|
|
+
|
|
|
+ // 跳过已处理的项
|
|
|
+ if (processedIds.has(rowid)) {
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- // 初始化年份数据
|
|
|
- this.yearColumns.forEach((year) => {
|
|
|
- rowData[`year_${year}`] = item[`year_${year}`] || ''
|
|
|
+ // 查找父项在 flatData 中的位置
|
|
|
+ const parentIndex = flatData.findIndex((row) => {
|
|
|
+ const parentRowid = row.rowid || row.id
|
|
|
+ return parentRowid === parentid
|
|
|
})
|
|
|
|
|
|
- // 初始化备注
|
|
|
- rowData.remark = item.remark || ''
|
|
|
-
|
|
|
- // 如果有传入的数据,填充值
|
|
|
- if (this.surveyData && this.surveyData[item.id]) {
|
|
|
- const savedData = this.surveyData[item.id]
|
|
|
- this.yearColumns.forEach((year) => {
|
|
|
- if (savedData[year] !== undefined) {
|
|
|
- rowData[`year_${year}`] = savedData[year]
|
|
|
- }
|
|
|
- })
|
|
|
- if (savedData.remark !== undefined) {
|
|
|
- rowData.remark = savedData.remark
|
|
|
+ if (parentIndex >= 0) {
|
|
|
+ // 找到父项,插入到父项后面
|
|
|
+ // 找到父项后面最后一个子项的位置
|
|
|
+ let insertIndex = parentIndex + 1
|
|
|
+ while (
|
|
|
+ insertIndex < flatData.length &&
|
|
|
+ (flatData[insertIndex].rowid || flatData[insertIndex].id) !==
|
|
|
+ parentid &&
|
|
|
+ flatData[insertIndex].parentid === parentid
|
|
|
+ ) {
|
|
|
+ insertIndex++
|
|
|
}
|
|
|
+ const rowData = this.createRowData(item, true, rowid)
|
|
|
+ flatData.splice(insertIndex, 0, rowData)
|
|
|
+ processedIds.add(rowid)
|
|
|
+ } else {
|
|
|
+ // 如果找不到父项,标记为未处理,等待下一轮
|
|
|
+ remainingItems.push(item)
|
|
|
+ hasUnprocessed = true
|
|
|
}
|
|
|
+ })
|
|
|
+
|
|
|
+ // 更新待处理的子项列表
|
|
|
+ childItems.length = 0
|
|
|
+ childItems.push(...remainingItems)
|
|
|
+ }
|
|
|
|
|
|
+ // 处理剩余未找到父项的项(可能是数据异常)
|
|
|
+ this.tableItems.forEach((item) => {
|
|
|
+ const rowid = item.rowid || item.id
|
|
|
+ if (!processedIds.has(rowid)) {
|
|
|
+ const rowData = this.createRowData(item, false, rowid)
|
|
|
flatData.push(rowData)
|
|
|
+ processedIds.add(rowid)
|
|
|
+ }
|
|
|
+ })
|
|
|
|
|
|
- // 如果有子项,递归处理
|
|
|
- if (item.children && Array.isArray(item.children)) {
|
|
|
- item.children.forEach((child) => {
|
|
|
- processItem(child, item)
|
|
|
- })
|
|
|
+ this.tableData = flatData
|
|
|
+ },
|
|
|
+ // 创建行数据
|
|
|
+ createRowData(item, isChild, rowid) {
|
|
|
+ const rowData = {
|
|
|
+ ...item,
|
|
|
+ rowid: rowid || item.rowid || item.id,
|
|
|
+ seq:
|
|
|
+ item.seq !== undefined
|
|
|
+ ? item.seq
|
|
|
+ : isChild
|
|
|
+ ? ''
|
|
|
+ : item.categorySeq || item.id || '',
|
|
|
+ isCategory: item.isCategory || false,
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化年份数据
|
|
|
+ this.yearColumns.forEach((year) => {
|
|
|
+ rowData[`year_${year}`] = item[`year_${year}`] || ''
|
|
|
+ })
|
|
|
+
|
|
|
+ // 初始化备注
|
|
|
+ rowData.remark = item.remark || ''
|
|
|
+
|
|
|
+ // 如果有传入的数据,填充值
|
|
|
+ if (this.surveyData && this.surveyData[rowid]) {
|
|
|
+ const savedData = this.surveyData[rowid]
|
|
|
+ this.yearColumns.forEach((year) => {
|
|
|
+ if (savedData[year] !== undefined) {
|
|
|
+ rowData[`year_${year}`] = savedData[year]
|
|
|
}
|
|
|
+ })
|
|
|
+ if (savedData.remark !== undefined) {
|
|
|
+ rowData.remark = savedData.remark
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 处理所有项
|
|
|
- this.tableItems.forEach((item) => {
|
|
|
- processItem(item)
|
|
|
- })
|
|
|
+ return rowData
|
|
|
+ },
|
|
|
+ // 加载详情数据并回显
|
|
|
+ async loadDetailData() {
|
|
|
+ // 如果有 uploadId 和 auditedUnitId,调用接口获取详情数据
|
|
|
+ const uploadId =
|
|
|
+ this.uploadId || this.surveyData.uploadId || this.surveyData.id
|
|
|
+ const auditedUnitId =
|
|
|
+ this.auditedUnitId || this.surveyData.auditedUnitId
|
|
|
+
|
|
|
+ if (uploadId && auditedUnitId) {
|
|
|
+ try {
|
|
|
+ const params = {
|
|
|
+ uploadId: uploadId,
|
|
|
+ auditedUnitId: auditedUnitId,
|
|
|
+ }
|
|
|
+ const res = await getSurveyDetail(params)
|
|
|
+ console.log('固定表详情数据', res)
|
|
|
+ if (res && res.code === 200 && res.value) {
|
|
|
+ // 将接口返回的数据转换为表格数据格式
|
|
|
+ const detailData = Array.isArray(res.value)
|
|
|
+ ? res.value
|
|
|
+ : res.value.items || res.value.records || []
|
|
|
+
|
|
|
+ // 按 rowid 分组数据
|
|
|
+ const dataByRowid = {}
|
|
|
+ detailData.forEach((item) => {
|
|
|
+ if (item.rowid) {
|
|
|
+ if (!dataByRowid[item.rowid]) {
|
|
|
+ dataByRowid[item.rowid] = {}
|
|
|
+ }
|
|
|
+ dataByRowid[item.rowid][item.rkey] = item.rvalue
|
|
|
+ }
|
|
|
+ })
|
|
|
|
|
|
- this.tableData = flatData
|
|
|
+ // 回显数据到表格
|
|
|
+ this.tableData.forEach((row) => {
|
|
|
+ const rowid = row.rowid || row.id
|
|
|
+ const rowData = dataByRowid[rowid]
|
|
|
+
|
|
|
+ if (rowData) {
|
|
|
+ // 回显基本信息
|
|
|
+ if (rowData['序号'] !== undefined) {
|
|
|
+ row.seq = rowData['序号']
|
|
|
+ }
|
|
|
+ if (rowData['项目'] !== undefined) {
|
|
|
+ row.itemName = rowData['项目']
|
|
|
+ }
|
|
|
+ if (rowData['单位'] !== undefined) {
|
|
|
+ row.unit = rowData['单位']
|
|
|
+ }
|
|
|
+ if (rowData['备注'] !== undefined) {
|
|
|
+ row.remark = rowData['备注']
|
|
|
+ }
|
|
|
+
|
|
|
+ // 回显年份数据
|
|
|
+ this.yearColumns.forEach((year) => {
|
|
|
+ if (rowData[year] !== undefined) {
|
|
|
+ row[`year_${year}`] = rowData[year]
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ // 强制更新视图
|
|
|
+ this.$forceUpdate()
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ console.error('获取固定表详情失败', err)
|
|
|
+ }
|
|
|
+ }
|
|
|
},
|
|
|
// 获取假数据(用于测试)
|
|
|
getMockTableData() {
|
|
|
@@ -589,7 +730,7 @@
|
|
|
this.handleClose()
|
|
|
},
|
|
|
// 保存
|
|
|
- handleSave() {
|
|
|
+ async handleSave() {
|
|
|
// 验证数据
|
|
|
if (!this.validateBeforeSave()) {
|
|
|
const errorMessages = this.validationErrors
|
|
|
@@ -599,37 +740,120 @@
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- // 格式化保存数据
|
|
|
- const saveData = {
|
|
|
- items: this.tableData.map((row) => {
|
|
|
- const itemData = {
|
|
|
- id: row.id,
|
|
|
- itemName: row.itemName,
|
|
|
+ try {
|
|
|
+ // 判断是否有数据(编辑模式):如果有 uploadId,说明是编辑已有数据
|
|
|
+ const hasData = !!(
|
|
|
+ this.uploadId ||
|
|
|
+ this.surveyData.uploadId ||
|
|
|
+ this.surveyData.id
|
|
|
+ )
|
|
|
+
|
|
|
+ // 格式化保存数据为接口需要的格式
|
|
|
+ const saveData = []
|
|
|
+
|
|
|
+ // 遍历所有行数据
|
|
|
+ this.tableData.forEach((row) => {
|
|
|
+ const rowid = row.rowid || row.id
|
|
|
+
|
|
|
+ // 跳过分类行(如果分类行不需要保存基本信息)
|
|
|
+ if (!row.isCategory) {
|
|
|
+ // 保存基本信息:序号、项目、单位
|
|
|
+ if (row.seq !== undefined && row.seq !== null && row.seq !== '') {
|
|
|
+ saveData.push({
|
|
|
+ rowid: rowid,
|
|
|
+ rkey: '序号',
|
|
|
+ rvalue: String(row.seq),
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (row.itemName) {
|
|
|
+ saveData.push({
|
|
|
+ rowid: rowid,
|
|
|
+ rkey: '项目',
|
|
|
+ rvalue: String(row.itemName),
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ if (row.unit) {
|
|
|
+ saveData.push({
|
|
|
+ rowid: rowid,
|
|
|
+ rkey: '单位',
|
|
|
+ rvalue: String(row.unit),
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 添加年份数据
|
|
|
+ // 保存年份数据(所有行都可以有年份数据)
|
|
|
this.yearColumns.forEach((year) => {
|
|
|
- if (!row.isCategory) {
|
|
|
- itemData[year] = row[`year_${year}`] || ''
|
|
|
+ const yearValue = row[`year_${year}`]
|
|
|
+ if (
|
|
|
+ yearValue !== undefined &&
|
|
|
+ yearValue !== null &&
|
|
|
+ yearValue !== ''
|
|
|
+ ) {
|
|
|
+ saveData.push({
|
|
|
+ rowid: rowid,
|
|
|
+ rkey: String(year),
|
|
|
+ rvalue: String(yearValue),
|
|
|
+ })
|
|
|
}
|
|
|
})
|
|
|
|
|
|
- // 添加备注
|
|
|
- if (!row.isCategory) {
|
|
|
- itemData.remark = row.remark || ''
|
|
|
+ // 保存备注(所有行都可以有备注)
|
|
|
+ if (
|
|
|
+ row.remark !== undefined &&
|
|
|
+ row.remark !== null &&
|
|
|
+ row.remark !== ''
|
|
|
+ ) {
|
|
|
+ saveData.push({
|
|
|
+ rowid: rowid,
|
|
|
+ rkey: '备注',
|
|
|
+ rvalue: String(row.remark),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ // 为每条数据添加公共字段
|
|
|
+ const finalSaveData = saveData.map((item) => {
|
|
|
+ const dataItem = {
|
|
|
+ ...item,
|
|
|
+ auditedUnitId:
|
|
|
+ this.auditedUnitId || this.surveyData.auditedUnitId || '',
|
|
|
+ surveyTemplateId:
|
|
|
+ this.surveyTemplateId || this.surveyData.surveyTemplateId || '',
|
|
|
+ catalogId: this.catalogId || this.surveyData.catalogId || '',
|
|
|
}
|
|
|
|
|
|
- return itemData
|
|
|
- }),
|
|
|
- yearColumns: this.yearColumns,
|
|
|
- }
|
|
|
+ // 如果有数据(编辑模式),添加 uploadId 字段
|
|
|
+ if (hasData) {
|
|
|
+ dataItem.uploadId =
|
|
|
+ this.uploadId ||
|
|
|
+ this.surveyData.uploadId ||
|
|
|
+ this.surveyData.id ||
|
|
|
+ ''
|
|
|
+ }
|
|
|
|
|
|
- console.log('保存数据:', saveData)
|
|
|
- Message.success('保存成功')
|
|
|
+ return dataItem
|
|
|
+ })
|
|
|
|
|
|
- // 触发保存事件
|
|
|
- this.$emit('save', saveData)
|
|
|
- this.handleClose()
|
|
|
+ console.log('保存数据:', finalSaveData)
|
|
|
+
|
|
|
+ // 调用保存接口
|
|
|
+ const res = await saveSingleRecordSurvey(finalSaveData)
|
|
|
+ if (res && res.code === 200) {
|
|
|
+ Message.success('保存成功')
|
|
|
+ // 触发保存事件
|
|
|
+ this.$emit('save', finalSaveData)
|
|
|
+ // 触发刷新事件
|
|
|
+ this.$emit('refresh')
|
|
|
+ this.handleClose()
|
|
|
+ } else {
|
|
|
+ Message.error(res.message || '保存失败')
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ console.error('保存失败', err)
|
|
|
+ Message.error(err.message || '保存失败')
|
|
|
+ }
|
|
|
},
|
|
|
},
|
|
|
}
|