index.js 11.3 KB
import store from "@/store";

const commissionTypesCoverCommissionBase = {
	BL: "GMV",
	XS: "DDTC",
	GDJE: "SJDDS",
};
export function convertDataToBackend(formData) {
	const productDefineList = store.getters["PerformanceFormStore/getProductDefineListOptions"];
	const goodsCategory = store.getters["PerformanceFormStore/getGoodsCategory"];
	const { paramConfigList, merchantBrandRefList, ...data } = formData;

	const requestData = {

		// 注意是scheme
		schemeName: data.schemeName,
		schemeType: data.schemeType,
		businessType: data.businessType,
		effectiveMonth: data.effectiveMonth,
		performanceConfigRule: {
			assessmentFactors: data.assessmentFactorValue.join(","),
			commissionBases: data.commissionBases.join(","),
			commissionTypes: data.commissionTypes.join(","),
			goodsConfigTypes: data.goodsConfigTypes.join(","),
		},
		paramConfigList: [],
		merchantBrandRefList: [],
	};
	if (data.originPerformanceConfigId) {
		requestData.originPerformanceConfigId = data.originPerformanceConfigId;
	}
	for (const paramConfig of paramConfigList) {
		// paramConfig ->  每一份表格
		const { templateType, ParamsType1, ParamsType1Products, ParamsType2 } = paramConfig;

		if (templateType === "ParamsType1") {
			// 处理ParamsType1 类型的表格
			coverParamsType1(ParamsType1, ParamsType1Products, (item, { rowItem, row }, { productColItem, colIndex }) => {
				const table = {
					...item,
					assessmentFactor: paramConfig.assessmentFactorOptions.value,
					goodsConfigType: paramConfig.goodsCategory,
					commissionTypeValue: productColItem.productValue[row],
				};
				console.log("paramConfig.ParamsType1Products[colIndex].productLabel");
				if (paramConfig.ParamsType1Products[colIndex]?.productLabel?.[0] === -1) {
					table.goodsConfigType = -1;
				} else {
					Object.assign(table, paramConfig.goodsCategory === 0
						? buildGoodsTypePayload(goodsCategory, paramConfig.ParamsType1Products[colIndex].productLabel)
						: buildProductDefinePayload(productDefineList, paramConfig.ParamsType1Products[colIndex].productLabel));
				}
				requestData.paramConfigList.push(table);
			});
		}

		if (templateType === "ParamsType2") {
			coverParamsType2(ParamsType2, (result, { paramsType2Item, paramsType2Index }) => {
				const table = {
					...result,
					commissionType: paramConfig.ParamsType2CommissionTypes,
					commissionBase: commissionTypesCoverCommissionBase[paramConfig.ParamsType2CommissionTypes],
					assessmentFactor: paramConfig.assessmentFactorOptions.value,
					goodsConfigType: paramConfig.goodsCategory,
					performanceParamConfigConditions: getPerformanceParamConfigConditions(paramsType2Item, paramConfig, goodsCategory, productDefineList),
				};

				if (paramsType2Item.productLabel?.[0] === -1) {
					table.goodsConfigType = -1;
				} else {
					Object.assign(table, paramConfig.goodsCategory === 0
						? buildGoodsTypePayload(goodsCategory, paramsType2Item.productLabel)
						: buildProductDefinePayload(productDefineList, paramsType2Item.productLabel));
				}

				requestData.paramConfigList.push(table);
			});
		}
	}
	console.log("setMerchantBrandRefList", merchantBrandRefList);
	// 整理门店
	if (merchantBrandRefList && merchantBrandRefList.length) {
		requestData.merchantBrandRefList = buildMerchantBrandPayload(merchantBrandRefList, data.businessType);
	}

	return requestData;
}
// 通过paramsConfig 获取 configCondition的列表
function getPerformanceParamConfigConditions(paramsType2Item, paramsConfig, goodsCategory, productDefineList) {
	const buildGoodsTypePayloadOptions = {
		level1Id: "conditionGoodsType",
		level2Id: "conditionSecondaryGoodsType",
		level3Id: "conditionThirdLevelGoodsType",
		level1Name: "conditionGoodsTypeName",
		level2Name: "conditionSecondaryGoodsTypeName",
		level3Name: "conditionThirdLevelGoodsTypeName",
	};
	const buildProductDefinePayloadOptions = {
		id: "conditionProductDefineId",
		name: "conditionProductDefineName",
		type: "conditionGoodsConfigType",
	};
	if (paramsType2Item.condition === 0) {
		return [];
	}
	const conditionItem = [{
		conditionType: paramsType2Item.condition,
		conditionGoodsConfigType: paramsConfig.goodsCategory,
		// conditionStartValue: "",
		// conditionEndValue: "",
	}];

	// 合并到 conditionItem 的第一位里
	let extend = {};

	const conditionParamsData = paramsType2Item[`conditionType${paramsType2Item.condition}`];

	// 如果 conditionParamsData 不存在,直接返回空的 conditionItem
	if (!conditionParamsData) {
		return conditionItem;
	}

	if (paramsType2Item.condition === 1) {
		// 检查四个渗透率字段是否有值
		const hasPermeabilityValue
			= (conditionParamsData.permeabilityMax !== undefined && conditionParamsData.permeabilityMax !== null && conditionParamsData.permeabilityMax !== "")
				|| (conditionParamsData.permeabilityMax1 !== undefined && conditionParamsData.permeabilityMax1 !== null && conditionParamsData.permeabilityMax1 !== "")
				|| (conditionParamsData.permeabilityMin !== undefined && conditionParamsData.permeabilityMin !== null && conditionParamsData.permeabilityMin !== "")
				|| (conditionParamsData.permeabilityMin1 !== undefined && conditionParamsData.permeabilityMin1 !== null && conditionParamsData.permeabilityMin1 !== "");

		// 如果四个渗透率字段全部没有值,直接返回空的 conditionItem,不进行后续处理
		if (!hasPermeabilityValue) {
			return conditionItem;
		}

		extend.conditionStartValue = conditionParamsData.permeabilityMin;
		extend.conditionEndValue = conditionParamsData.permeabilityMax;
		if (conditionParamsData?.productValue?.[0] === -1) {
			extend.conditionGoodsConfigType = -1;
		} else {
			extend = {
				...extend,
				...paramsConfig.goodsCategory === 0
					? buildGoodsTypePayload(goodsCategory, conditionParamsData.productValue || [], buildGoodsTypePayloadOptions)
					: buildProductDefinePayload(productDefineList, conditionParamsData.productValue || [], buildProductDefinePayloadOptions),
			};
		}

		let conditionItem1 = {
			conditionType: paramsType2Item.condition,
			conditionStartValue: conditionParamsData.permeabilityMin1,
			conditionEndValue: conditionParamsData.permeabilityMax1,
			conditionGoodsConfigType: paramsConfig.goodsCategory,
		};
		if (conditionParamsData.productValue1?.[0] === -1) {
			conditionItem1.conditionGoodsConfigType = -1;
		} else {
			conditionItem1 = {
				...conditionItem1,
				...paramsConfig.goodsCategory === 0
					? buildGoodsTypePayload(goodsCategory, conditionParamsData.productValue1 || [], buildGoodsTypePayloadOptions)
					: buildProductDefinePayload(productDefineList, conditionParamsData.productValue1 || [], buildProductDefinePayloadOptions),
			};
		}
		conditionItem.push(conditionItem1);
	}
	if (paramsType2Item.condition === 2) {
		extend.conditionEndValue = conditionParamsData.deductRate;
		extend.conditionCommissionTypeValue = conditionParamsData.perOrderDeductAmount;
	}
	if (paramsType2Item.condition === 3) {
		extend.conditionCommissionTypeValue = conditionParamsData.increaseReward;
	}
	if (paramsType2Item.condition === 4) {
		if (conditionParamsData?.productValue?.[0] === -1) {
			extend.conditionGoodsConfigType = -1;
		} else {
			extend = {
				...extend,
				...paramsConfig.goodsCategory === 0
					? buildGoodsTypePayload(goodsCategory, conditionParamsData.productValue || [], buildGoodsTypePayloadOptions)
					: buildProductDefinePayload(productDefineList, conditionParamsData.productValue || [], buildProductDefinePayloadOptions),
			};
		}
	}

	conditionItem[0] = {
		...conditionItem[0],
		...extend,
	};

	return conditionItem;
}

