addConfig.vue
13.6 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<template>
<d2-dialog class="addConfigDialog"
:is-visible="visible"
title="新增预警基线管理"
width="800px"
@closeDialog="handleClose"
@confirmDialog="handleAddConfirm">
<div class="reject-dialog">
<glsx-combi-form ref="formRef"
class="edit-form-container"
default-width="500px"
:form-data="editFormData"
:form-obj="addForm"
:form-ref="configRef"
:form-rules="formRules"
:is-block="true"
label-width="150px">
<template #ruleHtml>
<el-table border
class="warning-table"
:data="addForm.configList"
:header-cell-style="headerCellStyle">
<el-table-column align="center"
label="预警亮灯"
width="245">
<template #default="scope">
<span class="warning-level">
{{ getLevelText(scope.row.warningLevel) }}
<span class="level" :class="[getLevelClass(scope.row.warningLevel)]"></span>
</span>
</template>
</el-table-column>
<el-table-column align="center"
label="预警规则"
min-width="320">
<template #default="scope">
<div class="warning-rule">
<div class="warning-rule-info">
<el-input v-model="scope.row.minValue"
:disabled="scope.row.warningLevel === 0 ? true : false "
maxlength="10"
style="width: 140px;margin:0 8px;"
@input="handleMinInput(scope.$index, $event)">
</el-input>
<span>-</span>
<el-input v-model="scope.row.maxValue"
:disabled="scope.row.warningLevel === 2 ? true : false "
maxlength="10"
style="width: 140px;margin:0 8px;"
@input="handleMaxInput(scope.$index, $event)">
</el-input>
</div>
</div>
</template>
</el-table-column>
</el-table>
</template>
</glsx-combi-form>
</div>
</d2-dialog>
</template>
<script>
import { createOrUpdateBaselineConfig } from "@api/resident";
import { deepClone, Util } from "@/common/util";
export default {
name: "RejectDialog",
props: {
visible: {
type: Boolean,
default: false,
},
recordInfo: {
type: Object,
default: () => {},
},
},
data() {
return {
addForm: {
updateFlag: 0, // 0是新增,1是更新
indicatorCode: "A036",
indicatorName: "个人产值",
timeDimension: 1,
configList: [],
},
configRef: "configRef",
formRules: {
// indicatorCode: [{ required: true, message: "请选择指标", trigger: "change" }],
// timeDimension: [{ required: true, message: "请选择时间维度", trigger: "change" }],
},
editFormData: [
{
label: "指标",
name: "indicatorCode",
type: "radio",
optionList: [],
isDisabled: false,
optionId: "confId",
optionLabel: "confValue",
optionsName: "indicatorOpt",
handleRadioChange: (val) => {
this.addForm.indicatorName = this.baseData.indicatorOpt.find(item => item.confId === val).confValue;
},
},
{
label: "时间维度",
name: "timeDimension",
type: "radio",
optionList: [],
isDisabled: false,
optionId: "confId",
optionLabel: "confValue",
optionsName: "timeDimensionOpt",
handleRadioChange: (val) => {},
},
{
label: "预警规则",
name: "configList",
addHtml: "ruleHtml",
},
],
baseData: {
indicatorOpt: [
{
confId: "A036",
confValue: "个人产值",
},
{
confId: "A076",
confValue: "个人客单价",
},
{
confId: "A085",
confValue: "转化率",
},
],
timeDimensionOpt: [
{ confId: 1, confValue: "日" },
{ confId: 2, confValue: "周" },
{ confId: 3, confValue: "月" },
],
},
};
},
computed: {
isVisible: {
get() {
return this.visible;
},
set(newVal) {},
},
},
mounted() {
this.initFormatData();
this.getOptionsList(this, "editFormData", false);
},
methods: {
findAttrIdx(formName, attrName) {
return this[formName].findIndex(item => item.name == attrName);
},
// 表头样式
headerCellStyle() {
return {
backgroundColor: "#f8fafd",
color: "#475569",
fontWeight: "600",
fontSize: "14px",
borderBottom: "1px solid #e2e8f0",
};
},
initFormatData() {
console.log(this.recordInfo);
if (Object.keys(this.recordInfo).length) {
this.addForm.indicatorCode = this.recordInfo.indicatorCode;
this.addForm.indicatorName = this.recordInfo.indicatorName;
this.addForm.timeDimension = this.recordInfo.timeDimension;
if (Array.isArray(this.recordInfo.configList) && this.recordInfo.configList.length) {
this.addForm.configList = this.recordInfo.configList.map(item => ({
...item,
maxValue: item.maxValueInf == 1 ? "+∞" : item.maxValue,
}));
}
this.addForm.updateFlag = 1;
// this.editFormData[this.findAttrIdx("editFormData", "indicatorCode")].isDisabled = true;
// this.editFormData[this.findAttrIdx("editFormData", "timeDimension")].isDisabled = true;
} else {
const obj = {
warningLevel: 0,
minValue: "",
maxValue: null,
maxValueInf: 0,
};
for (let i = 0; i < 3; i++) {
obj.warningLevel = i;
obj.minValue = i === 0 ? 0 : "";
obj.maxValue = i === 2 ? "+∞" : "";
obj.maxValueInf = i === 2 ? 1 : 0;
this.addForm.configList.push(deepClone(obj));
}
this.addForm.updateFlag = 0;
}
},
// 预警级别文字
getLevelText(level) {
const map = { 0: "红灯", 1: "黄灯", 2: "绿灯" };
return map[level] || "--";
},
// 预警级别样式类
getLevelClass(level) {
const map = { 0: "level-red", 1: "level-yellow", 2: "level-green" };
return map[level] || "";
},
formatTimeDimension(dim) {
const map = { 1: "日", 2: "周", 3: "月" };
return map[dim] || "--";
},
// 校验配置区间的连续性与覆盖性
validateConfigList() {
const list = (this.addForm.configList || []).slice().sort((a, b) => a.warningLevel - b.warningLevel);
if (!list.length)
return true;
// 必须从 0 开始
const firstMin = this.getMinNumeric(list[0]);
if (firstMin !== 0) {
this.$message.error("区间必须从 0 开始");
return false;
}
for (let i = 0; i < list.length; i++) {
const seg = list[i];
const min = this.getMinNumeric(seg);
const max = this.getMaxNumeric(seg);
// 有效性
if (min === null) {
this.$message.error(`第 ${i + 1} 段起始值无效`);
return false;
}
if (max !== Infinity && max === null) {
this.$message.error(`第 ${i + 1} 段结束值无效`);
return false;
}
if (max !== Infinity && min >= max) {
this.$message.error(`第 ${i + 1} 段必须满足 起始值 < 结束值`);
return false;
}
// 连续性(从第二段开始检查)
if (i > 0) {
const prev = list[i - 1];
const prevMax = this.getMaxNumeric(prev);
// 如果上一段已经是 +∞,则不应再有下一段
if (prevMax === Infinity) {
this.$message.error(`第 ${i} 段已为 +∞,后续不应再有区间`);
return false;
}
if (min !== prevMax) {
this.$message.error(`连续性校验失败:第 ${i + 1} 段起始值应为 ${prevMax}`);
return false;
}
}
}
return true;
},
// 提取起始值(数字)
getMinNumeric(cfg) {
const n = Number(cfg?.minValue);
return Number.isFinite(n) ? n : null;
},
// 提取结束值(数字,+∞ 视为 Infinity)
getMaxNumeric(cfg) {
if (cfg?.maxValueInf === 1 || cfg?.warningLevel === 2 || cfg?.maxValue === "+∞") {
return Infinity;
}
const n = Number(cfg?.maxValue);
return Number.isFinite(n) ? n : null;
},
// 格式化数字,最多两位小数
// 格式化数字,最多两位小数
formatDecimal(value) {
if (value === "" || value === null || value === undefined) {
return "";
}
// 只允许数字和小数点
let str = String(value).replace(/[^\d.]/g, "");
// 保证只有一个小数点
const parts = str.split(".");
if (parts.length > 2) {
str = parts[0] + "." + parts.slice(1).join("");
}
// 处理以小数点开头的情况,补 0
if (str.startsWith(".")) {
str = "0" + str;
}
// 限制两位小数
const finalParts = str.split(".");
if (finalParts.length === 2) {
finalParts[1] = finalParts[1].slice(0, 2);
str = finalParts.join(".");
}
return str;
},
// 将当前行的结束值同步给下一行的起始值(若存在下一行)
handleMaxInput(index, value) {
const list = this.addForm.configList || [];
const nextIndex = index + 1;
// 格式化输入值,限制两位小数
const formattedValue = this.formatDecimal(value);
this.$set(list[index], "maxValue", formattedValue);
if (nextIndex < list.length) {
this.$set(list[nextIndex], "minValue", formattedValue);
}
},
handleMinInput(index, value) {
const list = this.addForm.configList || [];
const row = list[index];
if (!row)
return;
// 格式化输入值,限制两位小数
const formattedValue = this.formatDecimal(value);
this.$set(row, "minValue", formattedValue);
const min = Number(formattedValue);
if (!Number.isFinite(min))
return;
// 结束值为 +∞(或该行为绿灯)不做处理
const isMaxInfinite = row.maxValueInf === 1 || row.warningLevel === 2 || row.maxValue === "+∞";
if (isMaxInfinite)
return;
const max = Number(row.maxValue);
if (Number.isFinite(max) && min > max) {
// 开始值大于结束值,清空结束值
this.$set(row, "maxValue", "");
}
},
handleAddConfirm() {
// 先做自定义区间校验,不通过则拦截提交
if (!this.validateConfigList())
return;
this.$refs.formRef.$refs[this.configRef].validate(async (valid) => {
if (valid) {
const configList = this.addForm.configList.map((item) => {
return {
warningLevel: item.warningLevel,
minValue: item.minValue,
maxValue: item.maxValueInf == 1 ? null : item.maxValue,
maxValueInf: item.maxValueInf,
id: item.id,
};
});
const params = {
indicatorCode: this.addForm.indicatorCode,
indicatorName: this.addForm.indicatorName,
timeDimension: this.addForm.timeDimension,
updateFlag: this.addForm.updateFlag,
configList,
};
createOrUpdateBaselineConfig(params).then((res) => {
if (res) {
this.$emit("handleAddConfirm", false);
this.$message.success("操作成功");
}
});
}
});
},
handleClose() {
this.$emit("handleClose", false);
},
getOptionsList(that, formName, hasAll = true, refreshSigle) {
if (refreshSigle) {
let item = that[formName].find(k => k.name == refreshSigle);
let index = that[formName].findIndex(k => k.name == refreshSigle);
that.$set(that[formName][index], "optionList", deepClone(that.baseData[item.optionsName]));
if (hasAll && that.baseData[item.optionsName].length && that.baseData[item.optionsName][0][that[formName][index].optionLabel] !== "全部") {
const temp = {};
temp[that[formName][index].optionId] = "";
temp[that[formName][index].optionLabel] = "全部";
that[formName][index].optionList.unshift(temp);
}
} else {
that[formName].forEach((item, index) => {
if (that.baseData[item.optionsName]?.length) {
that.$set(that[formName][index], "optionList", deepClone(that.baseData[item.optionsName]));
if (hasAll && that.baseData[item.optionsName].length && that.baseData[item.optionsName][0][that[formName][index].optionLabel] !== "全部") {
const temp = {};
temp[that[formName][index].optionId] = "";
temp[that[formName][index].optionLabel] = "全部";
that[formName][index].optionList.unshift(temp);
}
}
});
}
},
},
};
</script>
<style lang="scss" scoped>
.addConfigDialog {
::v-deep {
.el-dialog__header {
border-bottom: 1px solid #e5e5e5;
}
.el-dialog__footer {
border-top: 1px solid #e5e5e5;
}
}
}
.warning-level {
display: flex;
align-items: center;
min-width: 48px;
font-weight: 500;
padding: 2px 0;
margin-right: 102px;
}
.level {
display: inline-block;
width: 6px;
height: 6px;
border-radius: 6px;
margin-left: 4px;
}
.level-red {
background: #f24724;
}
.level-yellow {
background: #ff9d0a;
}
.level-green {
background: #35c376;
}
.warning-rule-info {
display: flex;
align-items: center;
}
</style>