ny
23 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import { computed, reactive, toRefs } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicDrawer, useDrawer, useDrawerInner } from '@jnpf/ui/drawer';
import { isEmpty } from '@jnpf/utils';
 
import { cloneDeep } from 'lodash-es';
 
import { getDataModelFieldList } from '#/api/systemData/dataModel';
import { $t } from '#/locales';
 
import RelationConfigDrawer from './RelationConfigDrawer.vue';
import TableSelect from './TableSelect.vue';
 
interface State {
  dataForm: any;
  isEdit: boolean;
  allFieldList: any[];
  selectedRowKeys: string[];
  oldTable: string;
}
 
const props = defineProps({
  linkId: { type: String, default: '0' },
  sysVariableList: { type: Array, default: () => [] },
  getTableConfigTree: { type: Function },
});
 
const emit = defineEmits(['register', 'confirm', 'close']);
const fieldColumns = [
  { title: '字段名', dataIndex: 'field', key: 'field', width: 200 },
  { title: '字段描述', dataIndex: 'fieldName', key: 'fieldName' },
];
const logicOptions = [
  { id: 'and', fullName: '且' },
  { id: 'or', fullName: '或' },
];
const dataTypeOptions = [
  { id: 'text', fullName: 'text' },
  { id: 'double', fullName: 'double' },
  { id: 'bigint', fullName: 'bigint' },
  { id: 'date', fullName: 'date' },
  { id: 'time', fullName: 'time' },
];
const baseSymbolOptions = [
  { id: '==', fullName: '等于' },
  { id: '<>', fullName: '不等于' },
  { id: 'like', fullName: '包含' },
  { id: 'notLike', fullName: '不包含' },
  { id: 'null', fullName: '为空' },
  { id: 'notNull', fullName: '不为空' },
  { id: 'in', fullName: '包含任意一个' },
  { id: 'notIn', fullName: '不包含任意一个' },
];
const rangeSymbolOptions = [
  { id: '>=', fullName: '大于等于' },
  { id: '>', fullName: '大于' },
  { id: '==', fullName: '等于' },
  { id: '<=', fullName: '小于等于' },
  { id: '<', fullName: '小于' },
  { id: '<>', fullName: '不等于' },
  { id: 'between', fullName: '介于' },
  { id: 'null', fullName: '为空' },
  { id: 'notNull', fullName: '不为空' },
];
const fieldValueTypeOptions = [
  { id: 1, fullName: '自定义' },
  { id: 2, fullName: '系统参数' },
];
const emptyChildItem = {
  field: '',
  dataType: 'text',
  symbol: '==',
  fieldValue: '',
  fieldValueType: 1,
};
const emptyItem = { logic: 'and', groups: [emptyChildItem] };
const defaultRelationConfig = {
  ruleList: [],
  matchLogic: 'and',
  type: 1,
  relationList: [],
};
const defaultDataForm = {
  parentTable: '',
  table: '',
  tableName: '',
  fieldList: [],
  ruleList: [],
  matchLogic: 'and',
  relationConfig: defaultRelationConfig,
  children: [],
};
const state = reactive<State>({
  dataForm: {},
  isEdit: false,
  allFieldList: [],
  selectedRowKeys: [],
  oldTable: '',
});
const { dataForm, allFieldList, selectedRowKeys } = toRefs(state);
const [registerDrawer, { closeDrawer }] = useDrawerInner(init);
const [registerRelationConfigDrawer, { openDrawer: openConfigDrawer }] = useDrawer();
const { createMessage, createConfirm } = useMessage();
 
const getConfigBind = computed(() => ({ ...props }));
 
