ny
昨天 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
<script lang="ts" setup>
import { computed, reactive, toRefs } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicDrawer, useDrawerInner } from '@jnpf/ui/drawer';
import { isEmpty } from '@jnpf/utils';
 
import { cloneDeep } from 'lodash-es';
 
interface State {
  currTable: any;
  parentTable: any;
  dataForm: any;
}
 
const props = defineProps({
  linkId: { type: String, default: '0' },
  sysVariableList: { type: Array, default: () => [] },
  getTableConfigTree: { type: Function },
});
 
const emit = defineEmits(['register', 'confirm']);
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 typeList = [
  { id: 1, fullName: '左连接', icon: 'icon-ym icon-ym-left-join' },
  { id: 2, fullName: '右连接', icon: 'icon-ym icon-ym-right-join' },
  { id: 3, fullName: '内连接', icon: 'icon-ym icon-ym-inner-join' },
  { id: 4, fullName: '全连接', icon: 'icon-ym icon-ym-full-join' },
];
const emptyChildItem = {
  field: '',
  dataType: 'text',
  symbol: '==',
  fieldValue: '',
  fieldValueType: 1,
};
const emptyItem = { logic: 'and', groups: [emptyChildItem] };
const state = reactive<State>({
  currTable: {},
  parentTable: {},
  dataForm: {},
});
const { dataForm } = toRefs(state);
const [registerDrawer, { closeDrawer }] = useDrawerInner(init);
const { createMessage } = useMessage();
 
const getParentTableFieldList = computed(() => {
  const item = {
    id: state.parentTable.table,
    fullName: state.parentTable.tableName,
    options: state.parentTable.fieldList.map((c) => ({ id: c.field, fullName: c.fieldName, dataType: c.dataType })),
  };
  return [item];
});
const getCurrTableFieldList = computed(() => {
  const item = {
    id: state.currTable.table,
    fullName: state.currTable.tableName,
    options: state.currTable.fieldList.map((c) => ({ id: c.field, fullName: c.fieldName, dataType: c.dataType })),
  };
  return [item];
});
const getFieldList = computed(() => {
  const parentTableObj = {
    id: state.parentTable.table,
    fullName: state.parentTable.tableName,
    options: state.parentTable.fieldList.map((c) => ({ id: `${state.parentTable.table}-${c.field}`, fullName: c.fieldName, dataType: c.dataType })),
  };
  const currTableObj = {
    id: state.currTable.table,
    fullName: state.currTable.tableName,
    options: state.currTable.fieldList.map((c) => ({ id: `${state.currTable.table}-${c.field}`, fullName: c.fieldName, dataType: c.dataType })),
  };
  return [parentTableObj, currTableObj];
});
 
function init(data) {
  state.currTable = data;
  state.parentTable = (props.getTableConfigTree as any)().getSelectedNode(data.parentTable);
  state.dataForm = cloneDeep(data.relationConfig);
}
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 (item.dataType === 'text') {
    if (['notNull', 'null'].includes(item.symbol)) {
      item.fieldValueType = 1;
      item.fieldValue = '';
    }
    if (
      (['in', 'notIn'].includes(item.symbol) && typeof item.fieldValue === 'string') ||
      (!['in', 'notIn'].includes(item.symbol) && typeof item.fieldValue !== 'string')
    )
      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 = '';
}
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 addRelationItem() {
  state.dataForm.relationList.push({ pField: '', field: '' });
}
function delRelationItem(index) {
  state.dataForm.relationList.splice(index, 1);
}
function relationExist() {
  const list = state.dataForm.relationList;
  let isOk = true;
  for (const element of list) {
    if (!element.pField) {
      createMessage.warning(`关联字段中父表字段不能为空`);
      isOk = false;
      break;
    }
    if (!element.field) {
      createMessage.warning(`关联字段中当前表字段不能为空`);
      isOk = false;
    }
  }
  return isOk;
}
function handleSubmit() {
  if (!state.dataForm.relationList.length) return createMessage.warning('请至少配置一组字段关联');
  if (!relationExist()) return;
  if (!conditionExist()) return;
  emit('confirm', state.dataForm);
  closeDrawer();
}
</script>
<template>
  <BasicDrawer
    v-bind="$attrs"
    width="500px"
    @register="registerDrawer"
    title="数据连接"
    class="dataSet-table-config-drawer"
    show-footer
    :mask-closable="false"
    @ok="handleSubmit"
    destroy-on-close>
    <div class="px-[20px] pb-[20px]">
      <div class="common-cap">
        <span class="title">关联关系</span>
      </div>
      <a-radio-group v-model:value="dataForm.type" button-style="solid">
        <a-radio-button :value="item.id" v-for="item in typeList" :key="item.id">
          <i class="mr-[10px]" :class="item.icon"></i>{{ item.fullName }}
        </a-radio-button>
      </a-radio-group>
      <div class="common-cap">
        <span class="title">关联字段</span>
      </div>
      <div class="condition-main">
        <a-row :gutter="8" v-for="(item, index) in dataForm.relationList" :key="index" class="mb-[10px]">
          <a-col :span="10">
            <jnpf-select v-model:value="item.pField" :options="getParentTableFieldList" show-search allow-clear />
          </a-col>
          <a-col :span="2" class="symbol-text leading-[32px]">等于</a-col>
          <a-col :span="10">
            <jnpf-select v-model:value="item.field" :options="getCurrTableFieldList" show-search allow-clear />
          </a-col>
          <a-col :span="2" class="text-center">
            <i class="icon-ym icon-ym-btn-clearn" @click="delRelationItem(index)"></i>
          </a-col>
        </a-row>
        <span class="link-text inline-block" @click="addRelationItem()"><i class="icon-ym icon-ym-btn-add mr-[4px] text-[14px]"></i>新增关联字段</span>
      </div>
      <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="getFieldList" placeholder="请选择字段" allow-clear show-search />
              </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
                    mode="tags"
                    :open="false"
                    :disabled="child.disabled"
                    v-if="child.fieldValueType === 1 && ['in', 'notIn'].includes(child.symbol)" />
                  <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 placeholder="请选择系统参数" 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>
    </div>
  </BasicDrawer>
</template>