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.

222 lines
6.9 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.AutoHistoryByID true "删除回滚记录"
  22. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  23. // @Router /autoCode/delSysHistory [post]
  24. func DelSysHistory(c *gin.Context) {
  25. var id request.AutoHistoryByID
  26. _ = c.ShouldBindJSON(&id)
  27. err := service.DeletePage(id.ID)
  28. if err != nil {
  29. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  30. response.FailWithMessage("获取失败", c)
  31. }
  32. response.OkWithMessage("删除成功", c)
  33. }
  34. // @Tags AutoCode
  35. // @Summary 查询回滚记录
  36. // @Security ApiKeyAuth
  37. // @accept application/json
  38. // @Produce application/json
  39. // @Param data body request.SysAutoHistory true "查询回滚记录"
  40. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  41. // @Router /autoCode/getSysHistory [post]
  42. func GetSysHistory(c *gin.Context) {
  43. var search request.SysAutoHistory
  44. _ = c.ShouldBindJSON(&search)
  45. err, list, total := service.GetSysHistoryPage(search.PageInfo)
  46. if err != nil {
  47. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  48. response.FailWithMessage("获取失败", c)
  49. } else {
  50. response.OkWithDetailed(response.PageResult{
  51. List: list,
  52. Total: total,
  53. Page: search.Page,
  54. PageSize: search.PageSize,
  55. }, "获取成功", c)
  56. }
  57. }
  58. // @Tags AutoCode
  59. // @Summary 回滚
  60. // @Security ApiKeyAuth
  61. // @accept application/json
  62. // @Produce application/json
  63. // @Param data body request.AutoHistoryByID true "回滚自动生成代码"
  64. // @Success 200 {string} string "{"success":true,"data":{},"msg":"回滚成功"}"
  65. // @Router /autoCode/rollback [post]
  66. func RollBack(c *gin.Context) {
  67. var id request.AutoHistoryByID
  68. _ = c.ShouldBindJSON(&id)
  69. if err := service.RollBack(id.ID); err != nil {
  70. response.FailWithMessage(err.Error(), c)
  71. return
  72. }
  73. response.OkWithMessage("回滚成功", c)
  74. }
  75. // @Tags AutoCode
  76. // @Summary 回滚
  77. // @Security ApiKeyAuth
  78. // @accept application/json
  79. // @Produce application/json
  80. // @Param data body request.AutoHistoryByID true "获取meta信息"
  81. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  82. // @Router /autoCode/getMeta [post]
  83. func GetMeta(c *gin.Context) {
  84. var id request.AutoHistoryByID
  85. _ = c.ShouldBindJSON(&id)
  86. if v, err := service.GetMeta(id.ID); err != nil {
  87. response.FailWithMessage(err.Error(), c)
  88. return
  89. } else {
  90. response.OkWithDetailed(gin.H{"meta": v}, "获取成功", c)
  91. }
  92. }
  93. // @Tags AutoCode
  94. // @Summary 预览创建后的代码
  95. // @Security ApiKeyAuth
  96. // @accept application/json
  97. // @Produce application/json
  98. // @Param data body model.AutoCodeStruct true "预览创建代码"
  99. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  100. // @Router /autoCode/preview [post]
  101. func PreviewTemp(c *gin.Context) {
  102. var a model.AutoCodeStruct
  103. _ = c.ShouldBindJSON(&a)
  104. if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
  105. response.FailWithMessage(err.Error(), c)
  106. return
  107. }
  108. autoCode, err := service.PreviewTemp(a)
  109. if err != nil {
  110. global.GVA_LOG.Error("预览失败!", zap.Any("err", err))
  111. response.FailWithMessage("预览失败", c)
  112. } else {
  113. response.OkWithDetailed(gin.H{"autoCode": autoCode}, "预览成功", c)
  114. }
  115. }
  116. // @Tags AutoCode
  117. // @Summary 自动代码模板
  118. // @Security ApiKeyAuth
  119. // @accept application/json
  120. // @Produce application/json
  121. // @Param data body model.AutoCodeStruct true "创建自动代码"
  122. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  123. // @Router /autoCode/createTemp [post]
  124. func CreateTemp(c *gin.Context) {
  125. var a model.AutoCodeStruct
  126. _ = c.ShouldBindJSON(&a)
  127. if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
  128. response.FailWithMessage(err.Error(), c)
  129. return
  130. }
  131. var apiIds []uint
  132. if a.AutoCreateApiToSql {
  133. if ids, err := service.AutoCreateApi(&a); err != nil {
  134. global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
  135. c.Writer.Header().Add("success", "false")
  136. c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
  137. return
  138. } else {
  139. apiIds = ids
  140. }
  141. }
  142. err := service.CreateTemp(a, apiIds...)
  143. if err != nil {
  144. if errors.Is(err, model.AutoMoveErr) {
  145. c.Writer.Header().Add("success", "false")
  146. c.Writer.Header().Add("msgtype", "success")
  147. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  148. } else {
  149. c.Writer.Header().Add("success", "false")
  150. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  151. _ = os.Remove("./ginvueadmin.zip")
  152. }
  153. } else {
  154. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
  155. c.Writer.Header().Add("Content-Type", "application/json")
  156. c.Writer.Header().Add("success", "true")
  157. c.File("./ginvueadmin.zip")
  158. _ = os.Remove("./ginvueadmin.zip")
  159. }
  160. }
  161. // @Tags AutoCode
  162. // @Summary 获取当前数据库所有表
  163. // @Security ApiKeyAuth
  164. // @accept application/json
  165. // @Produce application/json
  166. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  167. // @Router /autoCode/getTables [get]
  168. func GetTables(c *gin.Context) {
  169. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  170. err, tables := service.GetTables(dbName)
  171. if err != nil {
  172. global.GVA_LOG.Error("查询table失败!", zap.Any("err", err))
  173. response.FailWithMessage("查询table失败", c)
  174. } else {
  175. response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
  176. }
  177. }
  178. // @Tags AutoCode
  179. // @Summary 获取当前所有数据库
  180. // @Security ApiKeyAuth
  181. // @accept application/json
  182. // @Produce application/json
  183. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  184. // @Router /autoCode/getDatabase [get]
  185. func GetDB(c *gin.Context) {
  186. if err, dbs := service.GetDB(); err != nil {
  187. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  188. response.FailWithMessage("获取失败", c)
  189. } else {
  190. response.OkWithDetailed(gin.H{"dbs": dbs}, "获取成功", c)
  191. }
  192. }
  193. // @Tags AutoCode
  194. // @Summary 获取当前表所有字段
  195. // @Security ApiKeyAuth
  196. // @accept application/json
  197. // @Produce application/json
  198. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  199. // @Router /autoCode/getColumn [get]
  200. func GetColumn(c *gin.Context) {
  201. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  202. tableName := c.Query("tableName")
  203. if err, columns := service.GetColumn(tableName, dbName); err != nil {
  204. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  205. response.FailWithMessage("获取失败", c)
  206. } else {
  207. response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
  208. }
  209. }