Blame view

ant-design-vue-jeecg/src/components/jeecg/JSuperQuery.vue 22.5 KB
1
<template>
2
3
<div class="j-super-query-box">
4
5
6
7
8
9
10
11
12
13
14
15
16
  <slot name="button" :isActive="superQueryFlag" :isMobile="izMobile" :open="handleOpen" :reset="handleReset">
    <a-tooltip v-if="superQueryFlag" v-bind="tooltipProps" :mouseLeaveDelay="0.2">
      <!-- begin 不知道为什么不加上这段代码就无法生效 -->
      <span v-show="false">{{tooltipProps}}</span>
      <!-- end 不知道为什么不加上这段代码就无法生效 -->
      <template slot="title">
        <span>已有高级查询条件生效</span>
        <a-divider type="vertical"/>
        <a @click="handleReset">清空</a>
      </template>
      <a-button-group>
        <a-button type="primary" @click="handleOpen">
          <a-icon type="appstore" theme="twoTone" spin/>
17
18
          <span>高级查询</span>
        </a-button>
19
20
21
22
23
        <a-button v-if="izMobile" type="primary" icon="delete" @click="handleReset"/>
      </a-button-group>
    </a-tooltip>
    <a-button v-else type="primary" icon="filter" @click="handleOpen">高级查询</a-button>
  </slot>
24
25
  <j-modal
26
    title="高级查询构造器"
27
    :width="1000"
28
29
30
    :visible="visible"
    @cancel="handleCancel"
    :mask="false"
31
    :fullscreen="izMobile"
32
    class="j-super-query-modal"
33
34
    style="top:5%;max-height: 95%;"
  >
35
36
    <template slot="footer">
37
38
39
40
41
42
      <div style="float: left">
        <a-button :loading="loading" @click="handleReset">重置</a-button>
        <a-button :loading="loading" @click="handleSave">保存查询条件</a-button>
      </div>
      <a-button :loading="loading" @click="handleCancel">关闭</a-button>
      <a-button :loading="loading" type="primary" @click="handleOk">查询</a-button>
43
44
    </template>
45
46
47
    <a-spin :spinning="loading">
      <a-row>
        <a-col :sm="24" :md="24-5">
48
49
          <a-empty v-if="queryParamsModel.length === 0" style="margin-bottom: 12px;">
50
51
52
53
54
55
56
57
58
            <div slot="description">
              <span>没有任何查询条件</span>
              <a-divider type="vertical"/>
              <a @click="handleAdd">点击新增</a>
            </div>
          </a-empty>

          <a-form v-else layout="inline">
59
60
61
62
63
64
65
66
67
68
            <a-row style="margin-bottom: 12px;">
              <a-col :md="12" :xs="24">
                <a-form-item label="过滤条件匹配" :labelCol="{md: 6,xs:24}" :wrapperCol="{md: 18,xs:24}" style="width: 100%;">
                  <a-select v-model="matchType" :getPopupContainer="node=>node.parentNode" style="width: 100%;">
                    <a-select-option value="and">AND(所有条件都要求匹配)</a-select-option>
                    <a-select-option value="or">OR(条件中的任意一个匹配)</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
69
70
71

            <a-row type="flex" style="margin-bottom:10px" :gutter="16" v-for="(item, index) in queryParamsModel" :key="index">
72
              <a-col :md="8" :xs="24" style="margin-bottom: 12px;">
73
74
75
76
77
78
79
80
81
82
83
84
85
                <a-tree-select
                  showSearch
                  v-model="item.field"
                  :treeData="fieldTreeData"
                  :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                  placeholder="选择查询字段"
                  allowClear
                  treeDefaultExpandAll
                  :getPopupContainer="node=>node.parentNode"
                  style="width: 100%"
                  @select="(val,option)=>handleSelected(option,item)"
                >
                </a-tree-select>
86
87
              </a-col>
