wujingjing
2025-03-27 7f97bb126bfb6450ba0f8d2b5d4a632db74c3c58
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
<template>
    <ywDialog
        v-model="dialogIsShow"
        :headerIcon="dialogHeaderIcon"
        :title="dialogTitle"
        width="470"
        @dlgClosed="closeDialog"
        @submit="submitFormValue"
    >
        <el-form :model="dialogFormValue" ref="dialogFormRef" :rules="dialogFormRules" label-width="96">
            <el-form-item label="维度ID" prop="dim_id" v-if="!isEditDialog">
                <el-input v-model="dialogFormValue.dim_id"></el-input>
            </el-form-item>
            <el-form-item label="定义" prop="dim_title">
                <el-input type="textarea" :rows="3" v-model="dialogFormValue.dim_title"></el-input>
            </el-form-item>
            <el-form-item label="类型" prop="dim_type">
                <el-select v-model="dialogFormValue.dim_type">
                    <el-option
                        v-for="item in Object.keys(DimensionTypeMap)"
                        :key="item"
                        :value="item"
                        :label="DimensionTypeMap[item]"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="别名" prop="dim_alias">
                <div class="flex flex-col gap-2">
                    <div class="flex flex-col gap-3">
                        <div class="flex items-center gap-2" v-for="(item, index) in editAlias" :key="index">
                            <el-input v-model="item.key" class="w-[60px] flex-0 mb-auto font-bold"></el-input>
                            <TagInput class="flex-auto mt-1" v-model="item.value" />
                            <el-button size="small" type="danger" circle @click="deleteAlias(index)">
                                <el-icon><Delete /></el-icon>
                            </el-button>
                        </div>
                    </div>
                    <el-button size="small" type="primary" @click="addAlias">添加别名</el-button>
                </div>
            </el-form-item>
            <el-form-item label="名称" prop="dim_name">
                <el-input v-model="dialogFormValue.dim_name"></el-input>
            </el-form-item>
            <el-form-item label="主题域" prop="dim_group">
                <el-tree-select
                    filterable
                    :props="{
                        id: 'group_id',
                        label: 'group_name',
                        children: 'Children',
                    }"
                    v-model="dialogFormValue.dim_group"
                    node-key="group_id"
                    clearable
                    defaultExpandAll
                    :data="listTreeData"
                    placeholder="请选择主题域"
                    check-strictly
                >
                </el-tree-select>
            </el-form-item>
            <el-form-item label="单位" prop="dim_unit">
                <el-input v-model="dialogFormValue.dim_unit"></el-input>
            </el-form-item>
            <el-form-item label="值列表" prop="dim_value_list">
                <div class="flex flex-col gap-2 w-full">
                    <el-select v-model="activeValueListType" @change="changeValueListType">
                        <el-option
                            v-for="item in Object.keys(ValueListTypeMap)"
                            :key="item"
                            :value="item"
                            :label="ValueListTypeMap[item]"
                        ></el-option>
                    </el-select>
 
                    <!-- <el-input v-model="dialogFormValue.dim_value_list" type="textarea" :rows="3"></el-input> -->
                    <TagInput
                        v-if="activeValueListType === ValueListType.Value"
                        class="flex-auto mt-1"
                        v-model="dialogFormValue.dim_value_list.values"
                    />
                    <el-input
                        placeholder="请输入SQL语句"
                        v-if="activeValueListType === ValueListType.Sql"
                        v-model="dialogFormValue.dim_value_list.sql.sql"
                        type="textarea"
                        :rows="3"
                    ></el-input>
                    <el-select placeholder="请选择数据库" v-if="activeValueListType === ValueListType.Sql" v-model="dialogFormValue.dim_value_list.sql.ds_id">
                        <el-option v-for="item in dataSources" :key="item.id" :label="item.title" :value="item.id"></el-option>
                    </el-select>
                </div>
            </el-form-item>
        </el-form>
    </ywDialog>
</template>
 
<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
import { storeToRefs } from 'pinia';
import { computed, ref, watch } from 'vue';
import * as dimensionApi from '/@/api/dimension';
import ywDialog from '/@/components/dialog/yw-dialog.vue';
import { useUserInfo } from '/@/stores/userInfo';
import { formatDate } from '/@/utils/formatTime';
import { deepClone } from '/@/utils/other';
import TagInput from './TagInput.vue';
import { DimensionTypeMap } from './constants';
import { getDataSourceList } from '/@/api/dataSource';
 
const enum ValueListType {
    Sql = 'sql',
    Value = 'value',
}
 
const activeValueListType = ref(ValueListType.Value);
const ValueListTypeMap = {
    [ValueListType.Sql]: 'SQL查询',
    [ValueListType.Value]: '值输入',
};
 
const getEmptyValueList = (type = activeValueListType.value) => {
    return type === ValueListType.Sql
        ? {
                sql: {
                    ds_id: '',
                    sql: '',
                },
          }
        : {
                values: [],
          };
};
 
const changeValueListType = (val) => {
    dialogFormValue.value.dim_value_list = getEmptyValueList(val);
};
 
const checkIsSql = (valueList) => {
    return valueList && valueList.hasOwnProperty('sql');
};
const checkIsEmptyValueList = (valueList) => {
    if (!checkIsSql(valueList)) {
        return valueList.values.length === 0;
    }
    return valueList.sql.ds_id === '' && valueList.sql.sql === '';
};
 
const props = defineProps(['item', 'groupId', 'listTreeData', 'tableData', 'dataSources']);
const emit = defineEmits(['update', 'insert']);
 
const addAlias = () => {
    editAlias.value.push({
        key: '',
        value: [],
    });
};
 
