convertBackendToData.js 9.7 KB
import { groupBy, isArray } from "lodash";
import { assessmentFactorOptions, paramConfigListTemplate } from "../components/performanceRuleSelection/options";
import { setBackendAllGoods, setBackendConditionGoods, setBackendGoods } from "./help";

export function convertBackendToData(data) {
	// merchantBrandModelPageBrands merchantBrandRefList 接口返回的字段不一样
	const { merchantBrandRefList, paramConfigList, performanceConfigRule, ...lists } = data;
	const response = {
		...lists,
		...handlePerformanceConfigRule(performanceConfigRule),

		merchantBrandRefList: dealInterfaceBrand(merchantBrandRefList),
		paramConfigList: coverParamConfigList(paramConfigList),
	};
	return response;
}
function handlePerformanceConfigRule(configRule) {
	if (!configRule) {
		console.warn("后端数据缺少 performanceConfigRule");
		return {};
	}
	const goodsConfigTypes = configRule.goodsConfigTypes.split(",");
	if (goodsConfigTypes && goodsConfigTypes.length) {
		goodsConfigTypes.forEach((item) => {
			item = Number(item);
		});
	}
	return {
		assessmentFactorValue: configRule.assessmentFactors.split(",").map(Number),
		commissionBases: configRule.commissionBases.split(","),
		commissionTypes: configRule.commissionTypes.split(","),
		goodsConfigTypes,
	};
}

const templateType = {
	1: "ParamsType1",
	2: "ParamsType2",
};
const tableEnum = {
	title: "",
	templateType: "ParamsType1",
	goodsCategory: 0,
	ParamsType1Products: [
		// { productLabel: [], productValue: [], optionList: [] },
	],
	ParamsType1: [],
	ParamsType2CommissionTypes: undefined,
	ParamsType2: [],
	priceRangeProps: [],
	assessmentFactorOptions: undefined,
	sort: -1,
};

function getAssessmentFactorOptionsTitle(assessmentFactorValue) {
	const item = assessmentFactorOptions.find(item => item.value === assessmentFactorValue);
	return item;
}

