sys_auto_code.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package system
  2. import (
  3. "fmt"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/common"
  5. "github.com/goccy/go-json"
  6. "io"
  7. "strings"
  8. "github.com/flipped-aurora/gin-vue-admin/server/global"
  9. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  10. "github.com/flipped-aurora/gin-vue-admin/server/utils/request"
  11. "github.com/gin-gonic/gin"
  12. "go.uber.org/zap"
  13. )
  14. type AutoCodeApi struct{}
  15. // GetDB
  16. // @Tags AutoCode
  17. // @Summary 获取当前所有数据库
  18. // @Security ApiKeyAuth
  19. // @accept application/json
  20. // @Produce application/json
  21. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前所有数据库"
  22. // @Router /autoCode/getDB [get]
  23. func (autoApi *AutoCodeApi) GetDB(c *gin.Context) {
  24. businessDB := c.Query("businessDB")
  25. dbs, err := autoCodeService.Database(businessDB).GetDB(businessDB)
  26. var dbList []map[string]interface{}
  27. for _, db := range global.GVA_CONFIG.DBList {
  28. var item = make(map[string]interface{})
  29. item["aliasName"] = db.AliasName
  30. item["dbName"] = db.Dbname
  31. item["disable"] = db.Disable
  32. item["dbtype"] = db.Type
  33. dbList = append(dbList, item)
  34. }
  35. if err != nil {
  36. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  37. response.FailWithMessage("获取失败", c)
  38. } else {
  39. response.OkWithDetailed(gin.H{"dbs": dbs, "dbList": dbList}, "获取成功", c)
  40. }
  41. }
  42. // GetTables
  43. // @Tags AutoCode
  44. // @Summary 获取当前数据库所有表
  45. // @Security ApiKeyAuth
  46. // @accept application/json
  47. // @Produce application/json
  48. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前数据库所有表"
  49. // @Router /autoCode/getTables [get]
  50. func (autoApi *AutoCodeApi) GetTables(c *gin.Context) {
  51. dbName := c.Query("dbName")
  52. businessDB := c.Query("businessDB")
  53. if dbName == "" {
  54. dbName = *global.GVA_ACTIVE_DBNAME
  55. if businessDB != "" {
  56. for _, db := range global.GVA_CONFIG.DBList {
  57. if db.AliasName == businessDB {
  58. dbName = db.Dbname
  59. }
  60. }
  61. }
  62. }
  63. tables, err := autoCodeService.Database(businessDB).GetTables(businessDB, dbName)
  64. if err != nil {
  65. global.GVA_LOG.Error("查询table失败!", zap.Error(err))
  66. response.FailWithMessage("查询table失败", c)
  67. } else {
  68. response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
  69. }
  70. }
  71. // GetColumn
  72. // @Tags AutoCode
  73. // @Summary 获取当前表所有字段
  74. // @Security ApiKeyAuth
  75. // @accept application/json
  76. // @Produce application/json
  77. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前表所有字段"
  78. // @Router /autoCode/getColumn [get]
  79. func (autoApi *AutoCodeApi) GetColumn(c *gin.Context) {
  80. businessDB := c.Query("businessDB")
  81. dbName := c.Query("dbName")
  82. if dbName == "" {
  83. dbName = *global.GVA_ACTIVE_DBNAME
  84. if businessDB != "" {
  85. for _, db := range global.GVA_CONFIG.DBList {
  86. if db.AliasName == businessDB {
  87. dbName = db.Dbname
  88. }
  89. }
  90. }
  91. }
  92. tableName := c.Query("tableName")
  93. columns, err := autoCodeService.Database(businessDB).GetColumn(businessDB, tableName, dbName)
  94. if err != nil {
  95. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  96. response.FailWithMessage("获取失败", c)
  97. } else {
  98. response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
  99. }
  100. }
  101. func (autoApi *AutoCodeApi) LLMAuto(c *gin.Context) {
  102. var llm common.JSONMap
  103. err := c.ShouldBindJSON(&llm)
  104. if err != nil {
  105. response.FailWithMessage(err.Error(), c)
  106. return
  107. }
  108. if global.GVA_CONFIG.AutoCode.AiPath == "" {
  109. response.FailWithMessage("请先前往插件市场个人中心获取AiPath并填入config.yaml中", c)
  110. return
  111. }
  112. path := strings.ReplaceAll(global.GVA_CONFIG.AutoCode.AiPath, "{FUNC}", fmt.Sprintf("api/chat/%s", llm["mode"]))
  113. res, err := request.HttpRequest(
  114. path,
  115. "POST",
  116. nil,
  117. nil,
  118. llm,
  119. )
  120. if err != nil {
  121. global.GVA_LOG.Error("大模型生成失败!", zap.Error(err))
  122. response.FailWithMessage("大模型生成失败"+err.Error(), c)
  123. return
  124. }
  125. var resStruct response.Response
  126. b, err := io.ReadAll(res.Body)
  127. defer res.Body.Close()
  128. if err != nil {
  129. global.GVA_LOG.Error("大模型生成失败!", zap.Error(err))
  130. response.FailWithMessage("大模型生成失败"+err.Error(), c)
  131. return
  132. }
  133. err = json.Unmarshal(b, &resStruct)
  134. if err != nil {
  135. global.GVA_LOG.Error("大模型生成失败!", zap.Error(err))
  136. response.FailWithMessage("大模型生成失败"+err.Error(), c)
  137. return
  138. }
  139. if resStruct.Code == 7 {
  140. global.GVA_LOG.Error("大模型生成失败!"+resStruct.Msg, zap.Error(err))
  141. response.FailWithMessage("大模型生成失败"+resStruct.Msg, c)
  142. return
  143. }
  144. response.OkWithData(resStruct.Data, c)
  145. }