88
89
              <a-col :md="4" :xs="24" style="margin-bottom: 12px;">
                <a-select placeholder="匹配规则" :value="item.rule" :getPopupContainer="node=>node.parentNode" @change="handleRuleChange(item,$event)">
90
                  <a-select-option value="eq">等于</a-select-option>
91
92
93
94
                  <a-select-option value="like">包含</a-select-option>
                  <a-select-option value="right_like">以..开始</a-select-option>
                  <a-select-option value="left_like">以..结尾</a-select-option>
                  <a-select-option value="in">在...中</a-select-option>
95
96
97
98
99
100
101
102
                  <a-select-option value="ne">不等于</a-select-option>
                  <a-select-option value="gt">大于</a-select-option>
                  <a-select-option value="ge">大于等于</a-select-option>
                  <a-select-option value="lt">小于</a-select-option>
                  <a-select-option value="le">小于等于</a-select-option>
                </a-select>
              </a-col>
103
              <a-col :md="8" :xs="24" style="margin-bottom: 12px;">
104
105
106
107
108
109
110
111
112
                <!-- 下拉搜索 -->
                <j-search-select-tag v-if="item.type==='sel_search'" v-model="item.val" :dict="getDictInfo(item)" placeholder="请选择"/>
                <!-- 下拉多选 -->
                <template v-else-if="item.type==='list_multi'">
                  <j-multi-select-tag v-if="item.options" v-model="item.val" :options="item.options" placeholder="请选择"/>
                  <j-multi-select-tag v-else v-model="item.val" :dictCode="getDictInfo(item)" placeholder="请选择"/>
                </template>

                <template v-else-if="item.dictCode">
113
114
115
116
117
118
119
                  <template v-if="item.type === 'table-dict'">
                    <j-popup
                      v-model="item.val"
                      :code="item.dictTable"
                      :field="item.dictCode"
                      :orgFields="item.dictCode"
                      :destFields="item.dictCode"
120
                      :multi="true"
121
122
                    ></j-popup>
                  </template>
123
124
125
126
                  <template v-else>
                    <j-multi-select-tag v-show="allowMultiple(item)" v-model="item.val" :dictCode="item.dictCode" placeholder="请选择"/>
                    <j-dict-select-tag v-show="!allowMultiple(item)" v-model="item.val" :dictCode="item.dictCode" placeholder="请选择"/>
                  </template>
127
                </template>
128
129
130
131
132
133
134
                <j-popup
                  v-else-if="item.type === 'popup'"
                  :value="item.val"
                  v-bind="item.popup"
                  group-id="superQuery"
                  @input="(e,v)=>handleChangeJPopup(item,e,v)"
                  :multi="true"/>
135
                <j-select-multi-user
136
                  v-else-if="item.type === 'select-user' || item.type === 'sel_user'"
137
138
139
140
141
142
143
                  v-model="item.val"
                  :buttons="false"
                  :multiple="false"
                  placeholder="请选择用户"
                  :returnKeys="['id', item.customReturnField || 'username']"
                />
                <j-select-depart
144
                  v-else-if="item.type === 'select-depart' || item.type === 'sel_depart'"
145
146
147
148
149
                  v-model="item.val"
                  :multi="false"
                  placeholder="请选择部门"
                  :customReturnField="item.customReturnField || 'id'"
                />
150
151
152
153
154
155
156
157
158
                <a-select
                  v-else-if="item.options instanceof Array"
                  v-model="item.val"
                  :options="item.options"
                  allowClear
                  placeholder="请选择"
                  :mode="allowMultiple(item)?'multiple':''"
                />
                <j-area-linkage v-model="item.val" v-else-if="item.type==='area-linkage' || item.type==='pca'" style="width: 100%"/>
159
160
                <j-date v-else-if=" item.type=='date' " v-model="item.val" placeholder="请选择日期" style="width: 100%"></j-date>
                <j-date v-else-if=" item.type=='datetime' " v-model="item.val" placeholder="请选择时间" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"></j-date>