// 转换 表格数据
export function coverParamConfigList(paramConfigList) {
	// 通过assessmentFactor 区分表格数量
	// 通过 rowId = String(startValue) + Staring(endValue) 区分行

	const tableList = groupBy(paramConfigList, "assessmentFactor");
	const _paramConfigList = [];
	if (tableList && Object.keys(tableList).length) {
		for (const tableIndex in tableList) {
			const tableItem = tableList[tableIndex][0];
			const table = tableList[tableIndex];
			// 因为每一个table的数据是一样的,所以直接取0位
			const baseTable = structuredClone(tableEnum);
			const assessmentFactorOptions = getAssessmentFactorOptionsTitle(tableItem.assessmentFactor);
			baseTable.title = assessmentFactorOptions?.label; // 找enum
			baseTable.templateType = templateType[tableList[tableIndex][0].templateType];
			baseTable.goodsCategory = tableList[tableIndex][0].goodsConfigType;
			baseTable.assessmentFactorOptions = structuredClone(assessmentFactorOptions);
			baseTable.sort = assessmentFactorOptions.sort;
			if (baseTable.templateType === "ParamsType2") {
				baseTable.ParamsType2CommissionTypes = tableList[tableIndex][0].commissionType;
			}

			const colTable = groupBy(table, item => String(item.productDefineId) + String(item.goodsType) + String(item.secondaryGoodsType) + String(item.thirdLevelGoodsType));
			// 整理ParamsType1Products
			if (baseTable.templateType === "ParamsType1") {
				console.log("colTable", colTable);
				Object.keys(colTable).forEach((colIndex, colEachIndex) => {
					const firstColItem = colTable[colIndex][0];
					// 整理 所选商品
					const paramsTypeProductsItem = {
						productLabel: [],
						productValue: [],
						optionList: [],
					};
					console.log("firstColItem", firstColItem);
					if ([1, 2].includes(firstColItem.goodsConfigType)) {
						firstColItem.productDefineId && paramsTypeProductsItem.productLabel.push(firstColItem.productDefineId);
					} else if (firstColItem.goodsConfigType === -1) {
						paramsTypeProductsItem.productLabel = [-1];
					} else {
						setBackendGoods(firstColItem, paramsTypeProductsItem);
					}
					// 整理数值
					colTable[colIndex].forEach((item, index) => {
						paramsTypeProductsItem.productValue.push(item?.commissionTypeValue);
					});

					// push 列
					baseTable.ParamsType1Products.push(paramsTypeProductsItem);
				});
			}

			if (baseTable.templateType === "ParamsType1") {
				const rowTable = groupBy(tableList[tableIndex], item => String(item.startValue) + String(item.endValue));
				Object.keys(rowTable).forEach((rowIndex, rowEachIndex) => {
					const rowItem = rowTable[rowIndex][0];
					baseTable.ParamsType1.push({
						priceRange: [rowItem.startValue, rowItem.endValue],
						commissionTypes: rowItem.commissionType,
					});
				});
			}
			if (baseTable.templateType === "ParamsType2") {
				const rowTable = groupBy(tableList[tableIndex], item => String(item.startValue) + String(item.endValue) + String(item.productDefineId) + String(item.goodsType) + String(item.secondaryGoodsType) + String(item.thirdLevelGoodsType));
				Object.keys(rowTable).forEach((rowIndex, rowEachIndex) => {
					const rowItem = rowTable[rowIndex][0];

					console.log("??????");
					const rowItemParamConfig = rowItem.performanceParamConfigConditions;
					const conditionType = rowItemParamConfig.length ? rowItemParamConfig[0].conditionType : 0;
					const conditionKey = `conditionType${conditionType}`;
					const paramsType2Item = {
						// 先还原默认数据
						...structuredClone(paramConfigListTemplate.ParamsType2[0]),
						condition: conditionType,
						priceRange: [rowItem.startValue, rowItem.endValue],
						productLabel: [],
					};
					if (rowItem.goodsConfigType === -1) {
						paramsType2Item.productLabel = [-1];
					} else {
						setBackendAllGoods(rowItem, paramsType2Item);
					}

					if (conditionType === 0) {
						paramsType2Item.conditionType0 = rowItem.commissionTypeValue;
					} else {
						paramsType2Item[conditionKey] = {
							triggerCondition: rowItem.commissionTypeValue,
							productValue: [],
						};
						if (rowItem.goodsConfigType === -1) {
							paramsType2Item[conditionKey].productValue = [-1];
						} else {
							setBackendConditionGoods(rowItemParamConfig[0], paramsType2Item[conditionKey], "productValue");
						}

						// 到这里 conditionType4 已经解决
						if (conditionType === 3) {
							paramsType2Item[conditionKey].increaseReward = rowItemParamConfig[0].conditionCommissionTypeValue;
						}
						if (conditionType === 2) {
							paramsType2Item[conditionKey].deductRate = rowItemParamConfig[0].conditionEndValue;
							paramsType2Item[conditionKey].perOrderDeductAmount = rowItemParamConfig[0].conditionCommissionTypeValue;
						}
						if (conditionType === 1) {
							// 要先排序. 小的在前面
							rowItemParamConfig.sort((a, b) => {
								if (a.id == null)
									return 1;
								if (b.id == null)
									return -1;
								return a.id - b.id;
							});
							console.log("排序", rowItemParamConfig);
							paramsType2Item[conditionKey].permeabilityMin = rowItemParamConfig[0].conditionStartValue;
							paramsType2Item[conditionKey].permeabilityMax = rowItemParamConfig[0].conditionEndValue;
							paramsType2Item[conditionKey].productValue = [];
							paramsType2Item[conditionKey].productValue1 = [];

							if (rowItem.goodsConfigType === -1) {
								paramsType2Item[conditionKey].productValue = [-1];
								paramsType2Item[conditionKey].productValue1 = [-1];
							} else {
								setBackendConditionGoods(rowItemParamConfig[0], paramsType2Item[conditionKey], "productValue");
								setBackendConditionGoods(rowItemParamConfig[1], paramsType2Item[conditionKey], "productValue1");
							}
							paramsType2Item[conditionKey].permeabilityMin1 = rowItemParamConfig[1].conditionStartValue === null ? undefined : rowItemParamConfig[1].conditionStartValue;
							paramsType2Item[conditionKey].permeabilityMax1 = rowItemParamConfig[1].conditionEndValue === null ? undefined : rowItemParamConfig[1].conditionEndValue;
						}
					}

					// paramsType2Item = sortTable(paramsType2Item);
					baseTable.ParamsType2.push(paramsType2Item);
				});
			}

			if (baseTable.goodsCategory === -1) {
				baseTable.goodsCategory = 0;
			}
			if (baseTable.templateType === "ParamsType2") {
				const productTableList = groupBy(baseTable.ParamsType2, item => JSON.stringify(item.productLabel));
				let sortedTable = [];
				Object.keys(productTableList).forEach((productIndex, productTableKey) => {
					sortedTable = [
						...sortedTable,
						...sortTable(productTableList[productIndex]),
					];
				});
				baseTable.ParamsType2 = sortedTable;
			}
			if (baseTable.templateType === "ParamsType1") {
				baseTable.ParamsType1 = sortTable(baseTable.ParamsType1);
			}

			// baseTable[baseTable.templateType] = sortTable(baseTable, baseTable.templateType);
			// console.log("sortTable(baseTable, baseTable.templateType)", baseTable);
			_paramConfigList.push(baseTable);
		}
	}
	console.log("即将返回出去的 _paramConfigList", _paramConfigList);

	return _paramConfigList;
}
function sortTable(range) {
	// // 先根据 rangePath 排序
	// table.sort((a, b) => {
	// 	const aPrice = a[rangePath];
	// 	const bPrice = b[rangePath];
	// 	return aPrice - bPrice;

	return range.sort((a, b) => {
		// 确保 priceRange 存在且有值,否则默认给个 0
		const priceA = (a.priceRange && a.priceRange[0]) || 0;
		const priceB = (b.priceRange && b.priceRange[0]) || 0;
		return priceA - priceB;
	});
}

// 转换 门店数据
function dealInterfaceBrand(brandList) {
	const map = new Map();
	(brandList || []).forEach((item) => {
		const key = item.merchantId;
		if (!map.has(key)) {
			map.set(key, {
				merchantId: item.merchantId,
				merchantName: item.merchantName,
				checkBrands: [],
			});
		}
		const group = map.get(key);
		group.checkBrands.push({
			brandId: item.brandId,
			brandName: item.brandName,
			childId: item.childId,
			childName: item.childName,
		});
	});
	return Array.from(map.values());
}