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.4 KiB

3 years ago
3 years ago
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. "net/url"
  6. "os"
  7. "github.com/flipped-aurora/gin-vue-admin/server/global"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  9. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  10. systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  11. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  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 system.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 system.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", "true")
  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 (autoApi *AutoCodeApi) GetTables(c *gin.Context) {
  169. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  170. err, tables := autoCodeService.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 (autoApi *AutoCodeApi) GetDB(c *gin.Context) {
  186. if err, dbs := autoCodeService.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 (autoApi *AutoCodeApi) GetColumn(c *gin.Context) {
  201. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  202. tableName := c.Query("tableName")
  203. if err, columns := autoCodeService.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. }