161
                <a-time-picker v-else-if="item.type==='time'" :value="item.val ? moment(item.val,'HH:mm:ss') : null" format="HH:mm:ss" style="width: 100%" @change="(time,value)=>item.val=value"/>
162
                <a-input-number v-else-if=" item.type=='int'||item.type=='number' " style="width: 100%" placeholder="请输入数值" v-model="item.val"/>
163
164
165
166
                <a-select v-else-if="item.type=='switch'" placeholder="请选择" v-model="item.val">
                  <a-select-option value="Y">是</a-select-option>
                  <a-select-option value="N">否</a-select-option>
                </a-select>
167
168
169
                <a-input v-else v-model="item.val" placeholder="请输入值"/>
              </a-col>
170
171
172
173
174
175
              <a-col :md="4" :xs="0" style="margin-bottom: 12px;">
                <a-button @click="handleAdd" icon="plus"></a-button>&nbsp;
                <a-button @click="handleDel( index )" icon="minus"></a-button>
              </a-col>

              <a-col :md="0" :xs="24" style="margin-bottom: 12px;text-align: right;">
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
                <a-button @click="handleAdd" icon="plus"></a-button>&nbsp;
                <a-button @click="handleDel( index )" icon="minus"></a-button>
              </a-col>

            </a-row>

          </a-form>
        </a-col>
        <a-col :sm="24" :md="5">
          <!-- 查询记录 -->

          <a-card class="j-super-query-history-card" :bordered="true">
            <div slot="title">
              保存的查询
            </div>
191
192
            <a-empty v-if="saveTreeData.length === 0" class="j-super-query-history-empty" description="没有保存任何查询"/>
193
            <a-tree
194
              v-else
195
196
              class="j-super-query-history-tree"
              showIcon
197
198
              :treeData="saveTreeData"
              :selectedKeys="[]"
199
200
201
202
203
204
205
206
207
208
              @select="handleTreeSelect"
            >
            </a-tree>
          </a-card>


        </a-col>
      </a-row>
209
    </a-spin>
210
211
212
213
214

    <a-modal title="请输入保存的名称" :visible="prompt.visible" @cancel="prompt.visible=false" @ok="handlePromptOk">
      <a-input v-model="prompt.value"></a-input>
    </a-modal>
215
  </j-modal>
216
</div>
217
218
219
</template>

<script>
220
  import moment from 'moment'
221
  import * as utils from '@/utils/util'
222
  import { mixinDevice } from '@/utils/mixin'
223
  import JDate from '@/components/jeecg/JDate.vue'
224
225
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUser'
226
227
  import JMultiSelectTag from '@/components/dict/JMultiSelectTag'
  import JAreaLinkage from '@comp/jeecg/JAreaLinkage'
