negotiation-summary-transform.js 1.66 KB
/**
 * 洽谈明细表汇总 — jsonContent 转只读区块模型(按行 recordRows)
 * 数据规则对齐 gl_tape negotiationList.getReportDetail;UI 对齐 form-group-repeater 只读
 */
import { getSectionRowFieldsList } from './app-subform-json.js'

function formatCellValue(value) {
	if (value == null || value === '') {
		return ''
	}
	if (typeof value === 'object') {
		if (value.displayText) {
			return String(value.displayText)
		}
		if (value.name) {
			return String(value.name)
		}
		return ''
	}
	return String(value)
}

function parseJsonContentList(jsonContent) {
	if (Array.isArray(jsonContent)) {
		return jsonContent
	}
	if (typeof jsonContent !== 'string' || !jsonContent.trim()) {
		return []
	}
	try {
		const parsed = JSON.parse(jsonContent)
		return Array.isArray(parsed) ? parsed : []
	} catch {
		return []
	}
}

function mapRowToFields(childRow) {
	return (childRow || [])
		.filter(field => field?.key)
		.map(field => ({
			label: field.label || field.key || '',
			value: formatCellValue(field.value)
		}))
}

/**
 * @param {string|Array} jsonContent detail.get 返回的 jsonContent
 * @returns {Array<{ label: string, recordRows: Array<{ fields: Array<{ label: string, value: string }> }> }>}
 */
export function buildNegotiationSummaryTables(jsonContent) {
	const list = parseJsonContentList(jsonContent)
	return list
		.filter(item => item?.type === 'subform')
		.map(item => {
			const children = getSectionRowFieldsList(item)
			if (!children.length) {
				return null
			}
			const recordRows = children.map(childRow => ({
				fields: mapRowToFields(childRow)
			}))
			return {
				label: item.label || '',
				recordRows
			}
		})
		.filter(Boolean)
}