ImportTable.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <script setup lang="ts">
  2. import { reactive, ref } from 'vue'
  3. import { getSchemaTableListApi, createCodegenListApi } from '@/api/infra/codegen'
  4. import {
  5. ElMessage,
  6. ElTable,
  7. ElTableColumn,
  8. ElForm,
  9. ElFormItem,
  10. ElInput,
  11. ElSelect,
  12. ElOption
  13. } from 'element-plus'
  14. import { useI18n } from '@/hooks/web/useI18n'
  15. import { getDataSourceConfigListApi } from '@/api/infra/dataSourceConfig'
  16. import type { DataSourceConfigVO } from '@/api/infra/dataSourceConfig/types'
  17. import type { DatabaseTableVO } from '@/api/infra/codegen/types'
  18. const { t } = useI18n() // 国际化
  19. const emit = defineEmits(['ok'])
  20. // ======== 显示页面 ========
  21. const visible = ref(false)
  22. const dbLoading = ref(true)
  23. const queryParams = reactive({
  24. tableName: undefined,
  25. tableComment: undefined,
  26. dataSourceConfigId: 0
  27. })
  28. const dataSourceConfigs = ref<DataSourceConfigVO[]>([])
  29. const show = async () => {
  30. const res = await getDataSourceConfigListApi()
  31. dataSourceConfigs.value = res
  32. queryParams.dataSourceConfigId = dataSourceConfigs.value[0].id
  33. visible.value = true
  34. await getList()
  35. }
  36. /** 查询表数据 */
  37. const dbTableList = ref<DatabaseTableVO[]>([])
  38. /** 查询表数据 */
  39. const getList = async () => {
  40. dbLoading.value = true
  41. const res = await getSchemaTableListApi(queryParams)
  42. dbTableList.value = res
  43. dbLoading.value = false
  44. }
  45. // 查询操作
  46. const handleQuery = async () => {
  47. await getList()
  48. }
  49. // 重置操作
  50. const resetQuery = async () => {
  51. queryParams.tableName = undefined
  52. queryParams.tableComment = undefined
  53. await getList()
  54. }
  55. /** 多选框选中数据 */
  56. const tables = ref<string[]>([])
  57. const handleSelectionChange = (val: DatabaseTableVO[]) => {
  58. tables.value = val.map((item) => item.name)
  59. }
  60. /** 导入按钮操作 */
  61. const handleImportTable = async () => {
  62. if (tables.value.length === 0) {
  63. ElMessage.error('请选择要导入的表')
  64. return
  65. }
  66. await createCodegenListApi({
  67. dataSourceConfigId: queryParams.dataSourceConfigId,
  68. tableNames: tables.value
  69. })
  70. ElMessage.success('导入成功')
  71. visible.value = false
  72. emit('ok')
  73. }
  74. defineExpose({
  75. show
  76. })
  77. </script>
  78. <template>
  79. <!-- 导入表 -->
  80. <Dialog title="导入表" v-model="visible" maxHeight="500px" width="50%">
  81. <el-form :model="queryParams" ref="queryRef" :inline="true">
  82. <el-form-item label="数据源" prop="dataSourceConfigId">
  83. <el-select v-model="queryParams.dataSourceConfigId" placeholder="请选择数据源" clearable>
  84. <el-option
  85. v-for="config in dataSourceConfigs"
  86. :key="config.id"
  87. :label="config.name"
  88. :value="config.id"
  89. />
  90. </el-select>
  91. </el-form-item>
  92. <el-form-item label="表名称" prop="tableName">
  93. <el-input v-model="queryParams.tableName" placeholder="请输入表名称" clearable />
  94. </el-form-item>
  95. <el-form-item label="表描述" prop="tableComment">
  96. <el-input v-model="queryParams.tableComment" placeholder="请输入表描述" clearable />
  97. </el-form-item>
  98. <el-form-item>
  99. <el-button type="primary" @click="handleQuery">
  100. <Icon icon="ep:search" class="mr-5px" />
  101. {{ t('common.query') }}
  102. </el-button>
  103. <el-button @click="resetQuery">
  104. <Icon icon="ep:refresh-right" class="mr-5px" />
  105. {{ t('common.reset') }}
  106. </el-button>
  107. </el-form-item>
  108. </el-form>
  109. <el-table
  110. ref="table"
  111. :data="dbTableList"
  112. @selection-change="handleSelectionChange"
  113. v-loading="dbLoading"
  114. height="400px"
  115. >
  116. <el-table-column type="selection" width="55" />
  117. <el-table-column prop="name" label="表名称" :show-overflow-tooltip="true" />
  118. <el-table-column prop="comment" label="表描述" :show-overflow-tooltip="true" />
  119. </el-table>
  120. <template #footer>
  121. <div class="dialog-footer">
  122. <el-button type="primary" @click="handleImportTable">{{ t('action.import') }}</el-button>
  123. <el-button @click="visible = false">{{ t('dialog.close') }}</el-button>
  124. </div>
  125. </template>
  126. </Dialog>
  127. </template>