function buildProductDefinePayload(tree, pathIds, options = {
	id: "productDefineId",
	name: "productDefineName",
	type: "goodsConfigType",
}) {
	console.log("buildProductDefinePayload", tree, pathIds);
	// 如果 pathIds 不存在或不是数组,返回空对象
	if (!pathIds || !Array.isArray(pathIds) || pathIds.length === 0) {
		return {
			[options.id]: null,
			[options.name]: null,
			[options.type]: null,
		};
	}
	console.log("buildProductDefinePayload", tree, pathIds);

	let currentLevel = tree;
	let targetNode = null;

	for (const id of pathIds) {
		targetNode = currentLevel.find(item => item.value === id);
		if (!targetNode)
			break;
		currentLevel = targetNode.children || [];
	}
	console.log("buildProductDefinePayload currentLevel", currentLevel);
	console.log("buildProductDefinePayload targetNode", targetNode);

	return {
		[options.id]: targetNode?.value ?? null,
		[options.name]: targetNode?.label ?? null,
		[options.type]: targetNode?.defineType,
	};
}

function buildGoodsTypePayload(tree, pathIds, options = {
	level1Id: "goodsType",
	level1Name: "goodsTypeName",
	level2Id: "secondaryGoodsType",
	level2Name: "secondaryGoodsTypeName",
	level3Id: "thirdLevelGoodsType",
	level3Name: "thirdLevelGoodsTypeName",

}) {
	const result = {};

	// 如果 pathIds 不存在或不是数组,返回空对象
	if (!pathIds || !Array.isArray(pathIds) || pathIds.length === 0) {
		return result;
	}
	console.log("pathIds", pathIds);

	console.log("tree", tree);
	pathIds.forEach((id, index) => {
		let node = {};
		traverseTree(tree, (_node) => {
			if (id === _node.categoryType) {
				node = _node;
			}
		});
		if (!node)
			return;

		if (index === 0) {
			result[options.level1Id] = node.categoryType;
			result[options.level1Name] = node.categoryName;
		} else if (index === 1) {
			result[options.level2Id] = node.categoryType;
			result[options.level2Name] = node.categoryName;
		} else if (index === 2) {
			result[options.level3Id] = node.categoryType;
			result[options.level3Name] = node.categoryName;
		}
	});

	return result;
}
function traverseTree(tree, callback) {
	if (!Array.isArray(tree))
		return;

	for (const node of tree) {
		callback(node);

		if (Array.isArray(node.children) && node.children.length) {
			traverseTree(node.children, callback);
		}
	}
}

