TableDesig.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <div>
  3. <el-button style="margin-top:10px" @click="genEntrty">生成实体类</el-button>
  4. <el-button style="margin-top:10px" @click="genControler">生成接口类</el-button>
  5. <el-table
  6. :data="tableData"
  7. :header-cell-style="headerStyle"
  8. border
  9. stripe
  10. fit
  11. style="width: 100%;margin-top: 25px;"
  12. >
  13. <el-table-column fixed prop="ColumnName" label="字段名" width="150"></el-table-column>
  14. <el-table-column prop="Description" min-width="200" label="说明"></el-table-column>
  15. <el-table-column prop="ColumnType" min-width="120" label="类型"></el-table-column>
  16. <el-table-column prop="CharMaxLength" min-width="60" label="长度"></el-table-column>
  17. <el-table-column prop="Nullable" align="center" label="不是 null"></el-table-column>
  18. <el-table-column prop="Default" min-width="260" label="默认值"></el-table-column>
  19. </el-table>
  20. </div>
  21. </template>
  22. <script>
  23. import Vue from "vue";
  24. import ElementUI from "element-ui";
  25. import Axios from "axios";
  26. import "element-ui/lib/theme-chalk/index.css";
  27. import Opt from "../opt";
  28. import { SStringBuilder } from "@persagy-web/base";
  29. Vue.use(ElementUI);
  30. export default {
  31. data: function() {
  32. return {
  33. tableData: [],
  34. headerStyle: {
  35. backgroundColor: "#e1e4e5",
  36. color: "#2b2b2b",
  37. lineHeight: "30px"
  38. } // 列表样式
  39. };
  40. },
  41. components: {},
  42. created: function() {
  43. this.getData();
  44. },
  45. mounted: function() {
  46. // alert(this.schema);
  47. },
  48. props: ["sys", "schema", "table"],
  49. computed: {},
  50. methods: {
  51. getData() {
  52. Axios.get(
  53. `${Opt.databaseDoc[this.sys]}/--database-doc--/column-list?schema=${
  54. this.schema
  55. }&table=${this.table}`,
  56. {}
  57. ).then(res => {
  58. this.tableData = res.data;
  59. });
  60. },
  61. //生成实体类
  62. genEntrty() {
  63. let strBuild = new SStringBuilder();
  64. strBuild.append(
  65. `package cn.sagacloud.server.datacenter.models.entities
  66. import cn.sagacloud.server.datacenter.models.entities.base.BaseInfo
  67. import cn.sagacloud.server.datacenter.models.entities.task.SchemeUtils
  68. import com.alibaba.fastjson.annotation.JSONField
  69. import com.sybotan.service.models.annotations.SCascade
  70. import io.swagger.annotations.ApiModel
  71. import io.swagger.annotations.ApiModelProperty
  72. import javax.persistence.Column
  73. import javax.persistence.Id
  74. import javax.persistence.Table
  75. /**
  76. * 资产信息实体类
  77. *
  78. * @author 自动生成
  79. */
  80. @ApiModel(description = "资产信息实体类")
  81. @Table(name = "${this.schema}.${this.table}")
  82. open class ${this.toHump(this.table)} : BaseInfo(),Comparator<${this.toHump(this.table)}> {
  83. `
  84. );
  85. this.tableData.forEach(element => {
  86. strBuild.append(` /** ${element.Description} */`);
  87. if (element.Nullable == "YES") {
  88. strBuild.append(
  89. ` @ApiModelProperty(value = "${element.Description}")`
  90. );
  91. } else {
  92. strBuild.append(
  93. ` @ApiModelProperty(value = "${element.Description}", required = true)`
  94. );
  95. }
  96. strBuild.append(` @Column(name = "${element.ColumnName}")`);
  97. strBuild.append(` var ${this.toHump(element.ColumnName)}: ${this.typeToName(element.ColumnType)}? = null\n`);
  98. });
  99. strBuild.append(`} // Class ${this.toHump(this.table)}`);
  100. this.download(`${this.toHump(this.table)}.kt`,strBuild.toString())
  101. },
  102. //生成接口类
  103. genControler() {
  104. this.tableData;
  105. },
  106. // 数据包类型到实体类的转换
  107. typeToName(name) {
  108. const obj = {
  109. varchar: "String",
  110. char: "String",
  111. timestamptz: "Date",
  112. timestamp: "Date",
  113. text: "String",
  114. bool: "Boolean",
  115. int4: "Int",
  116. date: "Date",
  117. int2: "Int",
  118. json: "HashMap<String, Any?>",
  119. jsonb:"HashMap<String, Any?>"
  120. };
  121. if (name) {
  122. return obj[name] ? obj[name] : "String";
  123. } else {
  124. return "String";
  125. }
  126. },
  127. //转驼峰
  128. toHump(name){
  129. return name
  130. },
  131. // 下载文件
  132. download(filename, text) {
  133. var element = document.createElement('a');
  134. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  135. element.setAttribute('download', filename);
  136. element.style.display = 'none';
  137. document.body.appendChild(element);
  138. element.click();
  139. document.body.removeChild(element);
  140. }
  141. }
  142. };
  143. </script>
  144. <style scoped>
  145. /deep/ table {
  146. margin: 0;
  147. }
  148. </style>