function init(data) {
  state.selectedRowKeys = [];
  state.allFieldList = [];
  state.isEdit = !!data.data;
  const dataForm = data.data ? cloneDeep(data.data) : cloneDeep(defaultDataForm);
  if (!state.isEdit && data.parentTable) dataForm.parentTable = data.parentTable;
  if (state.isEdit) getTableFieldList(dataForm.table, true);
  state.oldTable = dataForm.table;
  state.dataForm = dataForm;
}
function onTableChange(table, item) {
  if (!table) return handleNull();
  if (state.dataForm.table !== table) handleNull();
  getTableFieldList(table, state.dataForm.table === table);
  state.dataForm.table = table;
  state.dataForm.tableName = table + (item.tableName ? `(${item.tableName})` : '');
}
function handleNull() {
  state.dataForm.table = '';
  state.dataForm.tableName = '';
  state.dataForm.fieldList = [];
  state.dataForm.ruleList = [];
}
function getTableFieldList(table, isInit = false) {
  getDataModelFieldList(props.linkId, table).then((res) => {
    state.allFieldList = res.data.list;
    if (isInit) {
      const fieldList = state.dataForm.fieldList.filter((o) => state.allFieldList.some((e) => o.field === e.field));
      state.dataForm.fieldList = fieldList;
      state.selectedRowKeys = state.dataForm.fieldList.map((o) => o.field);
    } else {
      state.selectedRowKeys = state.allFieldList.map((o) => o.field);
      const fieldList = state.allFieldList.map((o) => ({
        field: o.field,
        fieldName: o.field + (o.fieldName ? `(${o.fieldName})` : ''),
        dataType: o.dataType,
      }));
      state.dataForm.fieldList = fieldList;
    }
  });
}
function onSelectedChange(selectedRowKeys, selectedRows) {
  state.selectedRowKeys = selectedRowKeys;
  state.dataForm.fieldList = selectedRows.map((o) => ({
    field: o.field,
    fieldName: o.field + (o.fieldName ? `(${o.fieldName})` : ''),
    dataType: o.dataType,
  }));
}
function addRuleItem(index) {
  state.dataForm.ruleList[index].groups.push(cloneDeep(emptyChildItem));
}
function delRuleItem(index, childIndex) {
  state.dataForm.ruleList[index].groups.splice(childIndex, 1);
  if (!state.dataForm.ruleList[index].groups.length) delRuleGroup(index);
}
function addRuleGroup() {
  state.dataForm.ruleList.push(cloneDeep(emptyItem));
}
function delRuleGroup(index) {
  state.dataForm.ruleList.splice(index, 1);
}
function onDataTypeChange(item) {
  item.fieldValueType = 1;
  if (item.dataType === 'text') {
    if (!baseSymbolOptions.some((o) => o.id === item.symbol)) {
      item.symbol = '==';
    }
    item.fieldValue = '';
  } else {
    if (!rangeSymbolOptions.some((o) => o.id === item.symbol)) {
      item.symbol = '==';
    }
    item.fieldValue = undefined;
  }
}
function onSymbolChange(item) {
  if (Array.isArray(item.fieldValue) && item.fieldValue?.length > 1 && !['in', 'notIn'].includes(item.symbol)) item.fieldValue = undefined;
  if (item.dataType === 'text') {
    if (['notNull', 'null'].includes(item.symbol)) {
      item.fieldValueType = 1;
      item.fieldValue = '';
    }
    if (['in', 'notIn'].includes(item.symbol)) {
      item.fieldValue = undefined;
    }
  } else {
    if (['notNull', 'null'].includes(item.symbol)) {
      item.fieldValue = undefined;
    } else if (item.symbol === 'between') {
      !Array.isArray(item.fieldValue) && (item.fieldValue = []);
    } else {
      Array.isArray(item.fieldValue) && (item.fieldValue = undefined);
    }
  }
}
function onFieldValueTypeChange(item) {
  item.fieldValue = undefined;
}
function openRelationConfig() {
  if (!state.dataForm.fieldList.length) return createMessage.warning('请至少选择一个字段');
  openConfigDrawer(true, { ...state.dataForm });
}
function onRelationConfigConfirm(data) {
  state.dataForm.relationConfig = data;
}
function conditionExist() {
  const list = state.dataForm.ruleList;
  let isOk = true;
  outer: for (const e of list) {
    for (let j = 0; j < e.groups.length; j++) {
      const child = e.groups[j];
      if (!child.field) {
        createMessage.warning(`字段不能为空`);
        isOk = false;
        break outer;
      }
      if (child.fieldValueType === 2 && !child.fieldValue) {
        createMessage.warning(`系统参数不能为空`);
        isOk = false;
        break outer;
      }
      if (
        child.fieldValueType === 1 &&
        !['notNull', 'null'].includes(child.symbol) &&
        ((!child.fieldValue && child.fieldValue !== 0) || isEmpty(child.fieldValue))
      ) {
        createMessage.warning('数据值不能为空');
        isOk = false;
        return;
      }
    }
  }
  return isOk;
}
function handleSubmit() {
  if (!state.dataForm.table) return createMessage.warning('请选择数据库表');
  if (state.dataForm.parentTable) {
    let allKeys = (props.getTableConfigTree as any)().getAllKeys();
    if (state.isEdit) allKeys = allKeys.filter((o) => o != state.oldTable);
    if (allKeys.includes(state.dataForm.table)) return createMessage.warning('数据库表不能重复,请重新选择');
  }
  if (!state.dataForm.fieldList.length) return createMessage.warning('请至少选择一个字段');
  if (!conditionExist()) return;
  if (state.dataForm.parentTable && !state.dataForm?.relationConfig?.relationList?.length) return createMessage.warning('请至少配置一组字段关联');
  if (state.oldTable && state.dataForm.table != state.oldTable) {
    createConfirm({
      iconType: 'warning',
      title: $t('common.tipTitle'),
      content: `切换数据库表将清空关联子表,确定要继续?`,
      onOk: () => {
        emit('confirm', state.dataForm, state.isEdit, true);
        closeDrawer();
      },
    });
    return;
  }
  emit('confirm', state.dataForm, state.isEdit);
  closeDrawer();
}
function handleClose() {
  emit('close');
  return true;
}
</script>
<template>
  <BasicDrawer
    v-bind="$attrs"
    width="500px"
    title="数据库表设置"
    @register="registerDrawer"
    class="dataSet-table-config-drawer"
    show-footer
    :mask-closable="false"
    @ok="handleSubmit"
    :close-func="handleClose"
    destroy-on-close>
    <div class="p-[20px]">
      <div class="common-cap !mt-0">
        <span class="title w-[80px] flex-shrink-0">数据库表</span>
        <div class="flex-1">
          <TableSelect :value="dataForm.table" :title="dataForm.tableName" :link-id="linkId" @change="onTableChange" />
        </div>
      </div>
      <template v-if="dataForm.table">
        <div class="common-cap">
          <span class="title">选择 {{ dataForm?.fieldList?.length || 0 }}/{{ allFieldList.length }}</span>
        </div>
        <a-table
          :data-source="allFieldList"
          :columns="fieldColumns"
          size="small"
          :pagination="false"
          :scroll="{ y: '300px' }"
          row-key="field"
          :row-selection="{ columnWidth: 50, selectedRowKeys, onChange: onSelectedChange }" />
        <div class="common-cap">
          <span class="title">条件筛选</span>
        </div>
        <div class="condition-main overflow-auto">
          <div class="mb-[10px]" v-if="dataForm.ruleList?.length">
            <jnpf-radio v-model:value="dataForm.matchLogic" :options="logicOptions" option-type="button" button-style="solid" />
          </div>
          <div class="condition-item" v-for="(item, index) in dataForm.ruleList" :key="index">
            <div class="condition-item-title">
              <div>条件组</div>
              <i class="icon-ym icon-ym-nav-close" @click="delRuleGroup(index)"></i>
            </div>
            <div class="condition-item-content">
              <div class="condition-item-cap">
                以下条件全部执行:
                <jnpf-radio v-model:value="item.logic" :options="logicOptions" option-type="button" button-style="solid" size="small" />
              </div>
              <a-row :gutter="8" v-for="(child, childIndex) in item.groups" :key="index + childIndex" wrap class="mb-[10px]">
                <a-col :span="18" class="!flex items-center">
                  <jnpf-select
                    v-model:value="child.field"
                    :options="dataForm.fieldList"
                    placeholder="请选择字段"
                    allow-clear
                    show-search
                    :field-names="{ label: 'fieldName', value: 'field' }" />
                </a-col>
                <a-col :span="6">
                  <jnpf-select class="w-full" v-model:value="child.dataType" :options="dataTypeOptions" @change="onDataTypeChange(child)" />
                </a-col>
                <a-col :span="6" class="mt-[10px]">
                  <jnpf-select
                    class="w-full"
                    v-model:value="child.symbol"
                    :options="child.dataType === 'text' ? baseSymbolOptions : rangeSymbolOptions"
                    @change="onSymbolChange(child)" />
                </a-col>
                <a-col :span="16" class="mt-[10px]" v-if="['double', 'bigint', 'date', 'time'].includes(child.dataType)">
                  <template v-if="['double', 'bigint'].includes(child.dataType)">
                    <jnpf-number-range v-model:value="child.fieldValue" v-if="child.symbol == 'between'" />
                    <jnpf-input-number v-model:value="child.fieldValue" placeholder="请输入" :disabled="['null', 'notNull'].includes(child.symbol)" v-else />
                  </template>
                  <template v-else-if="['date', 'time'].includes(child.dataType)">
                    <jnpf-date-range
                      v-model:value="child.fieldValue"
                      :format="child.dataType === 'time' ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD'"
                      allow-clear
                      v-if="child.symbol == 'between'" />
                    <jnpf-date-picker
                      v-model:value="child.fieldValue"
                      :format="child.dataType === 'time' ? 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD'"
                      allow-clear
                      :disabled="['null', 'notNull'].includes(child.symbol)"
                      v-else />
                  </template>
                </a-col>
                <template v-else>
                  <a-col :span="6" class="mt-[10px]">
                    <jnpf-select
                      v-model:value="child.fieldValueType"
                      :options="fieldValueTypeOptions"
                      @change="onFieldValueTypeChange(child)"
                      :disabled="['null', 'notNull'].includes(child.symbol)" />
                  </a-col>
                  <a-col :span="10" class="mt-[10px]">
                    <a-select
                      v-model:value="child.fieldValue"
                      placeholder="请输入"
                      allow-clear
                      :open="false"
                      mode="tags"
                      :disabled="child.disabled"
                      v-if="['in', 'notIn'].includes(child.symbol) && child.fieldValueType === 1" />
                    <a-input
                      v-model:value="child.fieldValue"
                      placeholder="请输入"
                      allow-clear
                      :disabled="['null', 'notNull'].includes(child.symbol)"
                      v-else-if="child.fieldValueType === 1" />
                    <jnpf-select v-model:value="child.fieldValue" :options="sysVariableList" allow-clear show-search v-else />
                  </a-col>
                </template>
                <a-col :span="2" class="mt-[10px] text-center">
                  <i class="icon-ym icon-ym-btn-clearn" @click="delRuleItem(index, childIndex)"></i>
                </a-col>
              </a-row>
              <span class="link-text inline-block" @click="addRuleItem(index)"><i class="icon-ym icon-ym-btn-add mr-[4px] text-[14px]"></i>添加条件</span>
            </div>
          </div>
          <span class="link-text inline-block" @click="addRuleGroup()"><i class="icon-ym icon-ym-btn-add mr-[4px] text-[14px]"></i>添加条件组</span>
        </div>
        <template v-if="!!dataForm.parentTable">
          <div class="common-cap">
            <span class="title">数据连接</span>
          </div>
          <a-button block @click="openRelationConfig()">配置数据连接</a-button>
        </template>
      </template>
    </div>
    <RelationConfigDrawer v-bind="getConfigBind" @register="registerRelationConfigDrawer" @confirm="onRelationConfigConfirm" />
  </BasicDrawer>
</template>
<style lang="scss">
.dataSet-table-config-drawer {
  .common-cap {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 15px 0;
    font-size: 14px;
 
    .title {
      overflow: hidden;
      text-overflow: ellipsis;
      font-size: 16px;
      white-space: nowrap;
    }
  }
}
</style>