114 lines
3.4 KiB
Go
114 lines
3.4 KiB
Go
package system
|
||
|
||
import (
|
||
"kra/pkg/response"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// JSONMap 通用JSON Map类型
|
||
type JSONMap map[string]interface{}
|
||
|
||
type AutoCodeApi struct{}
|
||
|
||
// GetDB 获取当前所有数据库
|
||
// @Summary 获取当前所有数据库
|
||
// @Tags AutoCode
|
||
// @Security ApiKeyAuth
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param businessDB query string false "业务数据库别名"
|
||
// @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前所有数据库"
|
||
// @Router /autoCode/getDB [get]
|
||
func (api *AutoCodeApi) GetDB(c *gin.Context) {
|
||
businessDB := c.Query("businessDB")
|
||
// 获取数据库列表
|
||
dbs, err := autoCodeUsecase.GetDB(c.Request.Context(), businessDB)
|
||
if err != nil {
|
||
response.FailWithMessage("获取失败", c)
|
||
return
|
||
}
|
||
// 获取配置的数据库列表
|
||
dbList := autoCodeUsecase.GetDBList()
|
||
response.OkWithDetailed(gin.H{"dbs": dbs, "dbList": dbList}, "获取成功", c)
|
||
}
|
||
|
||
// GetTables 获取当前数据库所有表
|
||
// @Summary 获取当前数据库所有表
|
||
// @Tags AutoCode
|
||
// @Security ApiKeyAuth
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param dbName query string false "数据库名"
|
||
// @Param businessDB query string false "业务数据库别名"
|
||
// @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前数据库所有表"
|
||
// @Router /autoCode/getTables [get]
|
||
func (api *AutoCodeApi) GetTables(c *gin.Context) {
|
||
dbName := c.Query("dbName")
|
||
businessDB := c.Query("businessDB")
|
||
|
||
// 如果dbName为空,使用默认数据库名
|
||
if dbName == "" {
|
||
dbName = autoCodeUsecase.GetActiveDBName(businessDB)
|
||
}
|
||
|
||
tables, err := autoCodeUsecase.GetTables(c.Request.Context(), businessDB, dbName)
|
||
if err != nil {
|
||
response.FailWithMessage("查询table失败", c)
|
||
return
|
||
}
|
||
response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
|
||
}
|
||
|
||
// GetColumn 获取当前表所有字段
|
||
// @Summary 获取当前表所有字段
|
||
// @Tags AutoCode
|
||
// @Security ApiKeyAuth
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param dbName query string false "数据库名"
|
||
// @Param tableName query string true "表名"
|
||
// @Param businessDB query string false "业务数据库别名"
|
||
// @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前表所有字段"
|
||
// @Router /autoCode/getColumn [get]
|
||
func (api *AutoCodeApi) GetColumn(c *gin.Context) {
|
||
businessDB := c.Query("businessDB")
|
||
dbName := c.Query("dbName")
|
||
tableName := c.Query("tableName")
|
||
|
||
// 如果dbName为空,使用默认数据库名
|
||
if dbName == "" {
|
||
dbName = autoCodeUsecase.GetActiveDBName(businessDB)
|
||
}
|
||
|
||
columns, err := autoCodeUsecase.GetColumn(c.Request.Context(), businessDB, tableName, dbName)
|
||
if err != nil {
|
||
response.FailWithMessage("获取失败", c)
|
||
return
|
||
}
|
||
response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
|
||
}
|
||
|
||
// LLMAuto 大模型自动生成代码
|
||
// @Summary 大模型自动生成代码
|
||
// @Tags AutoCode
|
||
// @Security ApiKeyAuth
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param data body JSONMap true "LLM参数"
|
||
// @Success 200 {object} response.Response{data=interface{}}
|
||
// @Router /autoCode/llmAuto [post]
|
||
func (api *AutoCodeApi) LLMAuto(c *gin.Context) {
|
||
var llm JSONMap
|
||
if err := c.ShouldBindJSON(&llm); err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
data, err := autoCodeUsecase.LLMAuto(c.Request.Context(), llm)
|
||
if err != nil {
|
||
response.FailWithMessage("大模型生成失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithData(data, c)
|
||
}
|