This commit is contained in:
2025-04-24 18:10:19 +08:00
commit f39069c9d7
258 changed files with 31158 additions and 0 deletions

View File

@ -0,0 +1,70 @@
package tools
import (
"errors"
"github.com/go-admin-team/go-admin-core/sdk/config"
"github.com/go-admin-team/go-admin-core/sdk/pkg"
"gorm.io/gorm"
)
type DBColumns struct {
TableSchema string `gorm:"column:TABLE_SCHEMA" json:"tableSchema"`
TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
ColumnName string `gorm:"column:COLUMN_NAME" json:"columnName"`
ColumnDefault string `gorm:"column:COLUMN_DEFAULT" json:"columnDefault"`
IsNullable string `gorm:"column:IS_NULLABLE" json:"isNullable"`
DataType string `gorm:"column:DATA_TYPE" json:"dataType"`
CharacterMaximumLength string `gorm:"column:CHARACTER_MAXIMUM_LENGTH" json:"characterMaximumLength"`
CharacterSetName string `gorm:"column:CHARACTER_SET_NAME" json:"characterSetName"`
ColumnType string `gorm:"column:COLUMN_TYPE" json:"columnType"`
ColumnKey string `gorm:"column:COLUMN_KEY" json:"columnKey"`
Extra string `gorm:"column:EXTRA" json:"extra"`
ColumnComment string `gorm:"column:COLUMN_COMMENT" json:"columnComment"`
}
func (e *DBColumns) GetPage(tx *gorm.DB, pageSize int, pageIndex int) ([]DBColumns, int, error) {
var doc []DBColumns
var count int64
table := new(gorm.DB)
if config.DatabaseConfig.Driver == "mysql" {
table = tx.Table("information_schema.`COLUMNS`")
table = table.Where("table_schema= ? ", config.GenConfig.DBName)
if e.TableName != "" {
return nil, 0, errors.New("table name cannot be empty")
}
table = table.Where("TABLE_NAME = ?", e.TableName)
}
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
return nil, 0, err
}
//table.Count(&count)
return doc, int(count), nil
}
func (e *DBColumns) GetList(tx *gorm.DB) ([]DBColumns, error) {
var doc []DBColumns
table := new(gorm.DB)
if e.TableName == "" {
return nil, errors.New("table name cannot be empty")
}
if config.DatabaseConfig.Driver == "mysql" {
table = tx.Table("information_schema.columns")
table = table.Where("table_schema= ? ", config.GenConfig.DBName)
table = table.Where("TABLE_NAME = ?", e.TableName).Order("ORDINAL_POSITION asc")
} else {
pkg.Assert(true, "目前只支持mysql数据库", 500)
}
if err := table.Find(&doc).Error; err != nil {
return doc, err
}
return doc, nil
}

View File

@ -0,0 +1,62 @@
package tools
import (
"errors"
"github.com/go-admin-team/go-admin-core/sdk/pkg"
"gorm.io/gorm"
config2 "github.com/go-admin-team/go-admin-core/sdk/config"
)
type DBTables struct {
TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
Engine string `gorm:"column:ENGINE" json:"engine"`
TableRows string `gorm:"column:TABLE_ROWS" json:"tableRows"`
TableCollation string `gorm:"column:TABLE_COLLATION" json:"tableCollation"`
CreateTime string `gorm:"column:CREATE_TIME" json:"createTime"`
UpdateTime string `gorm:"column:UPDATE_TIME" json:"updateTime"`
TableComment string `gorm:"column:TABLE_COMMENT" json:"tableComment"`
}
func (e *DBTables) GetPage(tx *gorm.DB, pageSize int, pageIndex int) ([]DBTables, int, error) {
var doc []DBTables
table := new(gorm.DB)
var count int64
if config2.DatabaseConfig.Driver == "mysql" {
table = tx.Table("information_schema.tables")
table = table.Where("TABLE_NAME not in (select table_name from `" + config2.GenConfig.DBName + "`.sys_tables) ")
table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
if e.TableName != "" {
table = table.Where("TABLE_NAME = ?", e.TableName)
}
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
return nil, 0, err
}
} else {
pkg.Assert(true, "目前只支持mysql数据库", 500)
}
//table.Count(&count)
return doc, int(count), nil
}
func (e *DBTables) Get(tx *gorm.DB) (DBTables, error) {
var doc DBTables
if config2.DatabaseConfig.Driver == "mysql" {
table := tx.Table("information_schema.tables")
table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
if e.TableName == "" {
return doc, errors.New("table name cannot be empty")
}
table = table.Where("TABLE_NAME = ?", e.TableName)
if err := table.First(&doc).Error; err != nil {
return doc, err
}
} else {
pkg.Assert(true, "目前只支持mysql数据库", 500)
}
return doc, nil
}

