You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.8 KiB

3 years ago
  1. package v1
  2. import (
  3. "errors"
  4. "fmt"
  5. "gin-vue-admin/global"
  6. "gin-vue-admin/model"
  7. "gin-vue-admin/model/request"
  8. "gin-vue-admin/model/response"
  9. "gin-vue-admin/service"
  10. "gin-vue-admin/utils"
  11. "net/url"
  12. "os"
  13. "github.com/gin-gonic/gin"
  14. "go.uber.org/zap"
  15. )
  16. // @Tags AutoCode
  17. // @Summary 查询回滚记录
  18. // @Security ApiKeyAuth
  19. // @accept application/json
  20. // @Produce application/json
  21. // @Param data body request.SysAutoHistory true "查询回滚记录"
  22. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  23. // @Router /autoCode/preview [post]
  24. func GetSysHistory(c *gin.Context) {
  25. var search request.SysAutoHistory
  26. _ = c.ShouldBindJSON(&search)
  27. err, list, total := service.GetSysHistoryPage(search.PageInfo)
  28. if err != nil {
  29. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  30. response.FailWithMessage("获取失败", c)
  31. } else {
  32. response.OkWithDetailed(response.PageResult{
  33. List: list,
  34. Total: total,
  35. Page: search.Page,
  36. PageSize: search.PageSize,
  37. }, "获取成功", c)
  38. }
  39. }
  40. // @Tags AutoCode
  41. // @Summary 回滚
  42. // @Security ApiKeyAuth
  43. // @accept application/json
  44. // @Produce application/json
  45. // @Param data body request.AutoHistoryByID true "回滚自动生成代码"
  46. // @Success 200 {string} string "{"success":true,"data":{},"msg":"回滚成功"}"
  47. // @Router /autoCode/preview [post]
  48. func RollBack(c *gin.Context) {
  49. var id request.AutoHistoryByID
  50. _ = c.ShouldBindJSON(&id)
  51. if err := service.RollBack(id.ID); err != nil {
  52. response.FailWithMessage(err.Error(), c)
  53. return
  54. }
  55. response.OkWithMessage("回滚成功", c)
  56. }
  57. // @Tags AutoCode
  58. // @Summary 预览创建后的代码
  59. // @Security ApiKeyAuth
  60. // @accept application/json
  61. // @Produce application/json
  62. // @Param data body model.AutoCodeStruct true "预览创建代码"
  63. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  64. // @Router /autoCode/preview [post]
  65. func PreviewTemp(c *gin.Context) {
  66. var a model.AutoCodeStruct
  67. _ = c.ShouldBindJSON(&a)
  68. if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
  69. response.FailWithMessage(err.Error(), c)
  70. return
  71. }
  72. autoCode, err := service.PreviewTemp(a)
  73. if err != nil {
  74. global.GVA_LOG.Error("预览失败!", zap.Any("err", err))
  75. response.FailWithMessage("预览失败", c)
  76. } else {
  77. response.OkWithDetailed(gin.H{"autoCode": autoCode}, "预览成功", c)
  78. }
  79. }
  80. // @Tags AutoCode
  81. // @Summary 自动代码模板
  82. // @Security ApiKeyAuth
  83. // @accept application/json
  84. // @Produce application/json
  85. // @Param data body model.AutoCodeStruct true "创建自动代码"
  86. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  87. // @Router /autoCode/createTemp [post]
  88. func CreateTemp(c *gin.Context) {
  89. var a model.AutoCodeStruct
  90. _ = c.ShouldBindJSON(&a)
  91. if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
  92. response.FailWithMessage(err.Error(), c)
  93. return
  94. }
  95. var apiIds []uint
  96. if a.AutoCreateApiToSql {
  97. if ids, err := service.AutoCreateApi(&a); err != nil {
  98. global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
  99. c.Writer.Header().Add("success", "false")
  100. c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
  101. return
  102. } else {
  103. apiIds = ids
  104. }
  105. }
  106. err := service.CreateTemp(a, apiIds...)
  107. if err != nil {
  108. if errors.Is(err, model.AutoMoveErr) {
  109. c.Writer.Header().Add("success", "false")
  110. c.Writer.Header().Add("msgtype", "success")
  111. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  112. } else {
  113. c.Writer.Header().Add("success", "false")
  114. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  115. _ = os.Remove("./ginvueadmin.zip")
  116. }
  117. } else {
  118. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
  119. c.Writer.Header().Add("Content-Type", "application/json")
  120. c.Writer.Header().Add("success", "true")
  121. c.File("./ginvueadmin.zip")
  122. _ = os.Remove("./ginvueadmin.zip")
  123. }
  124. }
  125. // @Tags AutoCode
  126. // @Summary 获取当前数据库所有表
  127. // @Security ApiKeyAuth
  128. // @accept application/json
  129. // @Produce application/json
  130. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  131. // @Router /autoCode/getTables [get]
  132. func GetTables(c *gin.Context) {
  133. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  134. err, tables := service.GetTables(dbName)
  135. if err != nil {
  136. global.GVA_LOG.Error("查询table失败!", zap.Any("err", err))
  137. response.FailWithMessage("查询table失败", c)
  138. } else {
  139. response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
  140. }
  141. }
  142. // @Tags AutoCode
  143. // @Summary 获取当前所有数据库
  144. // @Security ApiKeyAuth
  145. // @accept application/json
  146. // @Produce application/json
  147. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  148. // @Router /autoCode/getDatabase [get]
  149. func GetDB(c *gin.Context) {
  150. if err, dbs := service.GetDB(); err != nil {
  151. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  152. response.FailWithMessage("获取失败", c)
  153. } else {
  154. response.OkWithDetailed(gin.H{"dbs": dbs}, "获取成功", c)
  155. }
  156. }
  157. // @Tags AutoCode
  158. // @Summary 获取当前表所有字段
  159. // @Security ApiKeyAuth
  160. // @accept application/json
  161. // @Produce application/json
  162. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  163. // @Router /autoCode/getColumn [get]
  164. func GetColumn(c *gin.Context) {
  165. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  166. tableName := c.Query("tableName")
  167. if err, columns := service.GetColumn(tableName, dbName); err != nil {
  168. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  169. response.FailWithMessage("获取失败", c)
  170. } else {
  171. response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
  172. }
  173. }