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.

224 lines
7.3 KiB

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