first commit

This commit is contained in:
Blizzard
2026-02-27 13:54:01 +08:00
commit fc585fa4df
127 changed files with 18548 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
package internal
import (
"sundynix-go/config"
"sundynix-go/global"
"time"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
var Gorm = new(_gorm)
type _gorm struct{}
// Config 函数用于根据数据库类型和配置生成 GORM 的配置对象
// 该函数会根据全局配置中的数据库类型选择相应的通用配置,并返回一个配置好的 *gorm.Config 对象。
//
// 参数:
// - prefix: 表名前缀,用于在生成表名时添加到表名前。
// - singular: 是否禁用复数表名,true 表示禁用复数表名,false 表示使用复数表名。
//
// 返回值:
// - *gorm.Config: 配置好的 GORM 配置对象,包含日志、命名策略等配置。
func (g *_gorm) Config(prefix string, singular bool) *gorm.Config {
// 根据全局配置中的数据库类型选择相应的通用配置
var general config.GeneralDB
switch global.Config.System.DbType {
case "mysql":
general = global.Config.Mysql.GeneralDB
case "pgsql":
general = global.Config.Pgsql.GeneralDB
case "sqlite":
general = global.Config.Sqlite.GeneralDB
default:
// 默认使用 MySQL 的通用配置
general = global.Config.Mysql.GeneralDB
}
// 返回配置好的 GORM 配置对象
return &gorm.Config{
// 配置日志记录器,使用自定义的日志写入器,并设置慢查询阈值、日志级别和颜色输出
Logger: logger.New(NewWriter(general), logger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: general.LogLevel(),
Colorful: true,
}),
// 配置命名策略,设置表前缀和是否禁用复数表名
NamingStrategy: schema.NamingStrategy{
TablePrefix: prefix, // 表前缀
SingularTable: singular, // 禁用复数表名
},
// 禁用自动创建外键约束
DisableForeignKeyConstraintWhenMigrating: true,
}
}
+39
View File
@@ -0,0 +1,39 @@
package internal
import (
"fmt"
"gorm.io/gorm/logger"
"sundynix-go/config"
"sundynix-go/global"
)
type Writer struct {
config config.GeneralDB
writer logger.Writer
}
// NewWriter 创建一个Writer
func NewWriter(config config.GeneralDB) *Writer {
return &Writer{config: config}
}
// Printf 格式化打印日志
func (w *Writer) Printf(message string, data ...any) {
fmt.Printf(message, data)
//当开启了zap的情况下,会打印到日志记录中
if w.config.LogZap {
switch w.config.LogLevel() {
case logger.Silent:
global.Logger.Debug(fmt.Sprintf(message, data))
case logger.Error:
global.Logger.Error(fmt.Sprintf(message, data))
case logger.Warn:
global.Logger.Warn(fmt.Sprintf(message, data))
case logger.Info:
global.Logger.Info(fmt.Sprintf(message, data))
default:
global.Logger.Info(fmt.Sprintf(message, data))
}
return
}
}