addTable.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <template>
  2. <div class="sys-dbTable-container">
  3. <el-dialog v-model="isShowDialog" title="表新增" draggable width="1400px">
  4. <el-divider content-position="left">数据表信息</el-divider>
  5. <el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="80px">
  6. <el-row :gutter="35">
  7. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
  8. <el-form-item label="表名称" prop="tableName" :rules="[{ required: true, message: '名称不能为空', trigger: 'blur' }]">
  9. <el-input v-model="ruleForm.tableName" placeholder="表名称" clearable />
  10. </el-form-item>
  11. </el-col>
  12. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
  13. <el-form-item label="描述" prop="description" :rules="[{ required: true, message: '描述不能为空', trigger: 'blur' }]">
  14. <el-input v-model="ruleForm.description" placeholder="描述" clearable type="textarea" />
  15. </el-form-item>
  16. </el-col>
  17. </el-row>
  18. </el-form>
  19. <el-divider content-position="left">数据列信息</el-divider>
  20. <el-table :data="tableData" style="width: 100%" max-height="400">
  21. <el-table-column fixed prop="dbColumnName" label="字段名" width="150">
  22. <template #default="scope">
  23. <el-input v-model="scope.row.dbColumnName" autocomplete="off" />
  24. </template>
  25. </el-table-column>
  26. <el-table-column prop="columnDescription" label="描述" width="200">
  27. <template #default="scope">
  28. <el-input v-model="scope.row.columnDescription" autocomplete="off" />
  29. </template>
  30. </el-table-column>
  31. <el-table-column prop="isPrimarykey" label="主键">
  32. <template #default="scope">
  33. <el-select v-model="scope.row.isPrimarykey" class="m-2" placeholder="Select">
  34. <el-option v-for="item in isOrNotSelect()" :key="item.value" :label="item.label" :value="item.value" />
  35. </el-select>
  36. </template>
  37. </el-table-column>
  38. <el-table-column prop="isIdentity" label="自增">
  39. <template #default="scope">
  40. <el-select v-model="scope.row.isIdentity" class="m-2" placeholder="Select">
  41. <el-option v-for="item in isOrNotSelect()" :key="item.value" :label="item.label" :value="item.value" />
  42. </el-select>
  43. </template>
  44. </el-table-column>
  45. <el-table-column prop="dataType" label="类型">
  46. <template #default="scope">
  47. <el-select v-model="scope.row.dataType" class="m-2" placeholder="Select">
  48. <el-option v-for="item in apiTypeSelect()" :key="item.value" :label="item.value" :value="item.value" />
  49. </el-select>
  50. </template>
  51. </el-table-column>
  52. <el-table-column prop="isNullable" label="可空">
  53. <template #default="scope">
  54. <el-select v-model="scope.row.isNullable" class="m-2" placeholder="Select">
  55. <el-option v-for="item in isOrNotSelect()" :key="item.value" :label="item.label" :value="item.value" />
  56. </el-select>
  57. </template>
  58. </el-table-column>
  59. <el-table-column prop="length" label="长度">
  60. <template #default="scope">
  61. <el-input-number v-model="scope.row.length" size="small" />
  62. </template>
  63. </el-table-column>
  64. <el-table-column prop="decimalDigits" label="保留几位小数">
  65. <template #default="scope">
  66. <el-input-number v-model="scope.row.decimalDigits" size="small" />
  67. </template>
  68. </el-table-column>
  69. <el-table-column fixed="right" label="操作" width="220">
  70. <template #default="scope">
  71. <el-button link type="primary" icon="el-icon-delete" size="small" @click.prevent="handleColDelete(scope.$index)">删除</el-button>
  72. <el-button v-if="tableData.length > 1" link type="primary" icon="ele-Top" size="small" @click.prevent="handleColTop(scope.row, scope.$index)">上移</el-button>
  73. <el-button v-if="tableData.length > 1" link type="primary" icon="ele-Bottom" size="small" @click.prevent="handleColDown(scope.row, scope.$index)">下移</el-button>
  74. </template>
  75. </el-table-column>
  76. </el-table>
  77. <div style="text-align: left; margin-top: 10px">
  78. <el-button icon="ele-Plus" @click="addPrimaryColumn">新增主键字段</el-button>
  79. <el-button icon="ele-Plus" @click="addColumn">新增普通字段</el-button>
  80. <el-button icon="ele-Plus" @click="addTenantColumn">新增租户字段</el-button>
  81. <el-button icon="ele-Plus" @click="addBaseColumn">新增基础字段</el-button>
  82. </div>
  83. <template #footer>
  84. <span class="dialog-footer">
  85. <el-button @click="cancel" size="default">取 消</el-button>
  86. <el-button type="primary" @click="submit" size="default">确 定</el-button>
  87. </span>
  88. </template>
  89. </el-dialog>
  90. </div>
  91. </template>
  92. <script lang="ts">
  93. import { reactive, toRefs, defineComponent, getCurrentInstance, ref } from 'vue';
  94. import { EditRecordRow, UpdateDbTableInput } from '/@/api-services/models'
  95. import { ElMessage } from 'element-plus';
  96. import { getAPI } from '/@/utils/axios-utils';
  97. import { SysDatabaseApi } from '/@/api-services/api';
  98. export default defineComponent({
  99. name: 'sysAddTable',
  100. components: {},
  101. setup() {
  102. var colIndex = 0;
  103. const { proxy } = getCurrentInstance() as any;
  104. const ruleFormRef = ref();
  105. const state = reactive({
  106. isShowDialog: false,
  107. ruleForm: {} as UpdateDbTableInput,
  108. tableData: [] as Array<EditRecordRow>,
  109. });
  110. // 打开弹窗
  111. const openDialog = (row: any) => {
  112. state.ruleForm = row;
  113. state.isShowDialog = true;
  114. };
  115. // 关闭弹窗
  116. const closeDialog = () => {
  117. proxy.mittBus.emit('addTableSubmitted', state.ruleForm.tableName);
  118. state.tableData = [];
  119. state.isShowDialog = false;
  120. };
  121. // 取消
  122. const cancel = () => {
  123. state.isShowDialog = false;
  124. };
  125. // 提交
  126. const submit = () => {
  127. ruleFormRef.value.validate(async (valid: boolean) => {
  128. if (!valid) return;
  129. if (state.tableData.length === 0) {
  130. ElMessage({
  131. type: 'error',
  132. message: `请选择库名!`,
  133. });
  134. return;
  135. }
  136. const params: any = {
  137. dbColumnInfoList: state.tableData,
  138. ...state.ruleForm,
  139. };
  140. await getAPI(SysDatabaseApi).sysDatabaseAddTablePost(params);
  141. closeDialog();
  142. });
  143. };
  144. const apiTypeSelect = () => {
  145. return [
  146. {
  147. value: 'text',
  148. hasLength: false,
  149. hasDecimalDigits: false,
  150. },
  151. {
  152. value: 'varchar',
  153. hasLength: true,
  154. hasDecimalDigits: false,
  155. },
  156. {
  157. value: 'nvarchar',
  158. hasLength: true,
  159. hasDecimalDigits: false,
  160. },
  161. {
  162. value: 'char',
  163. hasLength: true,
  164. hasDecimalDigits: false,
  165. },
  166. {
  167. value: 'nchar',
  168. hasLength: true,
  169. hasDecimalDigits: false,
  170. },
  171. {
  172. value: 'timestamp',
  173. hasLength: false,
  174. hasDecimalDigits: false,
  175. },
  176. {
  177. value: 'int',
  178. hasLength: false,
  179. hasDecimalDigits: false,
  180. },
  181. {
  182. value: 'smallint',
  183. hasLength: false,
  184. hasDecimalDigits: false,
  185. },
  186. {
  187. value: 'tinyint',
  188. hasLength: false,
  189. hasDecimalDigits: false,
  190. },
  191. {
  192. value: 'bigint',
  193. hasLength: false,
  194. hasDecimalDigits: false,
  195. },
  196. {
  197. value: 'bit',
  198. hasLength: false,
  199. hasDecimalDigits: false,
  200. },
  201. {
  202. value: 'decimal',
  203. hasLength: true,
  204. hasDecimalDigits: true,
  205. },
  206. {
  207. value: 'datetime',
  208. hasLength: false,
  209. hasDecimalDigits: false,
  210. },
  211. ];
  212. };
  213. const isOrNotSelect = () => {
  214. return [
  215. {
  216. label: '是',
  217. value: 1,
  218. },
  219. {
  220. label: '否',
  221. value: 0,
  222. },
  223. ];
  224. };
  225. function addPrimaryColumn() {
  226. const addRow: EditRecordRow = {
  227. columnDescription: '主键Id',
  228. dataType: 'bigint',
  229. dbColumnName: 'Id',
  230. decimalDigits: 0,
  231. isIdentity: 0,
  232. isNullable: 0,
  233. isPrimarykey: 1,
  234. length: 0,
  235. key: colIndex,
  236. editable: true,
  237. isNew: true,
  238. };
  239. state.tableData.push(addRow);
  240. colIndex++;
  241. }
  242. function addColumn() {
  243. const addRow: EditRecordRow = {
  244. columnDescription: '',
  245. dataType: '',
  246. dbColumnName: '',
  247. decimalDigits: 0,
  248. isIdentity: 0,
  249. isNullable: 1,
  250. isPrimarykey: 0,
  251. length: 0,
  252. key: colIndex,
  253. editable: true,
  254. isNew: true,
  255. };
  256. state.tableData.push(addRow);
  257. colIndex++;
  258. }
  259. function addTenantColumn() {
  260. const addRow: EditRecordRow = {
  261. columnDescription: '租户Id',
  262. dataType: 'bigint',
  263. dbColumnName: 'TenantId',
  264. decimalDigits: 0,
  265. isIdentity: 0,
  266. isNullable: 1,
  267. isPrimarykey: 0,
  268. length: 0,
  269. key: colIndex,
  270. editable: true,
  271. isNew: true,
  272. };
  273. state.tableData.push(addRow);
  274. colIndex++;
  275. }
  276. function addBaseColumn() {
  277. const fileds = [
  278. {
  279. dataType: 'datetime',
  280. name: 'CreateTime',
  281. desc: '创建时间',
  282. },
  283. {
  284. dataType: 'datetime',
  285. name: 'UpdateTime',
  286. desc: '更新时间',
  287. },
  288. {
  289. dataType: 'bigint',
  290. name: 'CreateUserId',
  291. desc: '创建者Id',
  292. },
  293. {
  294. dataType: 'bigint',
  295. name: 'UpdateUserId',
  296. desc: '修改者Id',
  297. },
  298. {
  299. dataType: 'bit',
  300. name: 'IsDelete',
  301. desc: '软删除',
  302. isNullable: 0,
  303. },
  304. ];
  305. fileds.forEach((m: any) => {
  306. state.tableData.push({
  307. columnDescription: m.desc,
  308. dataType: m.dataType,
  309. dbColumnName: m.name,
  310. decimalDigits: 0,
  311. isIdentity: 0,
  312. isNullable: m.isNullable === 0 ? 0 : 1,
  313. isPrimarykey: 0,
  314. length: m.length || 0,
  315. key: colIndex,
  316. editable: true,
  317. isNew: true,
  318. });
  319. colIndex++;
  320. });
  321. }
  322. function handleColDelete(index: number) {
  323. state.tableData.splice(index, 1);
  324. }
  325. function handleColTop(record: EditRecordRow, index: number) {
  326. if (record.isNew) {
  327. var data1 = ChangeExForArray(index, index - 1, state.tableData);
  328. return data1;
  329. }
  330. }
  331. function handleColDown(record: EditRecordRow, index: number) {
  332. if (record.isNew) {
  333. return ChangeExForArray(index, index + 1, state.tableData);
  334. }
  335. }
  336. function ChangeExForArray(index1: number, index2: number, array: Array<EditRecordRow>) {
  337. let temp = array[index1];
  338. array[index1] = array[index2];
  339. array[index2] = temp;
  340. return array;
  341. }
  342. return {
  343. ruleFormRef,
  344. openDialog,
  345. closeDialog,
  346. cancel,
  347. submit,
  348. ...toRefs(state),
  349. apiTypeSelect,
  350. addPrimaryColumn,
  351. addColumn,
  352. addTenantColumn,
  353. addBaseColumn,
  354. isOrNotSelect,
  355. handleColTop,
  356. handleColDown,
  357. handleColDelete,
  358. };
  359. },
  360. });
  361. </script>