TableDesig.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.ColumnList"
  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. urlObj: {} // 路径参数
  40. };
  41. },
  42. components: {},
  43. created: function() {
  44. this.getData();
  45. },
  46. mounted: function() {
  47. },
  48. props: ["sys", "schema", "table","tableName"],
  49. computed: {},
  50. methods: {
  51. getData() {
  52. const origin = Location.origin
  53. Axios.get(
  54. `/${this.sys}/--database-doc--/table-info?schema=${
  55. this.schema
  56. }&table=${this.table}`,
  57. {}
  58. ).then(res => {
  59. this.tableData = res.data;
  60. });
  61. },
  62. //生成实体类
  63. genEntrty() {
  64. this.getUrlData();
  65. let strBuild = new SStringBuilder();
  66. const table = this.toHump(this.table);
  67. strBuild.append(
  68. `package cn.sagacloud.server.datacenter.models.entities
  69. import cn.sagacloud.server.datacenter.models.entities.base.BaseInfo
  70. import cn.sagacloud.server.datacenter.models.entities.task.SchemeUtils
  71. import com.alibaba.fastjson.annotation.JSONField
  72. import com.sybotan.service.models.annotations.SCascade
  73. import io.swagger.annotations.ApiModel
  74. import io.swagger.annotations.ApiModelProperty
  75. import javax.persistence.Column
  76. import javax.persistence.Id
  77. import javax.persistence.Table
  78. /**
  79. * ${this.tableName}信息实体类
  80. *
  81. * @author 自动生成
  82. */
  83. @ApiModel(description = "资产信息实体类")
  84. @Table(name = "${this.schema}.${this.table}")
  85. open class ${table} : BaseInfo(),Comparator<${table}> {
  86. `
  87. );
  88. this.tableData.ColumnList.forEach(element => {
  89. strBuild.append(` /** ${element.Description} */`);
  90. if (element.Nullable == "YES") {
  91. strBuild.append(
  92. ` @ApiModelProperty(value = "${element.Description}")`
  93. );
  94. } else {
  95. strBuild.append(
  96. ` @ApiModelProperty(value = "${element.Description}", required = true)`
  97. );
  98. }
  99. strBuild.append(` @Column(name = "${element.ColumnName}")`);
  100. strBuild.append(
  101. ` var ${this.toSmallHump(element.ColumnName)}: ${this.typeToName(
  102. element.ColumnType
  103. )}? = null\n`
  104. );
  105. });
  106. strBuild.append(`} // Class ${table}`);
  107. this.download(`${table}.kt`, strBuild.toString());
  108. },
  109. //生成接口类
  110. genControler() {
  111. this.getUrlData();
  112. let strBuild = new SStringBuilder();
  113. const className = `${this.toHump(this.table)}Controller`;
  114. strBuild.append(`package cn.sagacloud.server.datacenter.controllers
  115. import cn.sagacloud.server.datacenter.models.entities.Property
  116. import cn.sagacloud.server.datacenter.services.PropertyService
  117. import com.sybotan.base.extensions.toJson
  118. import com.sybotan.database.SFilter
  119. import com.sybotan.service.SPageContext
  120. import com.sybotan.service.models.requests.SCountRequest
  121. import com.sybotan.service.models.requests.SCreateRequest
  122. import com.sybotan.service.models.requests.SQueryRequest
  123. import com.sybotan.service.models.requests.SUpdateRequest
  124. import com.sybotan.service.models.responses.SBaseResponse
  125. import com.sybotan.service.models.responses.SCountResponse
  126. import com.sybotan.service.models.responses.SCreateResponse
  127. import com.sybotan.service.models.responses.SQueryResponse
  128. import io.swagger.annotations.Api
  129. import io.swagger.annotations.ApiOperation
  130. import org.slf4j.LoggerFactory
  131. import org.springframework.web.bind.annotation.PostMapping
  132. import org.springframework.web.bind.annotation.RequestBody
  133. import org.springframework.web.bind.annotation.RequestMapping
  134. import org.springframework.web.bind.annotation.RestController
  135. /**
  136. * ${this.tableName}接口
  137. *
  138. * @author 自动生成
  139. */
  140. @Api(tags = ["${this.tableName}"])
  141. @RestController
  142. @RequestMapping("/object/${this.table}")
  143. open class ${className} {
  144. companion object {
  145. /** 日志 */
  146. private val logger = LoggerFactory.getLogger(${className}::class.java)
  147. } // Companion object
  148. /**
  149. * 创建${this.tableName}
  150. *
  151. * @param request ${this.tableName}对象列表
  152. * @return 创建结果信息
  153. */
  154. @ApiOperation(value = "创建${this.tableName}信息", notes = "")
  155. @PostMapping(value = ["/create"])
  156. fun create(@RequestBody request: SCreateRequest<${this.toHump(
  157. this.table
  158. )}>): SCreateResponse<${this.toHump(this.table)}> {
  159. return PropertyService.createList( request)
  160. } // Function create()
  161. /**
  162. * 根据id删除${this.tableName}
  163. *
  164. * @param idList id数组
  165. * @return 删除的结果信息
  166. */
  167. @ApiOperation(value = "根据id删除${this.tableName}信息", notes = "")
  168. @PostMapping(value = ["/delete"])
  169. fun delete(@RequestBody idList: ArrayList<${this.toHump(
  170. this.table
  171. )}>): SBaseResponse {
  172. return PropertyService.deleteByKeysList(idList)
  173. } // Function delete()
  174. /**
  175. * 更新${this.tableName}信息
  176. *
  177. * @param request 更新的内容对象
  178. * @return 更新的结果
  179. */
  180. @ApiOperation(value = "更新${this.tableName}信息", notes = "")
  181. @PostMapping(value = ["/update"])
  182. fun update(@RequestBody request: SUpdateRequest<${this.toHump(
  183. this.table
  184. )}>): SBaseResponse {
  185. return PropertyService.updateList(request)
  186. } // Function update()
  187. /**
  188. * 查询${this.tableName}信息
  189. *
  190. * @param request 查询信息条件
  191. * @return 查询结果
  192. */
  193. @ApiOperation(value = "查询${this.tableName}信息", notes="")
  194. @PostMapping(value = ["/query"])
  195. fun query(@RequestBody request: SQueryRequest): SQueryResponse<${this.toHump(
  196. this.table
  197. )}> {
  198. return PropertyService.pageQuery(request)
  199. } // Function query()
  200. /**
  201. * 根据条件查询统计数量
  202. */
  203. @ApiOperation(value = "根据条件查询统计数量", notes = "")
  204. @PostMapping(value = ["/count"])
  205. fun count(@RequestBody request: SCountRequest): SCountResponse {
  206. return PropertyService.count(request)
  207. } // Function count()
  208. `);
  209. strBuild.append(`} // Class ${className}`);
  210. this.download(`${className}.kt`, strBuild.toString());
  211. },
  212. // 数据包类型到实体类的转换
  213. typeToName(name) {
  214. const obj = {
  215. varchar: "String",
  216. char: "String",
  217. timestamptz: "Date",
  218. timestamp: "Date",
  219. text: "String",
  220. bool: "Boolean",
  221. int4: "Int",
  222. date: "Date",
  223. int2: "Int",
  224. json: "HashMap<String, Any?>",
  225. jsonb: "HashMap<String, Any?>"
  226. };
  227. if (name) {
  228. return obj[name] ? obj[name] : "String";
  229. } else {
  230. return "String";
  231. }
  232. },
  233. //转大驼峰
  234. toHump(str) {
  235. var arr = str.split("_");
  236. for (var i = 0; i < arr.length; i++) {
  237. arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
  238. }
  239. return arr.join("");
  240. },
  241. //转小驼峰
  242. toSmallHump(str) {
  243. var arr = str.split("_");
  244. for (var i = 1; i < arr.length; i++) {
  245. arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
  246. }
  247. return arr.join("");
  248. },
  249. // 获取路径名称以及路径type
  250. getUrlData() {
  251. const dataArr = Location.href.split("#")[1].split("-");
  252. let obj = {};
  253. if (dataArr.length == 2) {
  254. obj = {
  255. urlType: dataArr[1],
  256. urlName: decodeURIComponent(dataArr[0])
  257. };
  258. } else {
  259. let urlName = "";
  260. let lengthNum = dataArr.length;
  261. dataArr.forEach((item, i) => {
  262. if (i + 2 <= lengthNum) {
  263. urlName = urlName + "-" + item;
  264. }
  265. });
  266. obj = {
  267. urlType: dataArr[lengthNum-1],
  268. urlName: decodeURIComponent(urlName)
  269. };
  270. }
  271. this.urlObj = obj;
  272. },
  273. // 下载文件
  274. download(filename, text) {
  275. var element = document.createElement("a");
  276. element.setAttribute(
  277. "href",
  278. "data:text/plain;charset=utf-8," + encodeURIComponent(text)
  279. );
  280. element.setAttribute("download", filename);
  281. element.style.display = "none";
  282. document.body.appendChild(element);
  283. element.click();
  284. document.body.removeChild(element);
  285. }
  286. }
  287. };
  288. </script>
  289. <style scoped>
  290. /deep/ table {
  291. margin: 0;
  292. }
  293. </style>