customerSnapshot.js 7.85 KB
const SNAPSHOT_KEY = "customerDetailSnapshot";

export const CUSTOMER_STATUS_LIST = [
	{ confId: 1, confLabel: "待跟进" },
	{ confId: 2, confLabel: "已升级" },
	{ confId: 3, confLabel: "战败" },
	{ confId: 4, confLabel: "已退款" },
];

export function buildCustomerStatusFilterOptions() {
	return [
		{ confId: "", confLabel: "全部" },
		...CUSTOMER_STATUS_LIST,
	];
}

export function saveCustomerSnapshot(row) {
	if (!row) {
		return;
	}
	try {
		sessionStorage.setItem(SNAPSHOT_KEY, JSON.stringify(row));
	} catch (err) {
		// ignore quota errors
	}
}

export function loadCustomerSnapshot() {
	try {
		const raw = sessionStorage.getItem(SNAPSHOT_KEY);
		return raw ? JSON.parse(raw) : {};
	} catch (err) {
		return {};
	}
}

/** 合并列表行快照与路由参数,客户分类仅来自列表数据 */
export function resolveCustomerRowFromRoute(route = {}, snapshotRow = {}) {
	const row = { ...snapshotRow };
	if (route.query && route.query.customerClass) {
		row.customerClass = route.query.customerClass;
	}
	if (route.query && route.query.customerStatus !== undefined && route.query.customerStatus !== "") {
		row.customerStatus = route.query.customerStatus;
	}
	return row;
}

function formatDateValue(value) {
	if (!value) {
		return "";
	}
	return new Date(value).toLocaleDateString();
}

export function formatCarModel(row = {}) {
	const carModel = (row.carModel || "").trim();
	if (carModel) {
		return carModel;
	}
	const parts = [row.carBrand, row.carSeriesName, row.carTypeName]
		.map(item => (item || "").trim())
		.filter(Boolean);
	return parts.length ? parts.join("-") : "";
}

export function formatCarModelForList(row = {}) {
	const value = formatCarModel(row);
	return value || "";
}

export function formatListPhone(row = {}) {
	return row.customerPhone || row.mobile || row.phone || row.phoneNumber || "";
}

export function formatCustomerClass(row = {}) {
	const customerClass = row.customerClass || row.customerCategory;
	if (!customerClass) {
		return "";
	}
	const normalized = String(customerClass).trim().toUpperCase();
	if (normalized === "A" || normalized === "A类") {
		return "A类";
	}
	if (normalized === "B" || normalized === "B类") {
		return "B类";
	}
	if (normalized === "C" || normalized === "C类") {
		return "C类";
	}
	return String(customerClass);
}

export function formatGroupStatus(row = {}) {
	if (row.groupStatus === true || row.groupStatus === "已关联") {
		return "已关联";
	}
	return "未关联";
}

export function formatDeliveryTime(row = {}) {
	const time = row.deliveryDate;

	return formatDateValue(time);
}

export function formatRecorder(row = {}) {
	return row.userName || row.advanceUserName || row.createdBy || "";
}

export function formatReferrer(row = {}) {
	return row.referrerName
		|| row.employeeName
		|| (row.order && row.order.employeeName)
		|| "";
}

export function formatCustomerStatus(row = {}, statusList = CUSTOMER_STATUS_LIST) {
	const customerStatus = row.customerStatus;
	if (customerStatus == null || customerStatus === "") {
		return "-";
	}
	const matched = statusList.find(item => Number(item.confId) === Number(customerStatus));
	return matched ? matched.confLabel : "-";
}

export function formatServiceTime(row = {}) {
	const time = row.order && row.order.installUpgradeTime;
	if (!time) {
		return "-";
	}
	return new Date(time).toLocaleDateString();
}

export function formatRemark(row = {}) {
	if (!row.order) {
		return "无";
	}
	if (row.order.upgradeFlag === "N") {
		return row.order.customer
			? (row.order.customer.refusalReason || "无")
			: "无";
	}
	return row.order.remark || "无";
}

export function formatSource(row = {}) {
	const source = row.order ? row.order.source : row.source;
	if (source === 3) {
		return "现场升级";
	}
	if (source === 5) {
		return "核销抵扣";
	}
	if (source === 13) {
		return "PAD录入";
	}
	return "系统录入";
}

export function formatCreateTime(row = {}) {
	if (!row.createdDate) {
		return "";
	}
	return formatDateValue(row.createdDate);
}