View File

@ -0,0 +1,101 @@
package tools
import (
common "go-admin/common/models"
"gorm.io/gorm"
)
type SysColumns struct {
ColumnId int `gorm:"primaryKey;autoIncrement" json:"columnId"`
TableId int `gorm:"" json:"tableId"`
ColumnName string `gorm:"size:128;" json:"columnName"`
ColumnComment string `gorm:"column:column_comment;size:128;" json:"columnComment"`
ColumnType string `gorm:"column:column_type;size:128;" json:"columnType"`
GoType string `gorm:"column:go_type;size:128;" json:"goType"`
GoField string `gorm:"column:go_field;size:128;" json:"goField"`
JsonField string `gorm:"column:json_field;size:128;" json:"jsonField"`
IsPk string `gorm:"column:is_pk;size:4;" json:"isPk"`
IsIncrement string `gorm:"column:is_increment;size:4;" json:"isIncrement"`
IsRequired string `gorm:"column:is_required;size:4;" json:"isRequired"`
IsInsert string `gorm:"column:is_insert;size:4;" json:"isInsert"`
IsEdit string `gorm:"column:is_edit;size:4;" json:"isEdit"`
IsList string `gorm:"column:is_list;size:4;" json:"isList"`
IsQuery string `gorm:"column:is_query;size:4;" json:"isQuery"`
QueryType string `gorm:"column:query_type;size:128;" json:"queryType"`
HtmlType string `gorm:"column:html_type;size:128;" json:"htmlType"`
DictType string `gorm:"column:dict_type;size:128;" json:"dictType"`
Sort int `gorm:"column:sort;" json:"sort"`
List string `gorm:"column:list;size:1;" json:"list"`
Pk bool `gorm:"column:pk;size:1;" json:"pk"`
Required bool `gorm:"column:required;size:1;" json:"required"`
SuperColumn bool `gorm:"column:super_column;size:1;" json:"superColumn"`
UsableColumn bool `gorm:"column:usable_column;size:1;" json:"usableColumn"`
Increment bool `gorm:"column:increment;size:1;" json:"increment"`
Insert bool `gorm:"column:insert;size:1;" json:"insert"`
Edit bool `gorm:"column:edit;size:1;" json:"edit"`
Query bool `gorm:"column:query;size:1;" json:"query"`
Remark string `gorm:"column:remark;size:255;" json:"remark"`
FkTableName string `gorm:"" json:"fkTableName"`
FkTableNameClass string `gorm:"" json:"fkTableNameClass"`
FkTableNamePackage string `gorm:"" json:"fkTableNamePackage"`
FkCol []SysColumns `gorm:"-" json:"fkCol"`
FkLabelId string `gorm:"" json:"fkLabelId"`
FkLabelName string `gorm:"size:255;" json:"fkLabelName"`
CreateBy int `gorm:"column:create_by;size:20;" json:"createBy"`
UpdateBy int `gorm:"column:update_By;size:20;" json:"updateBy"`
common.ModelTime
}
func (*SysColumns) TableName() string {
return "sys_columns"
}
func (e *SysColumns) GetList(tx *gorm.DB, exclude bool) ([]SysColumns, error) {
var doc []SysColumns
table := tx.Table("sys_columns")
table = table.Where("table_id = ? ", e.TableId)
if exclude {
notIn := make([]string, 0, 6)
notIn = append(notIn, "id")
notIn = append(notIn, "create_by")
notIn = append(notIn, "update_by")
notIn = append(notIn, "created_at")
notIn = append(notIn, "updated_at")
notIn = append(notIn, "deleted_at")
table = table.Where(" column_name not in(?)", notIn)
}
if err := table.Find(&doc).Error; err != nil {
return nil, err
}
return doc, nil
}
func (e *SysColumns) Create(tx *gorm.DB) (SysColumns, error) {
var doc SysColumns
e.CreateBy = 0
result := tx.Table("sys_columns").Create(&e)
if result.Error != nil {
err := result.Error
return doc, err
}
doc = *e
return doc, nil
}
func (e *SysColumns) Update(tx *gorm.DB) (update SysColumns, err error) {
if err = tx.Table("sys_columns").First(&update, e.ColumnId).Error; err != nil {
return
}
//参数1:是要修改的数据
//参数2:是修改的数据
e.UpdateBy = 0
if err = tx.Table("sys_columns").Model(&update).Updates(&e).Error; err != nil {
return
}
return
}

