reconciliationShared.js 11.1 KB
import { deepClone } from "@/common/util";
import { uniqueBy } from "./arrayUniqueBy";
import { getUpgradeGoodsRowKey } from "./groundingUpgradeList";

/**
 * 根据类目树反查完整路径(三级类目信息填充)
 * @param {Array} tree - 类目树形数据(productType)
 * @param {Number} targetValue - 目标类目值(goodsType)
 * @returns {Object|null} 包含完整路径信息的对象 { goodsType, goodsTypeName, secondaryGoodsType, secondaryGoodsTypeName, thirdLevelGoodsType, thirdLevelGoodsTypeName }
 */
export function findCategoryPathInfo(tree, targetValue) {
	if (!tree || !Array.isArray(tree) || targetValue == null) {
		return null;
	}

	// 递归查找目标节点,并记录路径信息
	const result = _findNodePathWithInfo(tree, targetValue, []);
	return result;
}

/**
 * 递归查找目标节点,同时记录路径和节点信息
 * @param {Array} nodes - 当前层级的节点列表
 * @param {Number} targetValue - 目标类目值
 * @param {Array} currentPath - 当前路径信息数组 [{ categoryType, categoryName }]
 * @returns {Object|null} 包含完整路径信息的对象
 */
function _findNodePathWithInfo(nodes, targetValue, currentPath) {
	if (!nodes || !nodes.length) {
		return null;
	}

	for (const node of nodes) {
		const newPath = [...currentPath, { categoryType: node.categoryType, categoryName: node.categoryName }];

		// 优先递归查找子节点(确保找到最深层的匹配)
		if (node.children && node.children.length) {
			const childResult = _findNodePathWithInfo(node.children, targetValue, newPath);
			if (childResult) {
				return childResult; // 子节点找到匹配,返回子节点路径信息
			}
		}

		// 子节点没有匹配,再检查当前节点
		if (node.categoryType === targetValue) {
			// 根据路径长度构建返回对象
			const result = {};
			if (newPath.length === 1) {
				// 一级类目
				result.goodsType = newPath[0].categoryType;
				result.goodsTypeName = newPath[0].categoryName;
			} else if (newPath.length === 2) {
				// 二级类目
				result.goodsType = newPath[0].categoryType;
				result.goodsTypeName = newPath[0].categoryName;
				result.secondaryGoodsType = newPath[1].categoryType;
				result.secondaryGoodsTypeName = newPath[1].categoryName;
			} else if (newPath.length === 3) {
				// 三级类目
				result.goodsType = newPath[0].categoryType;
				result.goodsTypeName = newPath[0].categoryName;
				result.secondaryGoodsType = newPath[1].categoryType;
				result.secondaryGoodsTypeName = newPath[1].categoryName;
				result.thirdLevelGoodsType = newPath[2].categoryType;
				result.thirdLevelGoodsTypeName = newPath[2].categoryName;
			}
			return result;
		}
	}

	return null;
}

/**
 * 填充 goodsTypeGroupRules 中的 goodsTypes 三级类目信息
 * @param {Array} goodsTypeGroupRules - 分账品类规则数组
 * @param {Array} productType - 类目树形数据
 */
export function fillGoodsTypesCategoryInfo(goodsTypeGroupRules, productType, key) {
	if (!Array.isArray(goodsTypeGroupRules) || !Array.isArray(productType)) {
		return goodsTypeGroupRules;
	}
	goodsTypeGroupRules.forEach((rule) => {
		if (!rule || !Array.isArray(rule[key || "goodsTypes"])) {
			return;
		}
		rule[key || "goodsTypes"]?.forEach((item) => {
			if (!item || item.goodsType == null) {
				return;
			}

			// 根据最细一级类目值查找完整路径信息
			const targetValue = item.thirdLevelGoodsType || item.secondaryGoodsType || item.goodsType;
			const pathInfo = findCategoryPathInfo(productType, targetValue);

			if (pathInfo) {
				// 填充完整信息
				item.goodsType = pathInfo.goodsType;
				item.goodsTypeName = pathInfo.goodsTypeName;
				if (pathInfo.secondaryGoodsType) {
					item.secondaryGoodsType = pathInfo.secondaryGoodsType;
					item.secondaryGoodsTypeName = pathInfo.secondaryGoodsTypeName;
				}
				if (pathInfo.thirdLevelGoodsType) {
					item.thirdLevelGoodsType = pathInfo.thirdLevelGoodsType;
					item.thirdLevelGoodsTypeName = pathInfo.thirdLevelGoodsTypeName;
				}
			}
		});
	});

	return goodsTypeGroupRules;
}