export function mapBasicInfoFromRow(row = {}, statusList = CUSTOMER_STATUS_LIST) {
	const deliveryTime = formatDeliveryTime(row);
	const referrer = formatReferrer(row);
	const recorder = formatRecorder(row);
	return {
		customerName: row.customerName || "-",
		mobile: formatListPhone(row) || "-",
		customerCategory: formatCustomerClass(row) || "-",
		carModel: formatCarModel(row) || "-",
		customerStatus: formatCustomerStatus(row, statusList),
		serviceTime: formatServiceTime(row),
		remark: formatRemark(row),
		trafficSource: row.merchantName || "-",
		referrer: referrer || "-",
		parkStore: row.yqMerchantName || "-",
		conversionStaff: (row.order && row.order.sellerName) || "-",
		groupRelInfo: formatGroupStatus(row),
		createMethod: formatSource(row),
		createTime: formatCreateTime(row) || "-",
		deliveryTime: deliveryTime || "-",
		recorder: recorder || "-",
	};
}

function buildProfileCarModel(res = {}) {
	const carModel = (res.carModel || "").trim();
	if (carModel) {
		return carModel;
	}
	const parts = [res.carBrand, res.carSeriesName]
		.map(item => (item || "").trim())
		.filter(Boolean);
	return parts.length ? parts.join("-") : "";
}

export function mapProfileResponse(res = {}, snapshotRow = {}) {
	const snapshotBasic = mapBasicInfoFromRow(snapshotRow);
	const deliveryTime = res.deliveryDate
		? formatDateValue(res.deliveryDate)
		: snapshotBasic.deliveryTime;

	const basicInfo = {
		...snapshotBasic,
		customerName: res.customerName || snapshotBasic.customerName,
		mobile: res.customerPhone || snapshotBasic.mobile,
		carModel: buildProfileCarModel(res) || snapshotBasic.carModel,
		trafficSource: res.merchantName || snapshotBasic.trafficSource,
		deliveryTime: deliveryTime || "-",
		// 客户分类、客户状态仅来自列表接口,详情接口不返回
		customerCategory: snapshotBasic.customerCategory,
		customerStatus: snapshotBasic.customerStatus,
	};

	const consumptionTag = res.consumptionTag || {};
	const productTag = res.productTag || {};
	const behaviorTag = res.behaviorTag || {};

	const consumptionDimensions = (consumptionTag.dimensions || []).map(dimension => ({
		dimensionName: dimension.dimensionName || "-",
		tags: (dimension.enumValues || []).map(item => item.enumName).filter(Boolean),
	}));

	const productDimensions = [];

	const dealTags = (productTag.dealEnumValues || [])
		.map(item => item.enumName)
		.filter(Boolean);
	if (dealTags.length) {
		productDimensions.push({ dimensionName: "已成交品类", tags: dealTags });
	}

	const noDealTags = (productTag.noDealEnumValues || [])
		.map(item => item.enumName)
		.filter(Boolean);
	if (noDealTags.length) {
		productDimensions.push({ dimensionName: "未成交品类", tags: noDealTags });
	}

	const behaviorTagList = [];
	(behaviorTag.dimensions || []).forEach((dimension) => {
		(dimension.enumValues || []).forEach((enumValue) => {
			behaviorTagList.push({
				dimensionName: dimension.dimensionName || "-",
				enumName: enumValue.enumName || "",
				basis: enumValue.scoreBasis || "",
			});
		});
	});

	return {
		basicInfo,
		consumptionScore: consumptionTag.totalScore || 0,
		consumptionDimensions,
		productScore: productTag.totalScore || 0,
		productDimensions,
		behaviorScore: behaviorTag.totalScore || 0,
		behaviorTagList,
	};
}

export function mapGroupAssocResponse(res = {}, snapshotRow = {}) {
	const snapshotBasic = mapBasicInfoFromRow(snapshotRow);
	const groupRelStatus = res.groupStatus || "未关联";

	const basicInfo = {
		...snapshotBasic,
		customerName: res.customerName || snapshotBasic.customerName,
		mobile: res.customerPhone || snapshotBasic.mobile,
		// 客户分类、客户状态仅来自列表接口,详情接口不返回
		customerCategory: snapshotBasic.customerCategory,
		customerStatus: snapshotBasic.customerStatus,
		// 接口返回后同步基础信息中的群关联状态
		groupRelInfo: groupRelStatus,
	};

	return {
		basicInfo,
		groupRelStatus,
		groupName: res.qwGroupName || "",
		groupMemberCustomer: res.wxName || "",
	};
}