car-brand-value.js 6.7 KB
/**
 * 四级车型值模型 — @see packageDynamicForm/docs/ARCHITECTURE.md
 */

/** 待 App 联调:schema childKey -> 后端 fieldKey 列表 */
const CAR_BRAND_SUBMIT_KEY_MAP = {
	// 示例: 'F3-1-6': ['vehicleBrandId', 'vehicleBrandName', ...]
}

/** 计划到店客户导入的三级值兼容元数据(不参与完整性校验) */
export const PARTIAL_FROM_PLAN_IMPORT_KEY = 'partialFromPlanImport'

const CAR_BRAND_FIELD_KEYS = [
	'vehicleBrandId',
	'vehicleBrandName',
	'vehicleSubBrandId',
	'vehicleSubBrandName',
	'vehicleSeriesId',
	'vehicleSeriesName',
	'vehicleShapeId',
	'vehicleShapeName'
]

/** 拼接品牌/子品牌/车系/车型名称为 displayText */
export function buildDisplayText(carInfo = {}) {
	const parts = [
		carInfo.vehicleBrandName,
		carInfo.vehicleSubBrandName,
		carInfo.vehicleSeriesName,
		carInfo.vehicleShapeName
	].filter(part => part != null && part !== '')
	return parts.join('/')
}

/** 将原始值(字符串 JSON / 对象)归一化为 carInfo 对象 */
export function normalizeCarBrandValue(raw) {
	if (raw == null || raw === '') {
		return null
	}
	if (typeof raw === 'string') {
		try {
			const parsed = JSON.parse(raw)
			if (parsed && typeof parsed === 'object') {
				return normalizeCarBrandObject(parsed)
			}
		} catch (error) {
			return {
				displayText: raw
			}
		}
		return { displayText: raw }
	}
	if (typeof raw === 'object') {
		return normalizeCarBrandObject(raw)
	}
	return null
}

function normalizeCarBrandObject(obj = {}) {
	if (Array.isArray(obj)) {
		return cascadeLevelsToCarInfo(obj)
	}
	const next = {}
	CAR_BRAND_FIELD_KEYS.forEach(key => {
		if (obj[key] != null && obj[key] !== '') {
			next[key] = obj[key]
		}
	})
	if (obj.displayText) {
		next.displayText = obj.displayText
	} else {
		next.displayText = buildDisplayText(next)
	}
	if (obj[PARTIAL_FROM_PLAN_IMPORT_KEY] === true) {
		next[PARTIAL_FROM_PLAN_IMPORT_KEY] = true
	}
	return next
}

/** 从选车组件 pick 结果归一化为 carInfo 对象 */
export function normalizeCarInfoFromPick(carInfo = {}) {
	const next = normalizeCarBrandValue(carInfo)
	if (!next) return null
	if (!next.displayText) {
		next.displayText = buildDisplayText(next)
	}
	return next
}

/**
 * 是否为计划到店导入的三级车型(未点编辑前可提交)
 * @param {*} value
 * @returns {boolean}
 */
export function hasPartialPlanImportMarker(value) {
	return (
		value != null &&
		typeof value === 'object' &&
		!Array.isArray(value) &&
		value[PARTIAL_FROM_PLAN_IMPORT_KEY] === true
	)
}

/**
 * 清除导入兼容元数据
 * @param {Object} [carInfo]
 * @returns {Object|undefined|null}
 */
export function clearPartialPlanImportMarker(carInfo) {
	if (!hasPartialPlanImportMarker(carInfo)) {
		return carInfo
	}
	const next = { ...carInfo }
	delete next[PARTIAL_FROM_PLAN_IMPORT_KEY]
	return next
}

function applyPartialPlanImportMarkerIfPartial(carInfo) {
	if (!carInfo) {
		return carInfo
	}
	if (
		carInfo.vehicleSeriesId != null &&
		carInfo.vehicleSeriesId !== '' &&
		(carInfo.vehicleShapeId == null || carInfo.vehicleShapeId === '')
	) {
		return { ...carInfo, [PARTIAL_FROM_PLAN_IMPORT_KEY]: true }
	}
	return carInfo
}

/**
 * 详情回显:已保存的三级车型还原导入豁免标记(与计划到店导入一致)
 * @param {*} raw carInfo 对象 / 四级 JSON 数组 / 字符串
 * @returns {*}
 */
