ludc
2024-10-17 d02571d59633367ac76b7f58ab38584698b1aa1b
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
<!-- 高级查询对话框组件
    1、打开与关闭与el-dialog用法一致;
    2、用户编辑好的查询条件通过@echoContion事件绑定相应的函数,实现子组件值传递回父组件;
    3、用户输入的值会以conditionMap[field]的格式拼接好进行回传,然后就需要使用的地方自行进行查询的调用;
 -->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="isShowDialog"
    append-to-body
    class="avue-dialog avue-dialog--top"
    destroy-on-close
    lock-scroll
    style="height: 100vh;overflow: hidden"
    title="高级查询"
    top="-3%"
    width="46vw"
    @close="recoverPage">
    <div class="search-total">
      <!-- 头部按钮区域 -->
      <div slot="title" class="dialog-search-button">
        <el-button
          icon="el-icon-search"
          size="small"
          type="primary"
          @click="searchSubmit">
          查询
        </el-button>
        <el-button
          icon="el-icon-refresh"
          size="small"
          type="warning"
          @click="resetInput">
          重置
        </el-button>
      </div>
      <!-- 页面主体内容区域 -->
      <div class="search-content">
        <el-row
          v-for="(item,index) in initOptions"
          :key="item.queryField"
          :span="24">
          <el-col :span="6">
            <div class="grid-content">
              <el-select v-model="searchFormArrays[index].queryField" disabled placeholder="请选择">
                <el-option
                  v-for="feildName in options"
                  :key="feildName.queryField"
                  :label="feildName.title"
                  :value="feildName.queryField">
                </el-option>
              </el-select>
            </div>
          </el-col>
          <el-col :span="4">
            <div class="grid-content">
              <el-select v-model="searchFormArrays[index].condition" placeholder="请选择">
                <el-option
                  v-for="condition in item.conditions"
                  :key="condition.value"
                  :label="condition.label"
                  :value="condition.value">
                </el-option>
              </el-select>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content">
              <el-input v-if="item.fieldType==='text' || item.fieldType===''"
                        v-model="searchFormArrays[index].fieldValue" placeholder="请输入" type="text"></el-input>
              <el-select v-else-if="item.fieldType==='combox'" v-model="searchFormArrays[index].fieldValue"
                         placeholder="请选择">
                <el-option
                  v-for="option in item.data"
                  :key="option.itemValue || option.value"
                  :label="option.itemName || option.label"
                  :value="option.itemValue || option.value">
                </el-option>
              </el-select>
              <el-switch v-else-if="item.fieldType==='truefalse'"
                         v-model="searchFormArrays[index].fieldValue"></el-switch>
              <el-date-picker v-else-if="item.fieldType==='datetime'"
                              v-model="searchFormArrays[index].fieldValue"
                              placeholder="选择日期"
                              type="date">
              </el-date-picker>
              <vci-web-refer
                v-else-if="item.fieldType==='refer'"
                :display="!item.hidden"
                :referConfig="item.referConfigData || {}"
                :text="item.showField"
                :value="searchFormArrays[index].fieldValue"
                @setReferValue="val=>setReferValue(val,index)">
              </vci-web-refer>
            </div>
          </el-col>
          <el-col :span="1">
            <div class="grid-content">
              <i class="el-icon-close" @click="removeInput(index)"></i>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
  </el-dialog>
</template>
 
<script>
import {getDictionary} from "@/api/omd/enum";
import moment from 'moment';
import vciWebRefer from '../refer/vciWebRefer.vue';
 
