ImportTable.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. name: undefined,
  25. comment: 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.name = undefined
  52. queryParams.comment = undefined
  53. queryParams.dataSourceConfigId = 0
  54. await getList()
  55. }
  56. /** 多选框选中数据 */
  57. const tables = ref<string[]>([])
  58. const handleSelectionChange = (val: DatabaseTableVO[]) => {
  59. tables.value = val.map((item) => item.name)
  60. }
  61. /** 导入按钮操作 */
  62. const handleImportTable = async () => {
  63. if (tables.value.length === 0) {
  64. ElMessage.error('请选择要导入的表')
  65. return
  66. }
  67. await createCodegenListApi({
  68. dataSourceConfigId: queryParams.dataSourceConfigId,
  69. tableNames: tables.value
  70. })
  71. ElMessage.success('导入成功')
  72. visible.value = false
  73. emit('ok')
  74. }
  75. defineExpose({
  76. show
  77. })
  78. </script>
  79. <template>
  80. <!-- 导入表 -->
  81. <Dialog title="导入表" v-model="visible" maxHeight="500px" width="50%">
  82. <el-form :model="queryParams" ref="queryRef" :inline="true">
  83. <el-form-item label="数据源" prop="dataSourceConfigId">
  84. <el-select v-model="queryParams.dataSourceConfigId" placeholder="请选择数据源" clearable>
  85. <el-option
  86. v-for="config in dataSourceConfigs"
  87. :key="config.id"
  88. :label="config.name"
  89. :value="config.id"
  90. />
  91. </el-select>
  92. </el-form-item>
  93. <el-form-item label="表名称" prop="name">
  94. <el-input v-model="queryParams.name" placeholder="请输入表名称" clearable />
  95. </el-form-item>
  96. <el-form-item label="表描述" prop="comment">
  97. <el-input v-model="queryParams.comment" placeholder="请输入表描述" clearable />
  98. </el-form-item>
  99. <el-form-item>
  100. <el-button type="primary" @click="handleQuery">
  101. <Icon icon="ep:search" class="mr-5px" />
  102. {{ t('common.query') }}
  103. </el-button>
  104. <el-button @click="resetQuery">
  105. <Icon icon="ep:refresh-right" class="mr-5px" />
  106. {{ t('common.reset') }}
  107. </el-button>
  108. </el-form-item>
  109. </el-form>
  110. <el-table
  111. ref="table"
  112. :data="dbTableList"
  113. @selection-change="handleSelectionChange"
  114. v-loading="dbLoading"
  115. height="400px"
  116. >
  117. <el-table-column type="selection" width="55" />
  118. <el-table-column prop="name" label="表名称" :show-overflow-tooltip="true" />
  119. <el-table-column prop="comment" label="表描述" :show-overflow-tooltip="true" />
  120. </el-table>
  121. <template #footer>
  122. <div class="dialog-footer">
  123. <el-button type="primary" @click="handleImportTable">{{ t('action.import') }}</el-button>
  124. <el-button @click="visible = false">{{ t('dialog.close') }}</el-button>
  125. </div>
  126. </template>
  127. </Dialog>
  128. </template>