mapDetailResponse.js 1.86 KB
import { mapSceneType } from "./mapSceneType";

function normalizeContextList(value) {
	if (Array.isArray(value)) {
		return value;
	}
	if (value == null || value === "") {
		return [];
	}
	return String(value).split(",").filter(Boolean);
}

function resolveBusinessValue(...values) {
	const value = values.find(item => item != null && item !== "");
	return value ?? "";
}

/**
 * 详情路由 query → dailyReport.detail.select 请求体
 */
export function buildDetailSelectParams({ id } = {}) {
	return { id };
}

/**
 * dailyReport.detail.select 响应 → 详情页 detail 对象
 */
export function mapDetailResponse(res = {}, fallback = {}) {
	const sceneType = res.sceneType ?? fallback.sceneType;
	const sceneContext = {
		baseSceneCode: res.baseSceneCode ?? fallback.baseSceneCode,
		sceneList: res.sceneList ?? fallback.sceneList,
	};
	const preciseScene = mapSceneType(undefined, sceneContext);
	const legacyScene = mapSceneType(sceneType);

	return {
		...res,
		id: res.id ?? res.dailyReportRecordId ?? fallback.id,
		staffName: res.staffName ?? fallback.staffName,
		merchantName: res.merchantName ?? fallback.merchantName,
		templateType: res.templateType ?? fallback.templateType,
		bizType: resolveBusinessValue(
			res.bizType,
			preciseScene.bizType,
			fallback.bizType,
			legacyScene.bizType,
		),
		bizTypeName: resolveBusinessValue(
			res.bizTypeName,
			preciseScene.bizTypeName,
			fallback.bizTypeName,
			legacyScene.bizTypeName,
		),
		reportDate: res.reportDate ?? fallback.reportDate,
		jsonContent: res.jsonContent,
		fieldDataList: res.fieldDataList,
		subformDataList: res.subformDataList,
		reportTemplateId: res.reportTemplateId,
		baseSceneCode: sceneContext.baseSceneCode,
		sceneType,
		schema: res.schema,
		brandIds: normalizeContextList(res.brandIds ?? fallback.brandIds),
		sceneList: sceneContext.sceneList ?? [],
	};
}

export default mapDetailResponse;