rules.js 14.5 KB
import { groupBy, isEqual } from "lodash-es";
import store from "@/store";

/**
 * 校验参数配置列表
 * @param {object} formData - 表单数据
 */
export function validateParamConfigList(formData) {
	if (!formData.paramConfigList || formData.paramConfigList.length === 0) {
		return "请至少配置一个绩效参数";
	}

	for (let i = 0; i < formData.paramConfigList.length; i++) {
		const paramConfig = formData.paramConfigList[i];

		// 校验模板类型
		if (!paramConfig.templateType) {
			return `第${i + 1}个参数配置:请选择模板类型`;
		}

		// 校验ParamsType1类型
		if (paramConfig.templateType === "ParamsType1") {
			if (!paramConfig.ParamsType1 || paramConfig.ParamsType1.length === 0) {
				return `第${i + 1}个参数配置:请至少添加一行数据`;
			}

			// 校验每一行
			for (let rowIndex = 0; rowIndex < paramConfig.ParamsType1.length; rowIndex++) {
				const row = paramConfig.ParamsType1[rowIndex];
				const priceRange = row.priceRange || [];
				const startValue = priceRange[0];
				const endValue = priceRange[1];

				// 校验价格范围
				if (startValue === undefined || startValue === null || startValue === "") {
					return `第${i + 1}个参数配置:第${rowIndex + 1}行的起始值不能为空`;
				}
				if (endValue === undefined || endValue === null || endValue === "") {
					return `第${i + 1}个参数配置:第${rowIndex + 1}行的结束值不能为空`;
				}
				if (Number(endValue) <= Number(startValue)) {
					return `第${i + 1}个参数配置:第${rowIndex + 1}行的结束值必须大于起始值`;
				}

				// 校验提成方式
				if (!row.commissionTypes) {
					return `第${i + 1}个参数配置:第${rowIndex + 1}行请选择提成方式`;
				}
			}

			// 校验商品列
			if (!paramConfig.ParamsType1Products || paramConfig.ParamsType1Products.length === 0) {
				return `第${i + 1}个参数配置:请至少添加一列商品`;
			}

			// 校验每一列的商品选择
			for (let colIndex = 0; colIndex < paramConfig.ParamsType1Products.length; colIndex++) {
				const productCol = paramConfig.ParamsType1Products[colIndex];
				if (!productCol.productLabel || productCol.productLabel.length === 0) {
					return `第${i + 1}个参数配置:第${colIndex + 1}列请选择商品`;
				}

				// 校验每一列的提成值
				if (!productCol.productValue || productCol.productValue.length === 0) {
					return `第${i + 1}个参数配置:第${colIndex + 1}列请填写提成值`;
				}

				for (let valueIndex = 0; valueIndex < productCol.productValue.length; valueIndex++) {
					const value = productCol.productValue[valueIndex];
					if (value === undefined || value === null || value === "") {
						return `第${i + 1}个参数配置:第${colIndex + 1}列第${valueIndex + 1}行的提成值不能为空`;
					}
				}
			}
		}

		// 校验ParamsType2类型
		if (paramConfig.templateType === "ParamsType2") {
			if (!paramConfig.ParamsType2 || paramConfig.ParamsType2.length === 0) {
				return `第${i + 1}个参数配置:请至少添加一行数据`;
			}

			// 校验提成方式
			if (!paramConfig.ParamsType2CommissionTypes) {
				return `第${i + 1}个参数配置:请选择提成方式`;
			}

			// 校验每一行
			for (let rowIndex = 0; rowIndex < paramConfig.ParamsType2.length; rowIndex++) {
				const row = paramConfig.ParamsType2[rowIndex];
				const priceRange = row.priceRange || [];
				const startValue = priceRange[0];
				const endValue = priceRange[1];

				// 校验价格范围
				if (startValue === undefined || startValue === null || startValue === "") {
					return `第${i + 1}个参数配置:第${rowIndex + 1}行的起始值不能为空`;
				}
				if (endValue === undefined || endValue === null || endValue === "") {
					return `第${i + 1}个参数配置:第${rowIndex + 1}行的结束值不能为空`;
				}
				if (Number(endValue) <= Number(startValue)) {
					return `第${i + 1}个参数配置:第${rowIndex + 1}行的结束值必须大于起始值`;
				}

				// 校验条件类型
				const condition = row.condition;
				if (condition === undefined || condition === null) {
					return `第${i + 1}个参数配置:第${rowIndex + 1}行请选择条件类型`;
				}

				// 根据条件类型校验对应字段
				if (condition === 0) {
					// 无条件
					if (row.conditionType0 === undefined || row.conditionType0 === null || row.conditionType0 === "") {
						return `第${i + 1}个参数配置:第${rowIndex + 1}行请填写触发条件`;
					}
				} else if (condition === 1) {
					// 渗透率条件 - 特殊处理:先检查四个渗透率字段是否有值
					const conditionType1 = row.conditionType1;

					// 检查四个渗透率字段是否有值(即使conditionType1不存在,也允许通过)
					let hasPermeabilityValue = false;
					if (conditionType1) {
						hasPermeabilityValue
							= (conditionType1.permeabilityMax !== undefined && conditionType1.permeabilityMax !== null && conditionType1.permeabilityMax !== "")
								|| (conditionType1.permeabilityMax1 !== undefined && conditionType1.permeabilityMax1 !== null && conditionType1.permeabilityMax1 !== "")
								|| (conditionType1.permeabilityMin !== undefined && conditionType1.permeabilityMin !== null && conditionType1.permeabilityMin !== "")
								|| (conditionType1.permeabilityMin1 !== undefined && conditionType1.permeabilityMin1 !== null && conditionType1.permeabilityMin1 !== "");
					}

					// 如果四个渗透率字段任意一个有值,则需要校验相关字段
					if (hasPermeabilityValue) {
						// 此时必须存在conditionType1对象
						if (!conditionType1) {
							return `第${i + 1}个参数配置:第${rowIndex + 1}行条件数据不完整`;
						}

						// 校验触发条件
						if (conditionType1.triggerCondition === undefined || conditionType1.triggerCondition === null || conditionType1.triggerCondition === "") {
							return `第${i + 1}个参数配置:第${rowIndex + 1}行请填写触发条件`;
						}

						// 校验商品相关字段
						if (!conditionType1.productValue || conditionType1.productValue.length === 0) {
							return `第${i + 1}个参数配置:第${rowIndex + 1}行请选择商品`;
						}
						// 校验第二个商品相关字段
						if ((conditionType1.productValue1 && conditionType1.productValue1.length > 0) || conditionType1.permeabilityMin1 || conditionType1.permeabilityMax1) {
							if (!(conditionType1.productValue1 && conditionType1.productValue1.length > 0) || !conditionType1.permeabilityMin1 || !conditionType1.permeabilityMax1) {
								return `第${i + 1}个参数配置:第${rowIndex + 1}行请选择第二个商品`;
							}
						}
					}
					// 如果四个渗透率字段全部没有值,则conditionType1对象不做任何校验,直接通过
				} else {
					// 其他条件类型(2、3、4)
					const conditionData = row[`conditionType${condition}`];
					if (!conditionData) {
						return `第${i + 1}个参数配置:第${rowIndex + 1}行条件数据不完整`;
					}

					if (condition === 2) {
						// 扣减条件
						// 校验触发条件
						if (conditionData.triggerCondition === undefined || conditionData.triggerCondition === null || conditionData.triggerCondition === "") {
							return `第${i + 1}个参数配置:第${rowIndex + 1}行请填写触发条件`;
						}
						if (conditionData.deductRate === undefined || conditionData.deductRate === null || conditionData.deductRate === "") {
							return `第${i + 1}个参数配置:第${rowIndex + 1}行请填写扣减比例`;
						}
						if (conditionData.perOrderDeductAmount === undefined || conditionData.perOrderDeductAmount === null || conditionData.perOrderDeductAmount === "") {
							return `第${i + 1}个参数配置:第${rowIndex + 1}行请填写每单扣减金额`;
						}
					} else if (condition === 3) {
						// 奖励条件
						// 校验触发条件
						if (conditionData.triggerCondition === undefined || conditionData.triggerCondition === null || conditionData.triggerCondition === "") {
							return `第${i + 1}个参数配置:第${rowIndex + 1}行请填写触发条件`;
						}
						if (conditionData.increaseReward === undefined || conditionData.increaseReward === null || conditionData.increaseReward === "") {
							return `第${i + 1}个参数配置:第${rowIndex + 1}行请填写奖励值`;
						}
					} else if (condition === 4) {
						// 商品条件
						// 校验触发条件
						if (conditionData.triggerCondition === undefined || conditionData.triggerCondition === null || conditionData.triggerCondition === "") {
							return `第${i + 1}个参数配置:第${rowIndex + 1}行请填写触发条件`;
						}
						if (!conditionData.productValue || conditionData.productValue.length === 0) {
							return `第${i + 1}个参数配置:第${rowIndex + 1}行请选择商品`;
						}
					}
				}
			}
		}
	}

	return null;
}