export default {
  components: {vciWebRefer},
  name: "advancedQuery",
  props: {
    // 对话框显示隐藏控制
    visible: {
      type: "Boolean",
      default: false,
    },
    // 页面显示配置
    options: {
      type: "Object",
      default: {},
    },
  },
  data() {
    return {
      // 对话框显示控制
      isShowDialog: this.visible,
      initOptions: [],
      // 字段类型是输入框类型的条件数组
      searchConditions: [
        {
          value: "_equal",
          label: "等于",
        }, {
          value: "_notequal",
          label: "不等于",
        }, {
          value: "_like",
          label: "包含",
        }, {
          value: "_notlike",
          label: "不包含",
        },{
          value: "_in",
          label: "范围(,间隔)",
        }
      ],
      // 字段类型是单选或下拉框类型的条件数组
      switchSearchConditions: [
        {
          value: "_equal",
          label: "等于",
        }, {
          value: "_notequal",
          label: "不等于",
        }
      ],
      // 参照类型的条件数组
      referSearchConditions: [
        {
          value: "_equal",
          label: "等于",
        }, {
          value: "_notequal",
          label: "不等于",
        }, {
          value: "_like",
          label: "包含",
        }, {
          value: "_notlike",
          label: "不包含",
        }
      ],
      // 字段类型是日期
      dateConditions: [
        {
          value: "_equal",
          label: "等于",
        }, {
          value: "_notequal",
          label: "不等于",
        }, {
          value: "_ge", //大于,默认为大于等于
          label: "大于",
        }, {
          value: "_le", //小于,默认为小于等于
          label: "小于",
        },
      ],
      searchFormArrays: [],
      fieldValue: '',
    }
  },
  watch: {
    // 监听父组件传的窗口显示隐藏的值
    visible() {
      this.isShowDialog = this.visible;
    },
    // 对话框内容渲染配置
    options() {
      this.initData();
    }
  },
  created() {
    this.resetInput()
  },
  methods: {
 
    initData() {
      // 将options配置赋值到data中的option中,避免深浅拷贝的问题所以需要转json之后再赋值
      const data = JSON.stringify(this.options);
      this.initOptions = JSON.parse(data);
      //console.log(this.initOptions);
      if (this.initOptions.length > 0) {
        let array = [];
        this.initOptions.forEach((item, index) => {
          if (item.fieldType === 'combox') {
            let enumCach = item.data || JSON.parse(localStorage.getItem(item.comboxKey));
            if (enumCach != null && enumCach.length > 0) {
              item.data = enumCach;
            } else {
              getDictionary({code: item.comboxKey}).then(res => {
                item.data = res.data.data;
                localStorage.setItem(item.comboxKey, JSON.stringify(res.data.data));
              })
            }
          } else if (item.fieldType === 'refer') {
            this.$set(item, "referConfigData", {
              title: item.title,
              showField: item.showField || item.field,
              field: item.field,
              placeholder: item.inputTip || '',
              options: Object.assign(item.referConfig, {width: "80%"}),
            })
            //console.log(item.referConfigData.options);
            //console.log(item);
          }
          let conditions = [];
          if (item.fieldType === 'text') {
            conditions = this.searchConditions;
          } else if (item.fieldType === 'combox' || item.fieldType === 'truefalse') {
            conditions = this.switchSearchConditions;
          } else if (item.fieldType === 'datetime' || item.fieldType === 'date') {
            conditions = this.dateConditions;
          } else {
            conditions = this.referSearchConditions;
          }
          // console.log(conditions);
          this.$set(item, "conditions", conditions)
 
          let add = {
            queryField: String(item.queryField),
            condition: item.fieldType === 'text' ? String("_like") : String("_equal"),
            fieldValue: item.fieldType === 'truefalse' ? Boolean(false) : String(''),
          }
          array.push(add)
        });
        this.searchFormArrays = array;
      }
      //console.log(this.initOptions);
      //console.log(this.searchFormArrays);
    },
    /** 为参照类型时值选择之后的处理 */
    setReferValue(data, index) {
      if (data.field) {
        this.searchFormArrays[index][data.fieldValue] = data.value || '';
        this.initOptions[index][data.showField] = data.text || '';
      }
    },
 
    // 属性切换时查询条件和查询值也需要对输入框进行切换
    // changeField(index) {
    //     //console.log(option,this.searchFormArrays[index],index);
    //     // 找到数组中对应的要切换为的那个对象
    //     let changeItem = this.options.filter((item)=>{
    //         return item.queryField == this.searchFormArrays[index].queryField;
    //     })[0]
    //     // 如果是combox类型的还需要对枚举类型进行请求
    //     if(changeItem.fieldType==='combox' && changeItem.comboxKey != '') {
    //         changeItem.data = JSON.parse(localStorage.getItem(changeItem.comboxKey));
    //     }
    //     //console.log(changeItem.fieldType);
    //     // 将当前切换的配置项赋值到option的对应的那个对象进行覆盖
    //     this.initOptions[index] = changeItem;
    //     // 覆盖v-model的对象
    //     this.searchFormArrays[index] = {
    //         queryField: String(changeItem.queryField),
    //         condition: changeItem.fieldType==='text' ? String("_like"):String("_equal"),
    //         fieldValue: changeItem.fieldType==='truefalse' ? Boolean(false):String(""),
    //     };
    //     //console.log(this.initOptions);
    //     //console.log(this.searchFormArrays);
    // },
 
    // 移除搜索框
 
    removeInput(index) {
      //console.log(this.initOptions);
      this.$delete(this.initOptions, index);
      this.$delete(this.searchFormArrays, index);
    },
    // 重置当前界面的输入框
    resetInput() {
      this.initData();
    },
    // 恢复页面
    recoverPage() {
      this.resetInput();
      this.$emit('update:visible', false);
    },
    // 提交当前页面的输入的查询条件并做对应的过滤与检查
    searchSubmit() {
      let condtionParam = {};
      const searchConditions = this.searchFormArrays;
      for (let index = 0; index < searchConditions.length; index++) {
        //console.log(condtionParam['conditionMap['+searchConditions[index].queryField+']']+'' == 'undefined');
        if (searchConditions[index].fieldValue + '' != '' || searchConditions[index].fieldValue + '' === 'false') {
          // 存在相同的查询条件
          if (condtionParam['conditionMap[' + searchConditions[index].queryField + ']'] + '' != 'undefined') {
            this.$message.warning("存在重复查询条件,请仔细核对!");
            //console.log(condtionParam['conditionMap['+searchConditions[index].queryField+']']);
            return false;
          }
          let fieldType = this.initOptions[index].fieldType;
          // 当出现查询日期的格式时,需要对日期格式进行处理
          if (fieldType === 'datetime' || fieldType === 'date') {
            // 将时间转换为本地时间
            let localTime = moment.utc(searchConditions[index].fieldValue).local();
            // 格式化时间为您想要的格式
            let formattedTime = localTime.format('YYYY-MM-DD HH:mm:ss');
            condtionParam['conditionMap[' + searchConditions[index].queryField + searchConditions[index].condition + ']'] = formattedTime;
            //console.log(formattedTime);
          } else {
            //拼接成map对象,将查询对象和condition拼接在一起,组成高级查询map的key
            condtionParam['conditionMap[' + searchConditions[index].queryField + searchConditions[index].condition + ']'] = searchConditions[index].fieldValue;
          }
        }
      }
      // 查询条件没有出现重复属性,并且过滤掉了空值,就传递给父组件
      //console.log(condtionParam);
      this.$emit('echoContion', condtionParam)
      this.isShowDialog = false;
    },
  }
}
</script>
 