228
229
230

  export default {
    name: 'JSuperQuery',
231
232
    mixins: [mixinDevice],
    components: { JAreaLinkage, JMultiSelectTag, JDate, JSelectDepart, JSelectMultiUser },
233
234
235
236
237
238
239
240
241
    props: {
      /*
       fieldList: [{
          value:'',
          text:'',
          type:'',
          dictCode:'' // 只要 dictCode 有值,无论 type 是什么,都显示为字典下拉框
       }]
       type:date datetime int number string
242
      * */
243
244
245
      fieldList: {
        type: Array,
        required: true
246
247
248
249
      },
      /*
      * 这个回调函数接收一个数组参数 即查询条件
      * */
250
251
252
253
254
255
256
257
258
259
260
261
262
      callback: {
        type: String,
        required: false,
        default: 'handleSuperQuery'
      },

      // 当前是否在加载中
      loading: {
        type: Boolean,
        default: false
      },

      // 保存查询条件的唯一 code,通过该 code 区分
263
      // 默认为 null,代表以当前路由全路径为区分Code
264
265
      saveCode: {
        type: String,
266
        default: null
267
268
269
270
271
      }

    },
    data() {
      return {
272
        moment,
273
        fieldTreeData: [],
274
275
276
277
278
279
280

        prompt: {
          visible: false,
          value: ''
        },

        visible: false,
281
        queryParamsModel: [],
282
        treeIcon: <a-icon type="file-text"/>,
283
284
        // 保存查询条件的treeData
        saveTreeData: [],
285
286
        // 保存查询条件的前缀名
        saveCodeBefore: 'JSuperQuerySaved_',
287
288
289
        // 查询类型,过滤条件匹配(and、or)
        matchType: 'and',
        superQueryFlag: false,
290
291
      }
    },
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
    computed: {
      izMobile() {
        return this.device === 'mobile'
      },
      tooltipProps() {
        return this.izMobile ? { visible: false } : {}
      },
      fullSaveCode() {
        let saveCode = this.saveCode
        if (saveCode == null || saveCode === '') {
          saveCode = this.$route.fullPath
        }
        return this.saveCodeBefore + saveCode
      },
    },
307
308
    watch: {
      // 当 saveCode 变化时,重新查询已保存的条件
309
      fullSaveCode: {
310
        immediate: true,
311
312
        handler() {
          let list = this.$ls.get(this.fullSaveCode)
313
          if (list instanceof Array) {
314
            this.saveTreeData = list.map(i => this.renderSaveTreeData(i))
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
      },
      fieldList: {
        deep: true,
        immediate: true,
        handler(val) {
          let mainData = [], subData = []
          val.forEach(item => {
            let data = { ...item }
            data.label = data.label || data.text
            let hasChildren = (data.children instanceof Array)
            data.disabled = hasChildren
            data.selectable = !hasChildren
            if (hasChildren) {
              data.children = data.children.map(item2 => {
                let child = { ...item2 }
                child.label = child.label || child.text
                child.label = data.label + '-' + child.label
                child.value = data.value + ',' + child.value
                child.val = ''
                return child
              })
              data.val = ''
              subData.push(data)
            } else {
              mainData.push(data)
            }
          })
          this.fieldTreeData = mainData.concat(subData)
        }
346
347
      }
    },
348
349
350

    methods: {
      show() {
351
352
        if (!this.queryParamsModel || this.queryParamsModel.length === 0) {
          this.resetLine()
353
        }
354
        this.visible = true
355
      },
356
357
358
359
360
361
362
363
364
365
366

      getDictInfo(item) {
        let str = ''
        if(!item.dictTable){
          str = item.dictCode
        }else{
          str = item.dictTable+','+item.dictText+','+item.dictCode
        }
        console.log('高级查询字典信息',str)
        return str
      },
367
368
369
      handleOk() {
        if (!this.isNullArray(this.queryParamsModel)) {
          let event = {
370
371
372
373
374
375
            matchType: this.matchType,
            params: this.removeEmptyObject(this.queryParamsModel)
          }
          // 移动端模式下关闭弹窗
          if (this.izMobile) {
            this.visible = false
376
          }
377
          this.emitCallback(event)
378
        } else {
379
          this.$message.warn("不能查询空条件")
380
381
        }
      },
382
383
384
385
386
387
388
389
390
      emitCallback(event = {}) {
        let { params = [], matchType = this.matchType } = event
        this.superQueryFlag = (params && params.length > 0)
        for (let param of params) {
          if (Array.isArray(param.val)) {
            param.val = param.val.join(',')
          }
        }
        console.debug('---高级查询参数--->', { params, matchType })
391
392
        this.$emit(this.callback, params, matchType)
      },
393
      handleCancel() {
394
395
        this.close()
      },
396
397
398
      close() {
        this.$emit('close')
        this.visible = false
399
      },
400
      handleAdd() {
401
402
403
404
405
406
407
408
409
        this.addNewLine()
      },
      addNewLine() {
        this.queryParamsModel.push({ rule: 'eq' })
      },
      resetLine() {
        this.superQueryFlag = false
        this.queryParamsModel = []
        this.addNewLine()
410
      },
411
412
      handleDel(index) {
        this.queryParamsModel.splice(index, 1)
413
      },
414
      handleSelected(node, item) {
415
        let { type, options, dictCode, dictTable, dictText, customReturnField, popup } = node.dataRef
416
        item['type'] = type
417
        item['options'] = options
418
        item['dictCode'] = dictCode
419
        item['dictTable'] = dictTable
420
        item['dictText'] = dictText
421
        item['customReturnField'] = customReturnField
422
423
424
        if (popup) {
          item['popup'] = popup
        }
425
        this.$set(item, 'val', undefined)
426
      },
427
428
429
      handleOpen() {
        this.show()
      },
430
      handleReset() {
431
        this.resetLine()
432
        this.emitCallback()
433
      },
434
      handleSave() {
435
        let queryParams = this.removeEmptyObject(this.queryParamsModel)
436
437
438
439
440
441
442
443
444
        if (this.isNullArray(queryParams)) {
          this.$message.warning('空条件不能保存')
        } else {
          this.prompt.value = ''
          this.prompt.visible = true
        }
      },
      handlePromptOk() {
        let { value } = this.prompt
445
446
447
448
449
450
451
452
        if(!value){
          this.$message.warning('保存名称不能为空')
          return
        }
        // 取出查询条件
        let records = this.removeEmptyObject(this.queryParamsModel)
        // 判断有没有重名的
        let filterList = this.saveTreeData.filter(i => i.originTitle === value)
453
454
455
456
457
        if (filterList.length > 0) {
          this.$confirm({
            content: `${value} 已存在,是否覆盖?`,
            onOk: () => {
              this.prompt.visible = false
458
              filterList[0].records = records
459
460
461
462
463
              this.saveToLocalStore()
              this.$message.success('保存成功')
            }
          })
        } else {
464
          // 没有重名的,直接添加
465
          this.prompt.visible = false
466
467
          // 添加到树列表中
          this.saveTreeData.push(this.renderSaveTreeData({
468
            title: value,
469
470
471
472
            matchType: this.matchType,
            records: records
          }))
          // 保存到 LocalStore
473
474
475
476
477
478
          this.saveToLocalStore()
          this.$message.success('保存成功')
        }
      },
      handleTreeSelect(idx, event) {
        if (event.selectedNodes[0]) {
479
480
481
482
          let { matchType, records } = event.selectedNodes[0].data.props
          // 将保存的matchType取出,兼容旧数据,如果没有保存就还是使用原来的
          this.matchType = matchType || this.matchType
          this.queryParamsModel = utils.cloneObject(records)
483
484
        }
      },
485
486
487
488
      handleRemoveSaveTreeItem(event, vNode) {
        // 阻止事件冒泡
        event.stopPropagation()
489
490
491
        this.$confirm({
          content: '是否删除当前查询?',
          onOk: () => {
492
493
            let { eventKey } = vNode
            this.saveTreeData.splice(Number.parseInt(eventKey.substring(2)), 1)
494
495
496
497
498
499
500
            this.saveToLocalStore()
          },
        })
      },

      // 将查询保存到 LocalStore 里
      saveToLocalStore() {
501
502
        let saveValue = this.saveTreeData.map(({ originTitle, matchType, records }) => ({ title: originTitle, matchType, records }))
        this.$ls.set(this.fullSaveCode, saveValue)
503
504
505
      },

      isNullArray(array) {
506
        //判断是不是空数组对象
507
        if (!array || array.length === 0) {
508
509
          return true
        }
510
511
        if (array.length === 1) {
          let obj = array[0]
512
          if (!obj.field || (obj.val == null || obj.val === '') || !obj.rule) {
513
514
515
            return true
          }
        }
516
517
518
        return false
      },
      // 去掉数组中的空对象
519
520
      removeEmptyObject(arr) {
        let array = utils.cloneObject(arr)
521
522
523
524
        for (let i = 0; i < array.length; i++) {
          let item = array[i]
          if (item == null || Object.keys(item).length <= 0) {
            array.splice(i--, 1)
525
          } else {
526
527
            if (Array.isArray(item.options)) {
              // 如果有字典属性,就不需要保存 options 了
528
529
530
              //update-begin-author:taoyan date:20200819 for:【开源问题】 高级查询 下拉框作为并且选项很多多多 LOWCOD-779
              delete item.options
              //update-end-author:taoyan date:20200819 for:【开源问题】 高级查询 下拉框作为并且选项很多多多 LOWCOD-779
531
            }
532
533
534
          }
        }
        return array
535
536
537
538
539
540
      },

      /** 渲染保存查询条件的 title(加个删除按钮) */
      renderSaveTreeData(item) {
        item.icon = this.treeIcon
        item.originTitle = item['title']
541
542
543
544
545
546
547
548
549
550
551
        item.title = (arg1, arg2) => {
          let vNode
          // 兼容旧版的Antdv
          if (arg1.dataRef) {
            vNode = arg1
          } else if (arg2.dataRef) {
            vNode = arg2
          } else {
            return <span style="color:red;">Antdv版本不支持</span>
          }
          let {originTitle} = vNode.dataRef
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
          return (
            <div class="j-history-tree-title">
              <span>{originTitle}</span>

              <div class="j-history-tree-title-closer" onClick={e => this.handleRemoveSaveTreeItem(e, vNode)}>
                <a-icon type="close-circle"/>
              </div>
            </div>
          )
        }
        return item
      },

      /** 判断是否允许多选 */
      allowMultiple(item) {
        return item.rule === 'in'
      },

      handleRuleChange(item, newValue) {
        let oldValue = item.rule
        this.$set(item, 'rule', newValue)
        // 上一个规则是否是 in,且type是字典或下拉
        if (oldValue === 'in') {
          if (item.dictCode || item.options instanceof Array) {
            let value = item.val
            if (typeof item.val === 'string') {
              value = item.val.split(',')[0]
            } else if (Array.isArray(item.val)) {
              value = item.val[0]
            }
            this.$set(item, 'val', value)
          }
        }
      },

      handleChangeJPopup(item, e, values) {
        item.val = values[item.popup['destFields']]
      },
591
592
593
594
    }
  }
</script>
595
<style lang="less" scoped>
596
597
598
599
  .j-super-query-box {
    display: inline-block;
  }
600
601
  .j-super-query-modal {
602
603
604
605
    .j-super-query-history-card {
      /deep/ .ant-card-body,
      /deep/ .ant-card-head-title {
606
607
608
        padding: 0;
      }
609
      /deep/ .ant-card-head {
610
611
612
613
614
        padding: 4px 8px;
        min-height: initial;
      }
    }
615
616
    .j-super-query-history-empty {
      /deep/ .ant-empty-image {
617
618
619
620
621
        height: 80px;
        line-height: 80px;
        margin-bottom: 0;
      }
622
      /deep/ img {
623
624
625
626
        width: 80px;
        height: 65px;
      }
627
      /deep/ .ant-empty-description {
628
629
630
631
632
        color: #afafaf;
        margin: 8px 0;
      }
    }
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
    .j-super-query-history-tree {

      .j-history-tree-title {
        width: calc(100% - 24px);
        position: relative;
        display: inline-block;

        &-closer {
          color: #999999;
          position: absolute;
          top: 0;
          right: 0;
          width: 24px;
          height: 24px;
          text-align: center;
          opacity: 0;
          transition: opacity 0.3s, color 0.3s;

          &:hover {
            color: #666666;
          }

          &:active {
            color: #333333;
          }
        }

        &:hover {
          .j-history-tree-title-closer {
            opacity: 1;
          }
        }

      }

      /deep/ .ant-tree-switcher {
669
670
671
        display: none;
      }
672
      /deep/ .ant-tree-node-content-wrapper {
673
674
675
676
677
        width: 100%;
      }
    }

  }
678
679

</style>