feat: 新增SQL数据造数功能,方便为数据库填充数据

pull/144/head
klaokai 2023-04-29 14:18:26 +08:00
parent 2ab8ea45da
commit 756fc1038c
5 changed files with 292 additions and 1 deletions

View File

@ -57,6 +57,16 @@ export type CodegenPreviewVO = {
code: string
}
export type CodegenFakeDataVO = {
tableId: string
num: string
}
export type CodegenMockTypeRespVO = {
type: number
lable: string
}
export type CodegenUpdateReqVO = {
table: CodegenTableVO | any
columns: CodegenColumnVO[]
@ -102,6 +112,21 @@ export const previewCodegen = (id: number) => {
return request.get({ url: '/infra/codegen/preview?tableId=' + id })
}
// SQL造数生成代码
export const fakeDataCodegen = (params) => {
return request.get({ url: '/infra/codegen/fake-data', params })
}
// 获取SQL字段模拟类型列表
export const fakeDataMockTypeList = () => {
return request.get({ url: '/infra/codegen/fake-data/types' })
}
//通过模拟类型获取响应的列表参数
export const getMockParamsByMockType = (type: number) => {
return request.get({ url: '/infra/codegen/fake-data/params?mockType=' + type })
}
// 下载生成代码
export const downloadCodegen = (id: number) => {
return request.download({ url: '/infra/codegen/download?tableId=' + id })

View File

@ -0,0 +1,152 @@
<template>
<Dialog v-model="dialogVisible" title="代码预览" width="80%">
<div class="common-layout">
<el-container v-if="active == 1">
<el-header>请配置模拟类型模拟参数以及模拟参数默认值</el-header>
<el-main>
<fake-data-colum-info-form ref="fakeDataColumInfoFormRef" :columns="formData.columns" />
</el-main>
<el-footer>
<div class="slider-demo-block">
<span class="demonstration">数据生成量</span>
<el-slider v-model="queryParams.num" :step="10" show-stops />
</div>
</el-footer>
</el-container>
<el-container v-if="active == 2">
<el-header>请配置模拟类型模拟参数以及模拟参数默认值</el-header>
<el-main>
<div v-loading="loading" :gutter="12" element-loading-text="加载代码中..." shadow="hover">
<div>
<pre><code class="hljs" v-html="highlightedCode(previewResult)"></code></pre>
</div>
</div>
</el-main>
<el-footer
><el-button class="float-right" text type="primary" @click="copy()">
{{ t('common.copy') }}
</el-button></el-footer
>
</el-container>
</div>
<el-steps :active="active" finish-status="success">
<el-step description="请配置模拟类型、模拟参数以及模拟参数默认值" title="步骤 1" />
<el-step description="SQL" title="步骤 2" />
</el-steps>
<el-button style="margin-top: 12px" @click="next"></el-button>
</Dialog>
</template>
<script lang="ts" name="InfraCodegenFakeData" setup>
import { FakeDataColumInfoForm } from './components'
import { ref } from 'vue'
import * as CodegenApi from '@/api/infra/codegen'
import 'highlight.js/styles/github.css' //
import hljs from 'highlight.js' //
import sql from 'highlight.js/lib/languages/sql'
import { useClipboard } from '@vueuse/core'
const { t } = useI18n() //
const message = useMessage() //
const dialogVisible = ref(false) //
const loading = ref(false) //
const fakeDataColumInfoFormRef = ref<ComponentRef<typeof FakeDataColumInfoForm>>()
const formLoading = ref(false) // 12
const formData = ref<CodegenApi.CodegenUpdateReqVO>({
table: {},
columns: []
})
/** 获得详情 */
const getDetail = async (id: number) => {
if (!id) {
return
}
formLoading.value = true
try {
formData.value = await CodegenApi.getCodegenTable(id)
} finally {
formLoading.value = false
}
}
/** 加载sql造数代码*/
const previewResult = ref('')
const queryParams = reactive({
tableId: undefined,
num: 1
})
const getFakeData = async () => {
loading.value = true
try {
previewResult.value = await CodegenApi.fakeDataCodegen(queryParams)
} finally {
loading.value = false
}
}
const active = ref(1)
const next = () => {
if (active.value == 1) {
submitForm()
getFakeData()
}
if (active.value++ > 1) active.value = 1
}
/** 提交按钮 */
const submitForm = async () => {
//
if (!unref(formData)) return
try {
//
await CodegenApi.updateCodegenTable(formData.value)
message.success(t('common.updateSuccess'))
close()
} catch {}
}
/** 打开弹窗 */
const open = async (id: number) => {
//
active.value = 1
//
previewResult.value = ''
//id
queryParams.tableId = id
getDetail(id)
dialogVisible.value = true
}
defineExpose({ open }) // open
/** 代码高亮 **/
const highlightedCode = (item) => {
const result = hljs.highlight('sql', item || '', true)
return result.value || '&nbsp;'
}
/** 复制 **/
const copy = async () => {
const { copy, copied, isSupported } = useClipboard({ source: previewResult.value })
if (!isSupported) {
message.error(t('common.copyError'))
return
}
await copy()
if (unref(copied)) {
message.success(t('common.copySuccess'))
}
}
if (onMounted) {
/** 初始化 **/
onMounted(async () => {
//
hljs.registerLanguage('sql', sql)
})
}
</script>
<style lang="scss"></style>

View File

@ -0,0 +1,96 @@
<template>
<el-table ref="dragTable" :data="formData" :max-height="tableHeight" row-key="columnId">
<el-table-column
:show-overflow-tooltip="true"
label="字段列名"
min-width="10%"
prop="columnName"
/>
<el-table-column label="字段描述" min-width="10%" prop="columnComment" />
<el-table-column
:show-overflow-tooltip="true"
label="物理类型"
min-width="10%"
prop="dataType"
/>
<el-table-column label="Java类型" min-width="11%" prop="javaType" />
<el-table-column label="java属性" min-width="10%" prop="javaField" />
<el-table-column label="插入" min-width="4%" prop="createOperation" />
<el-table-column label="编辑" min-width="4%" prop="updateOperation" />
<el-table-column label="允许空" min-width="5%" prop="nullable" />
<el-table-column label="示例" min-width="10%" prop="example" />
<el-table-column label="模拟类型" min-width="11%">
<template #default="scope">
<el-select v-model="scope.row.mockType" clearable filterable placeholder="请选择">
<el-option
v-for="dict in dictOptions"
:key="dict.label"
:label="dict.label"
:value="dict.type"
/>
</el-select>
</template>
</el-table-column>
<el-table-column label="模拟参数" min-width="10%">
<template #default="scope">
<el-select
v-if="scope.row.mockType == 4"
v-model="scope.row.mockParams"
clearable
filterable
placeholder="请选择"
>
<el-option v-for="dict in mockParamOptions" :key="dict" :label="dict" :value="dict" />
</el-select>
<el-input v-else v-model="scope.row.mockParams" />
</template>
</el-table-column>
<el-table-column label="模拟参数默认值" min-width="10%">
<template #default="scope">
<el-input v-model="scope.row.defaultValue" />
</template>
</el-table-column>
</el-table>
</template>
<script lang="ts" name="InfraCodegenFakeDataColumInfoForm" setup>
import { PropType } from 'vue'
import * as CodegenApi from '@/api/infra/codegen'
const props = defineProps({
columns: {
type: Array as unknown as PropType<CodegenApi.CodegenColumnVO[]>,
default: () => null
}
})
const formData = ref<CodegenApi.CodegenColumnVO[]>([])
const tableHeight = document.documentElement.scrollHeight - 350 + 'px'
/** 查询模拟类型下拉列表 */
const dictOptions = ref<CodegenApi.CodegenMockTypeRespVO[]>([])
const getDictOptions = async () => {
dictOptions.value = await CodegenApi.fakeDataMockTypeList()
}
const mockParamOptions = ref<string[]>([]) //
const getMockParmOptions = async () => {
mockParamOptions.value = await CodegenApi.getMockParamsByMockType(4)
}
watch(
() => props.columns,
(columns) => {
if (!columns) return
formData.value = columns
},
{
deep: true,
immediate: true
}
)
onMounted(async () => {
await getDictOptions()
await getMockParmOptions()
})
</script>

View File

@ -1,4 +1,5 @@
import BasicInfoForm from './BasicInfoForm.vue'
import ColumInfoForm from './ColumInfoForm.vue'
import GenerateInfoForm from './GenerateInfoForm.vue'
export { BasicInfoForm, ColumInfoForm, GenerateInfoForm }
import FakeDataColumInfoForm from './FakeDataColumInfoForm.vue'
export { BasicInfoForm, ColumInfoForm, GenerateInfoForm, FakeDataColumInfoForm }

View File

@ -100,6 +100,14 @@
>
预览
</el-button>
<el-button
v-hasPermi="['infra:codegen:preview']"
link
type="primary"
@click="fakeData(scope.row)"
>
造数
</el-button>
<el-button
v-hasPermi="['infra:codegen:update']"
link
@ -148,6 +156,8 @@
<ImportTable ref="importRef" @success="getList" />
<!-- 弹窗预览代码 -->
<PreviewCode ref="previewRef" />
<!-- 弹窗造数 -->
<FakeData ref="fakeDataRef" />
</template>
<script lang="ts" name="InfraCodegen" setup>
import { dateFormatter } from '@/utils/formatTime'
@ -156,6 +166,7 @@ import * as CodegenApi from '@/api/infra/codegen'
import * as DataSourceConfigApi from '@/api/infra/dataSourceConfig'
import ImportTable from './ImportTable.vue'
import PreviewCode from './PreviewCode.vue'
import FakeData from './FakeData.vue'
const message = useMessage() //
const { t } = useI18n() //
@ -215,6 +226,12 @@ const handlePreview = (row: CodegenApi.CodegenTableVO) => {
previewRef.value.open(row.id)
}
/** 造数操作 */
const fakeDataRef = ref()
const fakeData = (row: CodegenApi.CodegenTableVO) => {
fakeDataRef.value.open(row.id)
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
try {