<style lang="scss" scoped>
.search-total {
  border-radius: 4px;
  margin-top: -10px;
}
 
.dialog-search-button {
  margin-bottom: 15px;
}
 
// .search-total > .search-content > .el-row{
//     margin-bottom: 5px;
//     &:last-child {
//         margin-bottom: 0;
//     }
// }
.search-total > .search-content {
}
 
.search-total > .search-content > .el-row > .el-col {
  border-radius: 4px;
}
 
.search-total > .search-content > .el-row > .el-col > .grid-content {
  border-radius: 4px;
  min-height: 36px;
}
 
.search-total > .search-content > .el-row > .el-col {
  margin-right: 6px;
 
  &:last-child {
    margin-right: 0;
  }
}
 
.grid-content > .el-icon-close {
  font-size: 30px;
  line-height: 40px;
  cursor: pointer;
  color: rgb(222, 130, 105);
}
 
.grid-content > .el-icon-close:hover {
  font-size: 30px;
  color: rgb(219, 52, 6);
}
 
.grid-content > .el-select {
  width: 100%;
}
 
.grid-content > .el-switch {
  line-height: 40px;
  height: 40px;
}
 
.grid-content > .el-date-editor.el-input, .el-date-editor.el-input__inner {
  width: 100%;
}
 
</style>