/**
 * 商户对账 / 财务分账共用:嘀加商品来源 + 特殊品类时补齐 goodsTypeGroupRelations。
 */
export function fillDjGoodsTypeGroupRelations(rule) {
	if (!rule || typeof rule !== "object") {
		return rule;
	}
	if (rule.goodsSource != 1 || rule.groupType != 2) {
		return rule;
	}
	const existing = rule.goodsTypeGroupRelations;
	if (Array.isArray(existing) && existing.length > 0) {
		return rule;
	}

	let ct = rule.categoryType;
	let list = [];
	if (ct == 1 && rule.goodsTypes?.length) {
		list = deepClone(rule.goodsTypes);
	} else if (ct == 2 && rule.upgradeGoods?.length) {
		list = deepClone(rule.upgradeGoods);
	} else if (ct == 3 && rule.packageGoods?.length) {
		list = deepClone(rule.packageGoods);
	} else {
		const g = rule.goodsTypes?.length;
		const u = rule.upgradeGoods?.length;
		const p = rule.packageGoods?.length;
		if (g && !u && !p) {
			ct = 1;
			list = deepClone(rule.goodsTypes);
		} else if (!g && u && !p) {
			ct = 2;
			list = deepClone(rule.upgradeGoods);
		} else if (!g && !u && p) {
			ct = 3;
			list = deepClone(rule.packageGoods);
		}
	}
	if (!list.length) {
		return rule;
	}
	rule.categoryType = ct;
	rule.goodsTypeGroupRelations = list.map(row => ({
		...row,
		categoryType:
      row && row.categoryType != null && row.categoryType !== ""
      	? row.categoryType
      	: ct,
	}));
	return rule;
}

/** el-tabs 中排除「+」占位 pane */
export function filterDivisionTabPanes(list) {
	return Array.isArray(list)
		? list.filter(item => item && String(item.name) !== "add")
		: [];
}

function accumulateFromReconciliationRule(rule, sinks) {
	const { pushG, pushU, pushP } = sinks;
	if (!rule || typeof rule !== "object") {
		return;
	}
	const ct = rule.categoryType;
	const rel = Array.isArray(rule.goodsTypeGroupRelations)
		? rule.goodsTypeGroupRelations
		: [];
	const ug = Array.isArray(rule.upgradeGoods) ? rule.upgradeGoods : [];
	const pRaw = rule.packageGoods;
	const pg = Array.isArray(pRaw)
		? pRaw
		: pRaw && typeof pRaw === "object"
			? [pRaw]
			: [];
	const gt = Array.isArray(rule.goodsTypes) ? rule.goodsTypes : [];

	if (ct === 1) {
		;(rel.length ? rel : gt).forEach(pushG);
	} else if (ct === 2) {
		;(ug.length ? ug : rel).forEach(pushU);
	} else if (ct === 3) {
		;(pg.length ? pg : rel).forEach(pushP);
	} else {
		if (ug.length) {
			ug.forEach(pushU);
		} else if (pg.length) {
			pg.forEach(pushP);
		} else if (rel.length) {
			const inf = rel[0] && rel[0].categoryType;
			if (inf === 2) {
				rel.forEach(pushU);
			} else if (inf === 3) {
				rel.forEach(pushP);
			} else {
				rel.forEach(pushG);
			}
		} else if (gt.length) {
			gt.forEach(pushG);
		}
	}
}

/**
 * 从 goodsTypeGroupReconciliationRules + 可选占位 tab 快照聚合跨 Tab 已选品类/商品/套餐(对账编辑页)。
 * 与 {@link rebuildFinancialCrossTabGoodsSelections} 字段与去重策略相近;合并为单实现前需对财务/对账两页做回归(禁用列表、phantom 等差异)。
 */