const deleteAlias = (index) => {
    editAlias.value.splice(index, 1);
};
 
//#region ====================== 增加、修改记录操作, dialog init======================
const isEditDialog = ref(false);
const dialogTitle = computed(() => {
    return isEditDialog.value ? '修改维度' : '添加维度';
});
const dialogHeaderIcon = computed(() => {
    return isEditDialog.value ? 'ele-Edit' : 'ele-Plus';
});
const dialogFormValue = ref(null);
const dialogIsShow = defineModel({
    type: Boolean,
});
const dialogFormRef = ref<FormInstance>(null);
 
const dialogFormRules = ref<FormRules>({
    dim_id: [
        { required: true, message: '请输入维度ID', trigger: 'blur' },
        {
            validator: (rule, value, callback) => {
                if (!value) {
                    callback();
                    return;
                }
                const existItem = props.tableData.find((item) => item.id === value);
                if (existItem) {
                    callback(new Error('维度ID已存在'));
                    return;
                }
                callback();
            },
            trigger: 'blur',
        },
    ],
    dim_title: [{ required: true, message: '请输入维度定义', trigger: 'blur' }],
    dim_type: [{ required: true, message: '请选择维度类型', trigger: 'change' }],
    dim_name: [{ required: true, message: '请输入维度名称', trigger: 'blur' }],
});
const editAlias = ref([]);
 
const openOperateDialog = (row?) => {
    if (row) {
        isEditDialog.value = true;
        const {
            id: dim_id,
            prompt: dim_title,
            type: dim_type,
            alias: dim_alias,
            title: dim_name,
            group: dim_group,
            unit: dim_unit,
            dimValueList: dim_value_list,
        } = row;
        dialogFormValue.value = deepClone({
            dim_id,
            dim_title,
            dim_type,
            dim_alias,
            dim_name,
            dim_group,
            dim_unit,
            dim_value_list,
        });
    } else {
        isEditDialog.value = false;
        dialogFormValue.value = {
            dim_id: null,
            dim_title: null,
            dim_type: null,
            dim_alias: {},
            dim_name: null,
            dim_group: props.groupId,
            dim_unit: null,
            dim_value_list: null,
        };
    }
    editAlias.value = Object.keys(dialogFormValue.value.dim_alias ?? {}).map((item) => {
        return {
            key: item,
            value: dialogFormValue.value.dim_alias[item],
        };
    });
    dialogFormValue.value.dim_value_list = dialogFormValue.value.dim_value_list ?? getEmptyValueList();
    activeValueListType.value = checkIsSql(dialogFormValue.value.dim_value_list) ? ValueListType.Sql : ValueListType.Value;
    console.log("🚀 ~ dialogFormValue.value.dim_value_list:", dialogFormValue.value.dim_value_list)
};
 
const closeDialog = () => {
    dialogIsShow.value = false;
    dialogFormRef.value?.clearValidate();
};
 
const stores = useUserInfo();
const { userInfos } = storeToRefs(stores);
 
const checkAlias = () => {
    for (const item of editAlias.value) {
        if (!item.key) {
            ElMessage.error('别名的键不能为空');
            return false;
        }
    }
 
    const keyCount = editAlias.value.reduce((acc, item) => {
        acc[item.key] = (acc[item.key] || 0) + 1;
        return acc;
    }, {});
 
    for (const key in keyCount) {
        if (keyCount[key] > 1) {
            ElMessage.error(`别名的键 "${key}" 重复出现`);
            return false;
        }
    }
    return true;
};
 
const submitFormValue = async () => {
    const valid = await dialogFormRef.value?.validate().catch(() => {});
    if (!valid) return;
 
    if (!checkAlias()) return;
    dialogFormValue.value.dim_alias = editAlias.value.reduce((acc, item) => {
        acc[item.key] = item.value;
        return acc;
    }, {});
 
    const sendForm = {
        ...dialogFormValue.value,
        dim_alias: JSON.stringify(dialogFormValue.value.dim_alias),
        dim_value_list: checkIsEmptyValueList(dialogFormValue.value.dim_value_list)
            ? null
            : JSON.stringify(dialogFormValue.value.dim_value_list),
    };
 
    if (isEditDialog.value) {
        const res = await dimensionApi.updateDimensionByPost(sendForm);
        emit('update', {
            id: dialogFormValue.value.dim_id,
            prompt: dialogFormValue.value.dim_title,
            type: dialogFormValue.value.dim_type,
            alias: dialogFormValue.value.dim_alias,
            title: dialogFormValue.value.dim_name,
            group: dialogFormValue.value.dim_group,
            unit: dialogFormValue.value.dim_unit,
            dimValueList: dialogFormValue.value.dim_value_list,
        });
        closeDialog();
        ElMessage.success('修改维度成功');
    } else {
        const res = await dimensionApi.addDimensionByPost(sendForm);
        const newData = {
            id: dialogFormValue.value.dim_id,
            prompt: dialogFormValue.value.dim_title,
            type: dialogFormValue.value.dim_type,
            alias: dialogFormValue.value.dim_alias,
            title: dialogFormValue.value.dim_name,
            group: dialogFormValue.value.dim_group,
            unit: dialogFormValue.value.dim_unit,
            dimValueList: dialogFormValue.value.dim_value_list,
        };
        emit('insert', newData);
        closeDialog();
        ElMessage.success('添加维度成功');
    }
};
 
//#endregion
 
watch(
    () => dialogIsShow.value,
    (val) => {
        if (!val) return;
        openOperateDialog(props.item);
    }
);
</script>
 
<style scoped lang="scss"></style>