kra/internal/biz/system/auto_code.go

90 lines
2.6 KiB
Go

package system
import (
"context"
"github.com/go-kratos/kratos/v2/log"
)
// Db 数据库信息
type Db struct {
Database string `json:"database" gorm:"column:database"`
}
// Table 表信息
type Table struct {
TableName string `json:"tableName" gorm:"column:table_name"`
}
// Column 列信息
type Column struct {
ColumnName string `json:"columnName" gorm:"column:column_name"`
DataType string `json:"dataType" gorm:"column:data_type"`
DataTypeLong string `json:"dataTypeLong" gorm:"column:data_type_long"`
ColumnComment string `json:"columnComment" gorm:"column:column_comment"`
PrimaryKey int `json:"primaryKey" gorm:"column:primary_key"`
}
// DBInfo 数据库配置信息
type DBInfo struct {
AliasName string `json:"aliasName"`
DBName string `json:"dbName"`
Disable bool `json:"disable"`
DBType string `json:"dbtype"`
}
// AutoCodeRepo 自动代码仓储接口
type AutoCodeRepo interface {
GetDB(ctx context.Context, businessDB string) ([]Db, error)
GetTables(ctx context.Context, businessDB, dbName string) ([]Table, error)
GetColumn(ctx context.Context, businessDB, tableName, dbName string) ([]Column, error)
GetDBList() []DBInfo
GetActiveDBName(businessDB string) string
}
// AutoCodeUsecase 自动代码用例
type AutoCodeUsecase struct {
repo AutoCodeRepo
llm *AutoCodeLLMUsecase
log *log.Helper
}
// NewAutoCodeUsecase 创建自动代码用例
func NewAutoCodeUsecase(repo AutoCodeRepo, llm *AutoCodeLLMUsecase, logger log.Logger) *AutoCodeUsecase {
return &AutoCodeUsecase{
repo: repo,
llm: llm,
log: log.NewHelper(logger),
}
}
// GetDB 获取所有数据库
func (uc *AutoCodeUsecase) GetDB(ctx context.Context, businessDB string) ([]Db, error) {
return uc.repo.GetDB(ctx, businessDB)
}
// GetTables 获取指定数据库的所有表
func (uc *AutoCodeUsecase) GetTables(ctx context.Context, businessDB, dbName string) ([]Table, error) {
return uc.repo.GetTables(ctx, businessDB, dbName)
}
// GetColumn 获取指定表的所有列
func (uc *AutoCodeUsecase) GetColumn(ctx context.Context, businessDB, tableName, dbName string) ([]Column, error) {
return uc.repo.GetColumn(ctx, businessDB, tableName, dbName)
}
// GetDBList 获取配置的数据库列表
func (uc *AutoCodeUsecase) GetDBList() []DBInfo {
return uc.repo.GetDBList()
}
// GetActiveDBName 获取当前活动的数据库名
func (uc *AutoCodeUsecase) GetActiveDBName(businessDB string) string {
return uc.repo.GetActiveDBName(businessDB)
}
// LLMAuto 大模型自动生成代码
func (uc *AutoCodeUsecase) LLMAuto(ctx context.Context, llm map[string]interface{}) (interface{}, error) {
return uc.llm.LLMAuto(ctx, llm)
}