function coverParamsType1(ParamsType1, ParamsType1Products, fn) {
	ParamsType1.forEach((rowItem, row) => {
		ParamsType1Products.forEach((productColItem, colIndex) => {
			const result = {
				templateType: 1,
				startValue: rowItem.priceRange[0],
				endValue: rowItem.priceRange[1],
				commissionType: rowItem.commissionTypes,
				commissionBase: commissionTypesCoverCommissionBase[rowItem.commissionTypes],
			};
			// 还要加多 一个goodsConfigType 字段
			if (fn && typeof fn === "function") {
				fn(result, { rowItem, row }, { productColItem, colIndex });
			}
		});
	});
}

// 整理商户品牌数据
function coverParamsType2(ParamsType2, fn) {
	ParamsType2.forEach((rowItem, rowIndex) => {
		const result = {
			templateType: 2,
			startValue: rowItem.priceRange[0],
			endValue: rowItem.priceRange[1],
			commissionTypeValue: rowItem.condition === 0 ? rowItem.conditionType0 : rowItem[`conditionType${rowItem.condition}`].triggerCondition,
		};
		// 还要加多 一个goodsConfigType 字段
		if (fn && typeof fn === "function") {
			fn(result, {
				paramsType2Item: rowItem,
				paramsType2Index: rowIndex,

			});
		}
	});
}

// 本地数据 门店品牌数据  转成后端所需格式
export function buildMerchantBrandPayload(
	merchantBrandRefList,
	businessType,
) {
	const result = [];

	merchantBrandRefList.forEach((merchant) => {
		const {
			merchantId,
			merchantName,
			checkBrands = [],
		} = merchant;

		checkBrands.forEach((brand) => {
			result.push({
				merchantId,
				merchantName,
				brandId: brand.brandId,
				brandName: brand.brandName,
				childId: brand.childId,
				childName: brand.childName,
				businessType,
			});
		});
	});

	return result;
}