export function validPriceRange(rule, value, cb) {
	if (value?.[0] >= value?.[1]) {
		return cb(new Error("最小值不能大于最大值"));
	}
	// validateStrictIntervals({
	// 	intervals,
	// 	tolerance = 0,
	// 	maxLimit = 100,
	// 	mustReachMax = true,

	const { templateType, tableIndex, rowIndex, field } = rule;

	const formData = store.state.PerformanceFormStore.formData;
	const table = formData.paramConfigList[tableIndex];

	const priceList = table[templateType].map(item => item.priceRange);

	const error = validateStrictIntervals({
		intervals: priceList,
		tolerance: 0.01,
		mustReachMax: false,
	});
	if (error) {
		return cb(new Error(error));
	}
	cb();
}

export function validPriceRangeType2(rule, value, cb) {
	if (value?.[0] >= value?.[1]) {
		return cb(new Error("最小值不能大于最大值"));
	}
	// validateStrictIntervals({
	// 	intervals,
	// 	tolerance = 0,
	// 	maxLimit = 100,
	// 	mustReachMax = true,

	const { templateType, tableIndex, rowIndex, field } = rule;
	if (templateType === "ParamsType2") {
		const table = store.state.PerformanceFormStore.formData.paramConfigList[tableIndex];
		// 当为系数时,模板2 只能有一组商品,所以可以直接校验
		if (table.ParamsType2CommissionTypes === "XS") {
			const priceList = table[templateType].map(item => item.priceRange);
			const error = validateStrictIntervals({
				intervals: priceList,
				tolerance: 0.01,
				mustReachMax: true,
			});
			if (error) {
				return cb(new Error(error));
			}
		}
	}

	// const formData = store.state.PerformanceFormStore.formData;
	// const table = formData.paramConfigList[tableIndex];
	// console.log(" table[templateType]", table[templateType]);

	// // const priceList = table[templateType].map(item => item.priceRange);
	// const d = groupBy(table[templateType], item => JSON.stringify(item.productLabel));

	// console.log(d);
	// const error = validateStrictIntervals({
	// 	intervals: priceList,
	// 	tolerance: 0.01,
	// 	mustReachMax: true,
	// });
	// if (error) {
	// 	return cb(new Error(error));
	// }
	cb();
}

