rules.js
14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
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: "请选择生效月份" },
],
};
}