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.

202 lines
6.3 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/getSysHistory [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/rollback [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 request.AutoHistoryByID true "获取meta信息"
  63. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  64. // @Router /autoCode/getMeta [post]
  65. func GetMeta(c *gin.Context) {
  66. var id request.AutoHistoryByID
  67. _ = c.ShouldBindJSON(&id)
  68. if v, err := service.GetMeta(id.ID); err != nil {
  69. response.FailWithMessage(err.Error(), c)
  70. return
  71. } else {
  72. response.OkWithDetailed(gin.H{"meta": v}, "获取成功", c)
  73. }
  74. }
  75. // @Tags AutoCode
  76. // @Summary 预览创建后的代码
  77. // @Security ApiKeyAuth
  78. // @accept application/json
  79. // @Produce application/json
  80. // @Param data body model.AutoCodeStruct true "预览创建代码"
  81. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  82. // @Router /autoCode/preview [post]
  83. func PreviewTemp(c *gin.Context) {
  84. var a model.AutoCodeStruct
  85. _ = c.ShouldBindJSON(&a)
  86. if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
  87. response.FailWithMessage(err.Error(), c)
  88. return
  89. }
  90. autoCode, err := service.PreviewTemp(a)
  91. if err != nil {
  92. global.GVA_LOG.Error("预览失败!", zap.Any("err", err))
  93. response.FailWithMessage("预览失败", c)
  94. } else {
  95. response.OkWithDetailed(gin.H{"autoCode": autoCode}, "预览成功", c)
  96. }
  97. }
  98. // @Tags AutoCode
  99. // @Summary 自动代码模板
  100. // @Security ApiKeyAuth
  101. // @accept application/json
  102. // @Produce application/json
  103. // @Param data body model.AutoCodeStruct true "创建自动代码"
  104. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  105. // @Router /autoCode/createTemp [post]
  106. func CreateTemp(c *gin.Context) {
  107. var a model.AutoCodeStruct
  108. _ = c.ShouldBindJSON(&a)
  109. if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
  110. response.FailWithMessage(err.Error(), c)
  111. return
  112. }
  113. var apiIds []uint
  114. if a.AutoCreateApiToSql {
  115. if ids, err := service.AutoCreateApi(&a); err != nil {
  116. global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
  117. c.Writer.Header().Add("success", "false")
  118. c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
  119. return
  120. } else {
  121. apiIds = ids
  122. }
  123. }
  124. err := service.CreateTemp(a, apiIds...)
  125. if err != nil {
  126. if errors.Is(err, model.AutoMoveErr) {
  127. c.Writer.Header().Add("success", "false")
  128. c.Writer.Header().Add("msgtype", "success")
  129. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  130. } else {
  131. c.Writer.Header().Add("success", "false")
  132. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  133. _ = os.Remove("./ginvueadmin.zip")
  134. }
  135. } else {
  136. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
  137. c.Writer.Header().Add("Content-Type", "application/json")
  138. c.Writer.Header().Add("success", "true")
  139. c.File("./ginvueadmin.zip")
  140. _ = os.Remove("./ginvueadmin.zip")
  141. }
  142. }
  143. // @Tags AutoCode
  144. // @Summary 获取当前数据库所有表
  145. // @Security ApiKeyAuth
  146. // @accept application/json
  147. // @Produce application/json
  148. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  149. // @Router /autoCode/getTables [get]
  150. func GetTables(c *gin.Context) {
  151. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  152. err, tables := service.GetTables(dbName)
  153. if err != nil {
  154. global.GVA_LOG.Error("查询table失败!", zap.Any("err", err))
  155. response.FailWithMessage("查询table失败", c)
  156. } else {
  157. response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
  158. }
  159. }
  160. // @Tags AutoCode
  161. // @Summary 获取当前所有数据库
  162. // @Security ApiKeyAuth
  163. // @accept application/json
  164. // @Produce application/json
  165. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  166. // @Router /autoCode/getDatabase [get]
  167. func GetDB(c *gin.Context) {
  168. if err, dbs := service.GetDB(); err != nil {
  169. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  170. response.FailWithMessage("获取失败", c)
  171. } else {
  172. response.OkWithDetailed(gin.H{"dbs": dbs}, "获取成功", c)
  173. }
  174. }
  175. // @Tags AutoCode
  176. // @Summary 获取当前表所有字段
  177. // @Security ApiKeyAuth
  178. // @accept application/json
  179. // @Produce application/json
  180. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  181. // @Router /autoCode/getColumn [get]
  182. func GetColumn(c *gin.Context) {
  183. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  184. tableName := c.Query("tableName")
  185. if err, columns := service.GetColumn(tableName, dbName); err != nil {
  186. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  187. response.FailWithMessage("获取失败", c)
  188. } else {
  189. response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
  190. }
  191. }