DataSchemeForm.vue
9.46 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
<template>
<el-dialog :title="!dataForm.id ? '新建方案' : '编辑方案'" :close-on-click-modal="false"
:close-on-press-escape="false" :visible.sync="visible" lock-scroll
class="NCC-dialog NCC-dialog_center" width="630px">
<el-form ref="dataForm" :model="dataForm" :rules="dataRule" v-loading="formLoading">
<el-form-item prop="fullName">
<el-input v-model="dataForm.fullName" placeholder="请输入方案名称" />
</el-form-item>
<el-form-item v-for="(item, index) in condition" :key="index">
<el-row :gutter="5">
<el-col :span="7" class="mb-10">
<el-select v-model="item.logic" placeholder="请选择">
<el-option v-for="item in logicOptions" :key="item.value" :label="item.label"
:value="item.value">
</el-option>
</el-select>
</el-col>
<el-col :span="5" class="mb-10">
<el-button icon="el-icon-plus" @click="addItem(index)">添加条件</el-button>
</el-col>
<el-col :span="12" class="mb-10" style="text-align:right">
<el-button type="danger" icon="el-icon-close" @click="delGroup(index)">删除分组</el-button>
</el-col>
<el-col :span="24" v-for="(subItem, i) in item.groups" :key="index + i" class="mb-10">
<el-row :gutter="5">
<el-col :span="7">
<el-select v-model="subItem.id" placeholder="选择字段" clearable
@change="changeField($event,subItem)">
<el-option v-for="item in fieldOptions" :key="item.id" :label="item.fullName"
:value="item.id">
</el-option>
</el-select>
</el-col>
<el-col :span="7">
<el-select v-model="subItem.op" placeholder="选择符号" clearable>
<el-option v-for="item in subItem.opOptions" :key="item.value" :label="item.label"
:value="item.value">
</el-option>
</el-select>
</el-col>
<el-col :span="8">
<el-input v-model="subItem.value" placeholder="输入字段值"
:readonly="subItem.readonly" />
</el-col>
<el-col :span="2" style="text-align:right">
<el-button type="danger" icon="el-icon-close" @click="delItem(index,i)"
style="width:100%" />
</el-col>
</el-row>
</el-col>
</el-row>
</el-form-item>
<el-form-item>
<el-button @click="addGroup">添加分组</el-button>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="visible = false">{{$t("common.cancelButton")}}</el-button>
<el-button type="primary" :loading="btnLoading" @click="dataFormSubmit()">
{{ $t("common.confirmButton")}}</el-button>
</span>
</el-dialog>
</template>
<script>
import {
createDataScheme,
updateDataScheme,
getDataSchemeInfo,
getDataAuthorizeList,
} from "@/api/system/dataAuthorize"
import { deepClone } from '@/utils'
export default {
data() {
return {
visible: false,
formLoading: false,
btnLoading: false,
logicOptions: [
{
value: "and",
label: "and",
},
{
value: "or",
label: "or",
},
],
fieldOptions: [],
opOptions: [{
value: 'Equal',
label: '等于'
}, {
value: 'NotEqual',
label: '不等于'
}, {
value: 'GreaterThan',
label: '大于'
}, {
value: 'GreaterThanOrEqual',
label: '大于等于'
}, {
value: 'LessThan',
label: '小于'
}, {
value: 'LessThanOrEqual',
label: '小于等于'
}],
dataForm: {
id: "",
moduleId: "",
fullName: "",
conditionJson: "",
conditionText: ""
},
condition: [],
dataRule: {
fullName: [
{ required: true, message: "方案名称不能为空", trigger: "blur" }
]
}
}
},
methods: {
init(moduleId, id) {
this.dataForm.id = id || ""
this.dataForm.moduleId = moduleId
this.dataForm.fullName = ""
this.dataForm.conditionJson = ""
this.dataForm.conditionText = ""
this.condition = [{
logic: "and",
groups: [{
id: "",
field: "",
type: "",
op: "",
value: "",
opOptions: []
}]
}]
this.visible = true
this.$nextTick(() => {
this.formLoading = true
getDataAuthorizeList(this.dataForm.moduleId).then(res => {
this.fieldOptions = res.data.list
if (this.dataForm.id) {
getDataSchemeInfo(this.dataForm.id).then(res => {
this.dataForm = res.data
if (res.data.conditionJson) this.condition = JSON.parse(res.data.conditionJson)
for (let i = 0; i < this.condition.length; i++) {
let groups = this.condition[i].groups
for (let j = 0; j < groups.length; j++) {
let e = groups[j]
let item = this.fieldOptions.filter(o => o.id === groups[j].id)[0]
e.opOptions = this.getOptions(item)
if (item.conditionText !== 'text') {
e.readonly = true
} else {
e.readonly = false
}
}
}
this.formLoading = false
})
} else {
this.formLoading = false
}
})
})
},
changeField(val, item) {
if (!val) {
item.id = ''
item.field = ''
item.type = ''
item.op = ''
item.value = ''
item.opOptions = []
item.readonly = false
} else {
item.op = ''
item.value = ''
item.readonly = false
let fieldItem = this.fieldOptions.filter(o => o.id === val)[0]
item.type = fieldItem.type
item.field = fieldItem.enCode
item.opOptions = this.getOptions(fieldItem)
item.field = fieldItem.enCode
if (fieldItem.conditionText !== 'text') {
item.readonly = true
item.value = fieldItem.conditionText
}
}
},
getOptions(fieldItem) {
let opOptions = []
let options = fieldItem.conditionSymbol ? fieldItem.conditionSymbol.split(',') : []
outer: for (let i = 0; i < options.length; i++) {
inner: for (let j = 0; j < this.opOptions.length; j++) {
if (options[i] === this.opOptions[j].value) {
opOptions.push(this.opOptions[j])
break inner
}
}
}
return opOptions
},
addItem(index) {
this.condition[index].groups.push({
id: "",
field: "",
type: "",
op: "",
value: "",
opOptions: []
})
},
delItem(index, childIndex) {
this.condition[index].groups.splice(childIndex, 1)
},
delGroup(index) {
this.condition.splice(index, 1)
},
addGroup() {
this.condition.push({
logic: "and",
groups: [{
id: "",
field: "",
type: "",
op: "",
value: "",
opOptions: []
}]
})
},
getOpText(val) {
if (!val) return val
let list = this.opOptions.filter(o => o.value == val)
if (!list.length) return val
return list[0].label || val
},
getFieldText(val) {
if (!val) return val
let list = this.fieldOptions.filter(o => o.id == val)
if (!list.length) return val
return list[0].fullName || val
},
dataFormSubmit() {
this.$refs["dataForm"].validate((valid) => {
if (valid) {
this.btnLoading = true
let conditionText = "",
conditionValid = true,
condition = deepClone(this.condition)
outer: for (let i = 0; i < condition.length; i++) {
let e = condition[i]
let text = ''
if (i > 0) text += e.logic === "and" ? "而且" : "或者"
text += '【'
for (let j = 0; j < e.groups.length; j++) {
let ee = e.groups[j]
if (!ee.field || !ee.id || !ee.op || !ee.value) {
this.$message.warning("过滤条件检查出格式错误")
conditionValid = false
break outer
}
delete ee.readonly
delete ee.opOptions
text += `${j == 0 ? '' : ' and '}{${this.getFieldText(ee.id)}} {${this.getOpText(ee.op)}} {${ee.value}}`
}
text += '】'
conditionText += text
}
if (!conditionValid) return this.btnLoading = false
this.dataForm.conditionText = conditionText
this.dataForm.conditionJson = JSON.stringify(condition)
const formMethod = this.dataForm.id ? updateDataScheme : createDataScheme
formMethod(this.dataForm).then(res => {
this.$message({
message: res.msg,
type: "success",
duration: 1500,
onClose: () => {
this.visible = false
this.btnLoading = false
this.$emit("refreshDataList")
},
});
}).catch(() => {
this.btnLoading = false
})
}
})
}
}
}
</script>