TableDesig.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.${this.toHump(this.table)}Service
  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. val service = SBaseService(SMybatisDao(${this.toHump(this.table)}::class.java))
  150. /**
  151. * 创建${this.tableName}
  152. *
  153. * @param request ${this.tableName}对象列表
  154. * @return 创建结果信息
  155. */
  156. @ApiOperation(value = "创建${this.tableName}信息", notes = "")
  157. @PostMapping(value = ["/create"])
  158. fun create(@RequestBody request: SCreateRequest<${this.toHump(
  159. this.table
  160. )}>): SCreateResponse<${this.toHump(this.table)}> {
  161. return service.createList( request)
  162. } // Function create()
  163. /**
  164. * 根据id删除${this.tableName}
  165. *
  166. * @param idList id数组
  167. * @return 删除的结果信息
  168. */
  169. @ApiOperation(value = "根据id删除${this.tableName}信息", notes = "")
  170. @PostMapping(value = ["/delete"])
  171. fun delete(@RequestBody idList: ArrayList<${this.toHump(
  172. this.table
  173. )}>): SBaseResponse {
  174. return service.deleteByKeysList(idList)
  175. } // Function delete()
  176. /**
  177. * 更新${this.tableName}信息
  178. *
  179. * @param request 更新的内容对象
  180. * @return 更新的结果
  181. */
  182. @ApiOperation(value = "更新${this.tableName}信息", notes = "")
  183. @PostMapping(value = ["/update"])
  184. fun update(@RequestBody request: SUpdateRequest<${this.toHump(
  185. this.table
  186. )}>): SBaseResponse {
  187. return service.updateList(request)
  188. } // Function update()
  189. /**
  190. * 查询${this.tableName}信息
  191. *
  192. * @param request 查询信息条件
  193. * @return 查询结果
  194. */
  195. @ApiOperation(value = "查询${this.tableName}信息", notes="")
  196. @PostMapping(value = ["/query"])
  197. fun query(@RequestBody request: SQueryRequest): SQueryResponse<${this.toHump(
  198. this.table
  199. )}> {
  200. return service.pageQuery(request)
  201. } // Function query()
  202. /**
  203. * 根据条件查询统计数量
  204. */
  205. @ApiOperation(value = "根据条件查询统计数量", notes = "")
  206. @PostMapping(value = ["/count"])
  207. fun count(@RequestBody request: SCountRequest): SCountResponse {
  208. return servicee.count(request)
  209. } // Function count()
  210. `);
  211. strBuild.append(`} // Class ${className}`);
  212. this.download(`${className}.kt`, strBuild.toString());
  213. },
  214. // 数据包类型到实体类的转换
  215. typeToName(name) {
  216. const obj = {
  217. varchar: "String",
  218. char: "String",
  219. timestamptz: "Date",
  220. timestamp: "Date",
  221. text: "String",
  222. bool: "Boolean",
  223. int4: "Int",
  224. date: "Date",
  225. int2: "Int",
  226. json: "HashMap<String, Any?>",
  227. jsonb: "HashMap<String, Any?>"
  228. };
  229. if (name) {
  230. return obj[name] ? obj[name] : "String";
  231. } else {
  232. return "String";
  233. }
  234. },
  235. //转大驼峰
  236. toHump(str) {
  237. var arr = str.split("_");
  238. for (var i = 0; i < arr.length; i++) {
  239. arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
  240. }
  241. return arr.join("");
  242. },
  243. //转小驼峰
  244. toSmallHump(str) {
  245. var arr = str.split("_");
  246. for (var i = 1; i < arr.length; i++) {
  247. arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
  248. }
  249. return arr.join("");
  250. },
  251. // 获取路径名称以及路径type
  252. getUrlData() {
  253. const dataArr = Location.href.split("#")[1].split("-");
  254. let obj = {};
  255. if (dataArr.length == 2) {
  256. obj = {
  257. urlType: dataArr[1],
  258. urlName: decodeURIComponent(dataArr[0])
  259. };
  260. } else {
  261. let urlName = "";
  262. let lengthNum = dataArr.length;
  263. dataArr.forEach((item, i) => {
  264. if (i + 2 <= lengthNum) {
  265. urlName = urlName + "-" + item;
  266. }
  267. });
  268. obj = {
  269. urlType: dataArr[lengthNum-1],
  270. urlName: decodeURIComponent(urlName)
  271. };
  272. }
  273. this.urlObj = obj;
  274. },
  275. // 下载文件
  276. download(filename, text) {
  277. var element = document.createElement("a");
  278. element.setAttribute(
  279. "href",
  280. "data:text/plain;charset=utf-8," + encodeURIComponent(text)
  281. );
  282. element.setAttribute("download", filename);
  283. element.style.display = "none";
  284. document.body.appendChild(element);
  285. element.click();
  286. document.body.removeChild(element);
  287. }
  288. }
  289. };
  290. </script>
  291. <style scoped>
  292. /deep/ table {
  293. margin: 0;
  294. }
  295. </style>