View File

@ -0,0 +1,234 @@
package tools
import (
common "go-admin/common/models"
"strings"
"gorm.io/gorm"
)
type SysTables struct {
TableId int `gorm:"primaryKey;autoIncrement" json:"tableId"` //表编码
TBName string `gorm:"column:table_name;size:255;" json:"tableName"` //表名称
MLTBName string `gorm:"-" json:"-"` //表名称
TableComment string `gorm:"size:255;" json:"tableComment"` //表备注
ClassName string `gorm:"size:255;" json:"className"` //类名
TplCategory string `gorm:"size:255;" json:"tplCategory"` //
PackageName string `gorm:"size:255;" json:"packageName"` //包名
ModuleName string `gorm:"size:255;" json:"moduleName"` //go文件名
ModuleFrontName string `gorm:"size:255;comment:前端文件名;" json:"moduleFrontName"` //前端文件名
BusinessName string `gorm:"size:255;" json:"businessName"` //
FunctionName string `gorm:"size:255;" json:"functionName"` //功能名称
FunctionAuthor string `gorm:"size:255;" json:"functionAuthor"` //功能作者
PkColumn string `gorm:"size:255;" json:"pkColumn"`
PkGoField string `gorm:"size:255;" json:"pkGoField"`
PkJsonField string `gorm:"size:255;" json:"pkJsonField"`
Options string `gorm:"size:255;" json:"options"`
TreeCode string `gorm:"size:255;" json:"treeCode"`
TreeParentCode string `gorm:"size:255;" json:"treeParentCode"`
TreeName string `gorm:"size:255;" json:"treeName"`
Tree bool `gorm:"size:1;default:0;" json:"tree"`
Crud bool `gorm:"size:1;default:1;" json:"crud"`
Remark string `gorm:"size:255;" json:"remark"`
IsDataScope int `gorm:"size:1;" json:"isDataScope"`
IsActions int `gorm:"size:1;" json:"isActions"`
IsAuth int `gorm:"size:1;" json:"isAuth"`
IsLogicalDelete string `gorm:"size:1;" json:"isLogicalDelete"`
LogicalDelete bool `gorm:"size:1;" json:"logicalDelete"`
LogicalDeleteColumn string `gorm:"size:128;" json:"logicalDeleteColumn"`
common.ModelTime
common.ControlBy
DataScope string `gorm:"-" json:"dataScope"`
Params Params `gorm:"-" json:"params"`
Columns []SysColumns `gorm:"-" json:"columns"`
}
func (*SysTables) TableName() string {
return "sys_tables"
}
type Params struct {
TreeCode string `gorm:"-" json:"treeCode"`
TreeParentCode string `gorm:"-" json:"treeParentCode"`
TreeName string `gorm:"-" json:"treeName"`
}
func (e *SysTables) GetPage(tx *gorm.DB, pageSize int, pageIndex int) ([]SysTables, int, error) {
var doc []SysTables
table := tx.Table("sys_tables")
if e.TBName != "" {
table = table.Where("table_name = ?", e.TBName)
}
if e.TableComment != "" {
table = table.Where("table_comment = ?", e.TableComment)
}
var count int64
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
return nil, 0, err
}
//table.Where("`deleted_at` IS NULL").Count(&count)
return doc, int(count), nil
}
func (e *SysTables) Get(tx *gorm.DB, exclude bool) (SysTables, error) {
var doc SysTables
var err error
table := tx.Table("sys_tables")
if e.TBName != "" {
table = table.Where("table_name = ?", e.TBName)
}
if e.TableId != 0 {
table = table.Where("table_id = ?", e.TableId)
}
if e.TableComment != "" {
table = table.Where("table_comment = ?", e.TableComment)
}
if err := table.First(&doc).Error; err != nil {
return doc, err
}
var col SysColumns
col.TableId = doc.TableId
if doc.Columns, err = col.GetList(tx, exclude); err != nil {
return doc, err
}
return doc, nil
}
func (e *SysTables) GetTree(tx *gorm.DB) ([]SysTables, error) {
var doc []SysTables
var err error
table := tx.Table("sys_tables")
if e.TBName != "" {
table = table.Where("table_name = ?", e.TBName)
}
if e.TableId != 0 {
table = table.Where("table_id = ?", e.TableId)
}
if e.TableComment != "" {
table = table.Where("table_comment = ?", e.TableComment)
}
if err := table.Find(&doc).Error; err != nil {
return doc, err
}
for i := 0; i < len(doc); i++ {
var col SysColumns
//col.FkCol = append(col.FkCol, SysColumns{ColumnId: 0, ColumnName: "请选择"})
col.TableId = doc[i].TableId
if doc[i].Columns, err = col.GetList(tx, false); err != nil {
return doc, err
}
}
return doc, nil
}
func (e *SysTables) Create(tx *gorm.DB) (SysTables, error) {
var doc SysTables
e.CreateBy = 0
result := tx.Table("sys_tables").Create(&e)
if result.Error != nil {
err := result.Error
return doc, err
}
doc = *e
for i := 0; i < len(e.Columns); i++ {
e.Columns[i].TableId = doc.TableId
_, _ = e.Columns[i].Create(tx)
}
return doc, nil
}
func (e *SysTables) Update(tx *gorm.DB) (update SysTables, err error) {
//if err = orm.Eloquent.Table("sys_tables").First(&update, e.TableId).Error; err != nil {
// return
//}
//参数1:是要修改的数据
//参数2:是修改的数据
e.UpdateBy = 0
if err = tx.Table("sys_tables").Where("table_id = ?", e.TableId).Updates(&e).Error; err != nil {
return
}
tableNames := make([]string, 0)
for i := range e.Columns {
if e.Columns[i].FkTableName != "" {
tableNames = append(tableNames, e.Columns[i].FkTableName)
}
}
tables := make([]SysTables, 0)
tableMap := make(map[string]*SysTables)
if len(tableNames) > 0 {
if err = tx.Table("sys_tables").Where("table_name in (?)", tableNames).Find(&tables).Error; err != nil {
return
}
for i := range tables {
tableMap[tables[i].TBName] = &tables[i]
}
}
for i := 0; i < len(e.Columns); i++ {
if e.Columns[i].FkTableName != "" {
t, ok := tableMap[e.Columns[i].FkTableName]
if ok {
e.Columns[i].FkTableNameClass = t.ClassName
t.MLTBName = strings.Replace(t.TBName, "_", "-", -1)
e.Columns[i].FkTableNamePackage = t.MLTBName
} else {
tableNameList := strings.Split(e.Columns[i].FkTableName, "_")
e.Columns[i].FkTableNameClass = ""
//e.Columns[i].FkTableNamePackage = ""
for a := 0; a < len(tableNameList); a++ {
strStart := string([]byte(tableNameList[a])[:1])
strEnd := string([]byte(tableNameList[a])[1:])
e.Columns[i].FkTableNameClass += strings.ToUpper(strStart) + strEnd
//e.Columns[i].FkTableNamePackage += strings.ToLower(strStart) + strings.ToLower(strEnd)
}
}
}
_, _ = e.Columns[i].Update(tx)
}
return
}
func (e *SysTables) Delete(db *gorm.DB) (success bool, err error) {
tx := db.Begin()
defer func() {
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}()
if err = tx.Table("sys_tables").Delete(SysTables{}, "table_id = ?", e.TableId).Error; err != nil {
success = false
return
}
if err = tx.Table("sys_columns").Delete(SysColumns{}, "table_id = ?", e.TableId).Error; err != nil {
success = false
return
}
success = true
return
}
func (e *SysTables) BatchDelete(tx *gorm.DB, id []int) (Result bool, err error) {
if err = tx.Unscoped().Table(e.TableName()).Where(" table_id in (?)", id).Delete(&SysColumns{}).Error; err != nil {
return
}
Result = true
return
}