export function validGoodsCategory(rule, value, callback) {
	const { templateType, tableIndex, rowIndex, field } = rule;

	const formData = store.state.PerformanceFormStore.formData;

	const table = formData.paramConfigList[tableIndex];
	// 	{
	//     "tableIndex": 0,
	//     "rowIndex": 0,
	//     "templateType": "ParamsType1Products",
	//     "field": "paramConfigList.0.ParamsType1Products.0.productLabel",
	//     "fullField": "paramConfigList.0.ParamsType1Products.0.productLabel",
	//     "type": "string"
	// }
	if (templateType === "ParamsType1Products") {
		//
		const colList = table.ParamsType1Products;
		for (const [colIndex, col] of colList.entries()) {
			// 这里的rowIndex就是colIndex, 为了模板2才如此取名
			if (colIndex !== rowIndex) {
				if (isEqual(col.productLabel, value)) {
					return callback(new Error("同一个考核因子内商品不能重复"));
				}
			}
		}
	}
	callback();
}
/**
 * @param {Array[]} intervals - 二维数组数据
 * @param {number} tolerance - 必须满足的精确间隙
 * @param {number} maxLimit - 期望的终点上限
 * @param {boolean} mustReachMax - 是否强制要求终点达到 maxLimit
 */
function validateStrictIntervals({
	intervals,
	tolerance = 0,
	maxLimit = 100,
	mustReachMax = true,
}) {
	const EPSILON = 1e-10;

	if (!intervals || intervals.length === 0)
		return "数据不能为空";

	// 1. 排序(不改变原数组)
	const sorted = [...intervals].sort((a, b) => a[0] - b[0]);

	// 2. 边界检查:起点必须为 0
	if (Math.abs(sorted[0][0] - 0) > EPSILON) {
		return "起点必须从 0 开始";
	}

	// 3. 终点检查:仅在 mustReachMax 为 true 时执行
	const actualLastEnd = sorted[sorted.length - 1][1];
	if (mustReachMax && Math.abs(actualLastEnd - maxLimit) > EPSILON) {
		return `终点必须达到 ${maxLimit}`;
	}

	// 4. 动态上限检查:即使不强制达到 maxLimit,也不能超过它
	if (mustReachMax && actualLastEnd > maxLimit + EPSILON) {
		return `终点不能超过上限 ${maxLimit}`;
	}

	for (let i = 0; i < sorted.length; i++) {
		const [start, end] = sorted[i];

		// 5. 基础校验:单区间合法性
		if (end <= start) {
			return `无效区间: [${start}, ${end}],结束值必须大于开始值`;
		}

		// 6. 相邻区间检查(交集与连续性)
		if (i < sorted.length - 1) {
			const nextStart = sorted[i + 1][0];
			const expectedNextStart = end + tolerance;
			const diff = nextStart - expectedNextStart;

			// 只要 diff < 0,说明提前开始,有交集
			if (diff < -EPSILON) {
				return `区间存在交集或重复: [..., ${end}] 与 [${nextStart}, ...]`;
			}

			// 只要 diff > 0,说明开始晚了,有空隙
			if (diff > EPSILON) {
				return `区间不连续: 预期起点应为 ${expectedNextStart},实际为 ${nextStart}`;
			}
		}
	}

	return ""; // 验证通过
}
/**
 * 获取表单校验规则
 */
export function getFormRules(formData, merchantBrandRefList) {
	return {
		schemeName: [
			{ required: true, trigger: "blur", message: "方案名称不能为空" },
			{ min: 3, max: 30, message: "方案名称长度在3-30个字符之间", trigger: "blur" },
		],
		assessmentFactorValue: [
			{ required: true, trigger: "change", message: "请选择考核因子" },
		],
		commissionBases: [
			{ required: true, trigger: "change", message: "请选择提成基数" },
		],
		commissionTypes: [
			{ required: true, trigger: "change", message: "请选择提成方式" },
		],
		goodsConfigTypes: [
			{ required: true, trigger: "change", message: "请选择商品分类" },
		],
		businessType: [
			{ required: true, trigger: "change", message: "请选择适配业务" },
		],
		effectiveMonth: [
			{ required: true, trigger: "change", message: "请选择生效月份" },
		],
	};
}