export function maybeRestoreCarBrandPlanImportMarker(raw) {
	if (raw == null || raw === '') {
		return raw
	}
	const normalized = normalizeCarBrandValue(raw)
	if (!normalized) {
		return raw
	}
	if (hasPartialPlanImportMarker(normalized)) {
		return normalized
	}
	if (normalized.vehicleShapeId != null && normalized.vehicleShapeId !== '') {
		return normalized
	}
	return applyPartialPlanImportMarkerIfPartial(normalized)
}

/**
 * 判断车型值是否满足必填校验:车系层级即可完整提交。
 * partialFromPlanImport 仅保留为旧数据兼容元数据,不作为校验门槛。
 */
export function isCarBrandComplete(value) {
	const normalized = normalizeCarBrandValue(value)
	return !!(
		normalized &&
		normalized.vehicleSeriesId != null &&
		normalized.vehicleSeriesId !== ''
	)
}

/**
 * 校验/引擎取值:App 双 key 时优先 field[key] / 行 map,避免仅用 displayText 的 value
 * @param {Object} [rowField] cache 中的 FormField
 * @param {string} [childKey]
 * @param {*} [rowMapValue] values[groupKey][rowIndex][childKey]
 */
export function resolveCarBrandValueForValidation(rowField, childKey, rowMapValue) {
	const meta = rowField?.[childKey]
	if (meta != null && meta !== '' && typeof meta === 'object') {
		return meta
	}
	if (rowMapValue != null && rowMapValue !== '' && typeof rowMapValue === 'object') {
		return rowMapValue
	}
	if (rowField?.value !== undefined) {
		return rowField.value
	}
	return rowMapValue
}

const CASCADE_LEVEL_SPECS = [
	{ idKey: 'vehicleBrandId', nameKey: 'vehicleBrandName' },
	{ idKey: 'vehicleSubBrandId', nameKey: 'vehicleSubBrandName' },
	{ idKey: 'vehicleSeriesId', nameKey: 'vehicleSeriesName' },
	{ idKey: 'vehicleShapeId', nameKey: 'vehicleShapeName' }
]

/** flat carInfo → 四级 [{ id, name }, ...] */
export function carInfoToCascadeLevels(carInfo = {}) {
	const normalized = normalizeCarBrandValue(carInfo)
	if (!normalized) return []
	return CASCADE_LEVEL_SPECS.map(({ idKey, nameKey }) => ({
		id: normalized[idKey] != null ? String(normalized[idKey]) : '',
		name: normalized[nameKey] != null ? String(normalized[nameKey]) : ''
	})).filter(level => level.id !== '' || level.name !== '')
}

/** 四级 [{ id, name }, ...] → flat carInfo */
export function cascadeLevelsToCarInfo(levels = []) {
	if (!Array.isArray(levels) || !levels.length) {
		return null
	}
	const next = {}
	CASCADE_LEVEL_SPECS.forEach(({ idKey, nameKey }, index) => {
		const level = levels[index]
		if (!level) return
		const id = level.id ?? level.value
		const name = level.name ?? level.label
		if (id != null && id !== '') {
			next[idKey] = String(id)
		}
		if (name != null && name !== '') {
			next[nameKey] = String(name)
		}
	})
	if (!Object.keys(next).length) {
		return null
	}
	next.displayText = buildDisplayText(next)
	return applyPartialPlanImportMarkerIfPartial(next)
}

/**
 * fieldDataList 单条:四级数组 JSON
 */
export function flattenCarBrandForSubmit(childKey, value) {
	const levels = carInfoToCascadeLevels(value)
	if (!levels.length) return []

	const mapKeys = CAR_BRAND_SUBMIT_KEY_MAP[childKey]
	if (Array.isArray(mapKeys) && mapKeys.length) {
		const normalized = normalizeCarBrandValue(value)
		return mapKeys
			.filter(key => normalized[key] != null && normalized[key] !== '')
			.map(fieldKey => ({
				fieldKey,
				fieldValue: normalized[fieldKey]
			}))
	}

	return [
		{
			fieldKey: childKey,
			fieldValue: levels
		}
	]
}

export { CAR_BRAND_SUBMIT_KEY_MAP, CAR_BRAND_FIELD_KEYS }