export function rebuildCrossTabReconciliationSelections(
	rules,
	{ includePhantom = false, phantomRule = null } = {},
) {
	if (!Array.isArray(rules)) {
		return {
			goodsSelectList: [],
			selectUpgradeGoodsList: [],
			selectPackageGoodsList: [],
		};
	}
	const goodsAgg = [];
	const upgradeAgg = [];
	const packageAgg = [];
	const pushG = (item) => {
		if (!item || item.goodsType == null) {
			return;
		}
		goodsAgg.push(deepClone(item));
	};
	const pushU = (item) => {
		if (!item) {
			return;
		}
		const k = getUpgradeGoodsRowKey(item);
		if (!k) {
			return;
		}
		upgradeAgg.push(deepClone(item));
	};
	const pushP = (item) => {
		if (!item || item.packageCode == null) {
			return;
		}
		packageAgg.push(deepClone(item));
	};
	const sinks = { pushG, pushU, pushP };

	for (let i = 0; i < rules.length; i++) {
		accumulateFromReconciliationRule(rules[i], sinks);
	}
	if (
		includePhantom
		&& phantomRule
		&& typeof phantomRule === "object"
	) {
		accumulateFromReconciliationRule(phantomRule, sinks);
	}
	// 使用最细一级类目值做唯一性判断(与 classificationCpt 禁用逻辑一致)
	const getFinestValue = (item) => {
		if (item && item.thirdLevelGoodsType) {
			return item.thirdLevelGoodsType;
		}
		if (item && item.secondaryGoodsType) {
			return item.secondaryGoodsType;
		}
		return item ? item.goodsType : null;
	};

	return {
		goodsSelectList: uniqueBy(goodsAgg, x => getFinestValue(x)),
		selectUpgradeGoodsList: uniqueBy(
			upgradeAgg,
			x => getUpgradeGoodsRowKey(x),
		),
		selectPackageGoodsList: uniqueBy(
			packageAgg,
			x => x && x.packageCode,
		),
	};
}

/**
 * 财务分账页:从 goodsTypeGroupRules 聚合跨 Tab 已选(浅拷贝条目,与 classificationCpt 禁用列表语义一致)。
 * @see rebuildCrossTabReconciliationSelections — 对账侧聚合;两函数刻意分叉,合并前需专项测试。
 */
export function rebuildFinancialCrossTabGoodsSelections(rules) {
	if (!Array.isArray(rules) || !rules.length) {
		return {
			goodsSelectList: [],
			selectUpgradeGoodsList: [],
			selectPackageGoodsList: [],
		};
	}
	const hashG = {};
	const hashU = {};
	const hashP = {};
	const goodsAgg = [];
	const upgradeAgg = [];
	const packageAgg = [];
	// 使用最细一级类目值做唯一性判断(与 classificationCpt 禁用逻辑一致)
	const getFinestValue = (item) => {
		if (item && item.thirdLevelGoodsType) {
			return item.thirdLevelGoodsType;
		}
		if (item && item.secondaryGoodsType) {
			return item.secondaryGoodsType;
		}
		return item ? item.goodsType : null;
	};
	const pushG = (item) => {
		if (!item) {
			return;
		}
		const k = getFinestValue(item);
		if (!k) {
			return;
		}
		if (!hashG[k]) {
			hashG[k] = true;
			goodsAgg.push(typeof item === "object" ? { ...item } : item);
		}
	};
	const pushU = (item) => {
		if (!item) {
			return;
		}
		const k = getUpgradeGoodsRowKey(item);
		if (!k) {
			return;
		}
		if (!hashU[k]) {
			hashU[k] = true;
			upgradeAgg.push(typeof item === "object" ? { ...item } : item);
		}
	};
	const pushP = (item) => {
		if (!item || item.packageCode == null) {
			return;
		}
		const k = item.packageCode;
		if (!hashP[k]) {
			hashP[k] = true;
			packageAgg.push(typeof item === "object" ? { ...item } : item);
		}
	};
	for (let i = 0; i < rules.length; i++) {
		const rule = rules[i];
		if (!rule || typeof rule !== "object") {
			continue;
		}
		let g = Array.isArray(rule.goodsTypes) ? rule.goodsTypes : [];
		let u = Array.isArray(rule.upgradeGoods) ? rule.upgradeGoods : [];
		const pRaw = rule.packageGoods;
		let pList = [];
		if (Array.isArray(pRaw)) {
			pList = pRaw;
		} else if (pRaw && typeof pRaw === "object") {
			pList = [pRaw];
		}
		const rel = rule.goodsTypeGroupRelations;
		if (
			Array.isArray(rel)
			&& rel.length
			&& !g.length
			&& !u.length
			&& !pList.length
		) {
			const inferred = rel[0] && rel[0].categoryType;
			const ct = inferred != null ? inferred : rule.categoryType;
			if (ct === 2) {
				u = rel;
			} else if (ct === 3) {
				pList = rel;
			} else {
				g = rel;
			}
		}
		g.forEach(pushG);
		u.forEach(pushU);
		pList.forEach(pushP);
	}
	return {
		goodsSelectList: goodsAgg,
		selectUpgradeGoodsList: upgradeAgg,
		selectPackageGoodsList: packageAgg,
	};
}