2026-04-22 16:01:59 +08:00
|
|
|
|
package model
|
2026-03-09 21:28:58 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-03-17 00:47:17 +08:00
|
|
|
|
"go.uber.org/zap"
|
2026-03-09 21:28:58 +08:00
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
"gorm.io/gorm/logger"
|
2026-05-15 14:44:40 +08:00
|
|
|
|
"gorm.io/plugin/dbresolver"
|
2026-03-09 21:28:58 +08:00
|
|
|
|
|
2026-04-22 16:01:59 +08:00
|
|
|
|
"with_you/internal/config"
|
2026-03-09 21:28:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-13 09:38:18 +08:00
|
|
|
|
// NewDB 创建数据库连接(用于 Wire 依赖注入)
|
|
|
|
|
|
func NewDB(cfg *config.DatabaseConfig) (*gorm.DB, error) {
|
2026-03-09 21:28:58 +08:00
|
|
|
|
var err error
|
|
|
|
|
|
var db *gorm.DB
|
|
|
|
|
|
gormLogger := logger.New(
|
|
|
|
|
|
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
|
|
|
|
|
logger.Config{
|
|
|
|
|
|
SlowThreshold: time.Duration(cfg.SlowThresholdMs) * time.Millisecond,
|
|
|
|
|
|
LogLevel: parseGormLogLevel(cfg.LogLevel),
|
|
|
|
|
|
IgnoreRecordNotFoundError: cfg.IgnoreRecordNotFound,
|
|
|
|
|
|
ParameterizedQueries: cfg.ParameterizedQueries,
|
|
|
|
|
|
Colorful: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 根据数据库类型选择驱动
|
|
|
|
|
|
switch cfg.Type {
|
|
|
|
|
|
case "sqlite":
|
|
|
|
|
|
db, err = gorm.Open(sqlite.Open(cfg.SQLite.Path), &gorm.Config{
|
|
|
|
|
|
Logger: gormLogger,
|
|
|
|
|
|
})
|
|
|
|
|
|
case "postgres", "postgresql":
|
|
|
|
|
|
dsn := cfg.Postgres.DSN()
|
|
|
|
|
|
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
|
|
|
|
|
|
Logger: gormLogger,
|
|
|
|
|
|
})
|
|
|
|
|
|
default:
|
|
|
|
|
|
// 默认使用PostgreSQL
|
|
|
|
|
|
dsn := cfg.Postgres.DSN()
|
|
|
|
|
|
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
|
|
|
|
|
|
Logger: gormLogger,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2026-03-13 09:38:18 +08:00
|
|
|
|
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
2026-03-09 21:28:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 配置连接池(SQLite不支持连接池配置,跳过)
|
|
|
|
|
|
if cfg.Type != "sqlite" {
|
2026-03-13 09:38:18 +08:00
|
|
|
|
sqlDB, err := db.DB()
|
2026-03-09 21:28:58 +08:00
|
|
|
|
if err != nil {
|
2026-03-13 09:38:18 +08:00
|
|
|
|
return nil, fmt.Errorf("failed to get database instance: %w", err)
|
2026-03-09 21:28:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
|
|
|
|
|
|
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-15 14:44:40 +08:00
|
|
|
|
// 配置读写分离(仅 PostgreSQL 且配置了读副本时生效)
|
|
|
|
|
|
if (cfg.Type == "postgres" || cfg.Type == "postgresql") && cfg.HasReplica() {
|
|
|
|
|
|
allReplicas := cfg.AllReplicas()
|
|
|
|
|
|
replicaDialectors := make([]gorm.Dialector, len(allReplicas))
|
|
|
|
|
|
for i, replica := range allReplicas {
|
|
|
|
|
|
replicaDialectors[i] = postgres.Open(replica.DSN())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
replicaMaxIdle := cfg.ReplicaMaxIdle
|
|
|
|
|
|
if replicaMaxIdle == 0 {
|
|
|
|
|
|
replicaMaxIdle = cfg.MaxIdleConns
|
|
|
|
|
|
}
|
|
|
|
|
|
replicaMaxOpen := cfg.ReplicaMaxOpen
|
|
|
|
|
|
if replicaMaxOpen == 0 {
|
|
|
|
|
|
replicaMaxOpen = cfg.MaxOpenConns
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resolver := dbresolver.Register(dbresolver.Config{
|
|
|
|
|
|
Replicas: replicaDialectors,
|
|
|
|
|
|
}).
|
|
|
|
|
|
SetMaxIdleConns(replicaMaxIdle).
|
|
|
|
|
|
SetMaxOpenConns(replicaMaxOpen).
|
|
|
|
|
|
SetConnMaxLifetime(time.Hour)
|
|
|
|
|
|
|
|
|
|
|
|
if err := db.Use(resolver); err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to register dbresolver plugin: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
zap.L().Info("Database read/write splitting configured",
|
|
|
|
|
|
zap.String("type", cfg.Type),
|
|
|
|
|
|
zap.Int("replica_count", len(allReplicas)),
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-09 21:28:58 +08:00
|
|
|
|
// 自动迁移
|
2026-03-13 09:38:18 +08:00
|
|
|
|
if err := autoMigrate(db); err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("failed to auto migrate: %w", err)
|
2026-03-09 21:28:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 00:47:17 +08:00
|
|
|
|
zap.L().Info("Database connected and migrated successfully",
|
|
|
|
|
|
zap.String("type", cfg.Type),
|
|
|
|
|
|
)
|
2026-03-13 09:38:18 +08:00
|
|
|
|
return db, nil
|
2026-03-09 21:28:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func parseGormLogLevel(level string) logger.LogLevel {
|
|
|
|
|
|
switch level {
|
|
|
|
|
|
case "silent":
|
|
|
|
|
|
return logger.Silent
|
|
|
|
|
|
case "error":
|
|
|
|
|
|
return logger.Error
|
|
|
|
|
|
case "warn":
|
|
|
|
|
|
return logger.Warn
|
|
|
|
|
|
case "info":
|
|
|
|
|
|
return logger.Info
|
|
|
|
|
|
default:
|
|
|
|
|
|
return logger.Warn
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// autoMigrate 自动迁移数据库表
|
|
|
|
|
|
func autoMigrate(db *gorm.DB) error {
|
|
|
|
|
|
err := db.AutoMigrate(
|
|
|
|
|
|
// 用户相关
|
|
|
|
|
|
&User{},
|
|
|
|
|
|
|
|
|
|
|
|
// 帖子相关
|
|
|
|
|
|
&Post{},
|
|
|
|
|
|
&PostImage{},
|
2026-03-24 22:27:53 +08:00
|
|
|
|
&Channel{},
|
2026-03-09 21:28:58 +08:00
|
|
|
|
|
|
|
|
|
|
// 评论相关
|
|
|
|
|
|
&Comment{},
|
|
|
|
|
|
&CommentLike{},
|
|
|
|
|
|
|
|
|
|
|
|
// 消息相关(使用雪花算法ID和seq机制)
|
|
|
|
|
|
// 已读位置存储在 ConversationParticipant.LastReadSeq 中
|
|
|
|
|
|
&Conversation{},
|
|
|
|
|
|
&ConversationParticipant{},
|
|
|
|
|
|
&Message{},
|
|
|
|
|
|
|
|
|
|
|
|
// 系统通知(独立表,每个用户只能看到自己的通知)
|
|
|
|
|
|
&SystemNotification{},
|
|
|
|
|
|
|
|
|
|
|
|
// 通知
|
|
|
|
|
|
&Notification{},
|
|
|
|
|
|
|
|
|
|
|
|
// 推送中心相关
|
|
|
|
|
|
&PushRecord{}, // 推送记录
|
|
|
|
|
|
&DeviceToken{}, // 设备Token
|
|
|
|
|
|
|
|
|
|
|
|
// 社交
|
|
|
|
|
|
&Follow{},
|
|
|
|
|
|
&UserBlock{},
|
|
|
|
|
|
&PostLike{},
|
|
|
|
|
|
&Favorite{},
|
|
|
|
|
|
|
|
|
|
|
|
// 投票
|
|
|
|
|
|
&VoteOption{},
|
|
|
|
|
|
&UserVote{},
|
|
|
|
|
|
|
|
|
|
|
|
// 敏感词和审核
|
|
|
|
|
|
&SensitiveWord{},
|
2026-06-01 13:41:02 +08:00
|
|
|
|
// &AuditLog{}, // TODO: define AuditLog model
|
2026-03-09 21:28:58 +08:00
|
|
|
|
|
2026-03-29 20:18:36 +08:00
|
|
|
|
// 举报
|
|
|
|
|
|
&Report{},
|
|
|
|
|
|
|
2026-03-19 19:43:37 +08:00
|
|
|
|
// 日志相关
|
|
|
|
|
|
&OperationLog{}, // 操作日志
|
|
|
|
|
|
&LoginLog{}, // 登录日志
|
|
|
|
|
|
&DataChangeLog{}, // 数据变更日志
|
|
|
|
|
|
|
2026-03-09 21:28:58 +08:00
|
|
|
|
// 群组相关
|
|
|
|
|
|
&Group{},
|
|
|
|
|
|
&GroupMember{},
|
|
|
|
|
|
&GroupAnnouncement{},
|
|
|
|
|
|
&GroupJoinRequest{},
|
|
|
|
|
|
|
|
|
|
|
|
// 自定义表情
|
|
|
|
|
|
&UserSticker{},
|
2026-03-12 08:38:14 +08:00
|
|
|
|
|
|
|
|
|
|
// 课表
|
|
|
|
|
|
&ScheduleCourse{},
|
2026-03-14 02:09:38 +08:00
|
|
|
|
|
2026-05-12 01:28:18 +08:00
|
|
|
|
// 成绩与考试
|
|
|
|
|
|
&Grade{},
|
|
|
|
|
|
&GpaSummary{},
|
|
|
|
|
|
&Exam{},
|
2026-05-31 21:29:45 +08:00
|
|
|
|
&EmptyClassroom{},
|
2026-05-12 01:28:18 +08:00
|
|
|
|
|
2026-03-14 02:09:38 +08:00
|
|
|
|
// 用户活跃相关
|
|
|
|
|
|
&UserActiveLog{},
|
|
|
|
|
|
&UserActivityStat{},
|
|
|
|
|
|
|
|
|
|
|
|
// 角色和权限相关
|
|
|
|
|
|
&Role{},
|
|
|
|
|
|
&UserRole{},
|
|
|
|
|
|
&CasbinRule{},
|
2026-03-25 20:44:12 +08:00
|
|
|
|
|
|
|
|
|
|
// 学习资料相关
|
|
|
|
|
|
&MaterialSubject{},
|
|
|
|
|
|
&MaterialFile{},
|
2026-03-27 01:54:34 +08:00
|
|
|
|
|
|
|
|
|
|
// 通话相关
|
|
|
|
|
|
&CallSession{},
|
|
|
|
|
|
&CallParticipant{},
|
2026-04-05 20:27:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 身份认证相关
|
|
|
|
|
|
&VerificationRecord{},
|
2026-04-15 11:50:47 +08:00
|
|
|
|
|
|
|
|
|
|
// 用户资料审核相关
|
|
|
|
|
|
&UserProfileAudit{},
|
2026-04-26 00:37:20 +08:00
|
|
|
|
|
|
|
|
|
|
// 帖子内链引用关系
|
|
|
|
|
|
&PostReference{},
|
feat(trade): implement second-hand trading/flea market feature
Add complete trade functionality including:
- TradeItem, TradeImage, TradeFavorite models with auto-migration
- TradeRepository with CRUD and favorite operations
- TradeService with business logic for listing, creating, updating, status management
- TradeHandler with RESTful endpoints for trade operations
- DTOs and converters for request/response handling
Routes include: list, get by id, create, update, delete, status update, view recording, favorite/unfavorite.
2026-04-26 15:20:26 +08:00
|
|
|
|
|
|
|
|
|
|
// 二手交易相关
|
|
|
|
|
|
&TradeItem{},
|
|
|
|
|
|
&TradeImage{},
|
|
|
|
|
|
&TradeFavorite{},
|
2026-03-09 21:28:58 +08:00
|
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-24 05:18:30 +08:00
|
|
|
|
// AutoMigrate 不会删除列:清理已废弃的 posts.hot_score
|
|
|
|
|
|
if err := dropPostsHotScoreColumnIfExists(db); err != nil {
|
|
|
|
|
|
return fmt.Errorf("drop legacy posts.hot_score: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-14 18:01:55 +08:00
|
|
|
|
// 初始化角色种子数据
|
|
|
|
|
|
if err := seedRoles(db); err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to seed roles: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化权限策略种子数据
|
|
|
|
|
|
if err := seedPermissions(db); err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to seed permissions: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-24 22:27:53 +08:00
|
|
|
|
// 初始化频道配置种子数据
|
|
|
|
|
|
if err := seedChannels(db); err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to seed channels: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-25 20:44:12 +08:00
|
|
|
|
// 初始化学习资料学科种子数据
|
|
|
|
|
|
if err := seedMaterialSubjects(db); err != nil {
|
|
|
|
|
|
return fmt.Errorf("failed to seed material subjects: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-14 18:01:55 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-24 05:18:30 +08:00
|
|
|
|
// postWithHotScore 仅用于 Migrator 检测/删除旧列(Post 模型已移除 HotScore)
|
|
|
|
|
|
type postWithHotScore struct {
|
|
|
|
|
|
HotScore float64 `gorm:"column:hot_score"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (postWithHotScore) TableName() string { return "posts" }
|
|
|
|
|
|
|
|
|
|
|
|
func dropPostsHotScoreColumnIfExists(db *gorm.DB) error {
|
|
|
|
|
|
m := db.Migrator()
|
|
|
|
|
|
if !m.HasTable(&Post{}) {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
if !m.HasColumn(&postWithHotScore{}, "HotScore") {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return m.DropColumn(&postWithHotScore{}, "HotScore")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-14 18:01:55 +08:00
|
|
|
|
// seedRoles 初始化角色种子数据
|
|
|
|
|
|
func seedRoles(db *gorm.DB) error {
|
|
|
|
|
|
// 检查是否已有角色数据
|
|
|
|
|
|
var count int64
|
|
|
|
|
|
if err := db.Model(&Role{}).Count(&count).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果已有数据,跳过种子初始化
|
|
|
|
|
|
if count > 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 预定义角色数据
|
|
|
|
|
|
roles := []Role{
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: RoleSuperAdmin,
|
|
|
|
|
|
DisplayName: "超级管理员",
|
|
|
|
|
|
Description: "拥有系统最高权限,可以管理所有用户和内容",
|
|
|
|
|
|
Priority: 100,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: RoleAdmin,
|
|
|
|
|
|
DisplayName: "管理员",
|
|
|
|
|
|
Description: "系统管理员,可以管理用户和内容",
|
|
|
|
|
|
Priority: 80,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: RoleModerator,
|
|
|
|
|
|
DisplayName: "版主",
|
|
|
|
|
|
Description: "内容审核员,可以审核和管理内容",
|
|
|
|
|
|
Priority: 60,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: RoleUser,
|
|
|
|
|
|
DisplayName: "普通用户",
|
|
|
|
|
|
Description: "注册用户,拥有基本权限",
|
|
|
|
|
|
Priority: 40,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
Name: RoleBanned,
|
|
|
|
|
|
DisplayName: "被封禁用户",
|
|
|
|
|
|
Description: "被禁止访问的用户",
|
|
|
|
|
|
Priority: 0,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 批量插入角色
|
|
|
|
|
|
if err := db.Create(&roles).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 00:47:17 +08:00
|
|
|
|
zap.L().Info("Seeded roles successfully",
|
|
|
|
|
|
zap.Int("count", len(roles)),
|
|
|
|
|
|
)
|
2026-03-14 18:01:55 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// seedPermissions 初始化权限策略种子数据
|
|
|
|
|
|
func seedPermissions(db *gorm.DB) error {
|
|
|
|
|
|
// 检查是否已有权限策略数据
|
|
|
|
|
|
var count int64
|
|
|
|
|
|
if err := db.Model(&CasbinRule{}).Where("ptype = ?", "p").Count(&count).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果已有数据,跳过种子初始化
|
|
|
|
|
|
if count > 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 预定义权限策略数据
|
|
|
|
|
|
// p = sub, obj, act (角色, 资源, 操作)
|
|
|
|
|
|
permissions := []CasbinRule{
|
|
|
|
|
|
// 超级管理员 - 拥有所有权限
|
|
|
|
|
|
{Ptype: "p", V0: RoleSuperAdmin, V1: "/*", V2: "*"},
|
|
|
|
|
|
|
|
|
|
|
|
// 管理员 - 拥有管理权限
|
|
|
|
|
|
{Ptype: "p", V0: RoleAdmin, V1: "/api/v1/admin/*", V2: "*"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleAdmin, V1: "/api/v1/users/*", V2: "GET"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleAdmin, V1: "/api/v1/posts/*", V2: "*"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleAdmin, V1: "/api/v1/comments/*", V2: "*"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleAdmin, V1: "/api/v1/groups/*", V2: "*"},
|
|
|
|
|
|
|
|
|
|
|
|
// 版主 - 拥有内容审核权限
|
|
|
|
|
|
{Ptype: "p", V0: RoleModerator, V1: "/api/v1/posts/*", V2: "GET"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleModerator, V1: "/api/v1/posts/*", V2: "DELETE"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleModerator, V1: "/api/v1/comments/*", V2: "GET"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleModerator, V1: "/api/v1/comments/*", V2: "DELETE"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleModerator, V1: "/api/v1/groups/*", V2: "GET"},
|
|
|
|
|
|
|
|
|
|
|
|
// 普通用户 - 拥有基本权限
|
|
|
|
|
|
{Ptype: "p", V0: RoleUser, V1: "/api/v1/posts", V2: "GET"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleUser, V1: "/api/v1/posts/*", V2: "GET"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleUser, V1: "/api/v1/posts", V2: "POST"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleUser, V1: "/api/v1/comments/*", V2: "GET"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleUser, V1: "/api/v1/comments", V2: "POST"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleUser, V1: "/api/v1/users/me", V2: "GET"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleUser, V1: "/api/v1/users/me", V2: "PUT"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleUser, V1: "/api/v1/groups", V2: "GET"},
|
|
|
|
|
|
{Ptype: "p", V0: RoleUser, V1: "/api/v1/groups/*", V2: "GET"},
|
|
|
|
|
|
|
|
|
|
|
|
// 被封禁用户 - 无权限
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 批量插入权限策略
|
|
|
|
|
|
if err := db.Create(&permissions).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 00:47:17 +08:00
|
|
|
|
zap.L().Info("Seeded permissions successfully",
|
|
|
|
|
|
zap.Int("count", len(permissions)),
|
|
|
|
|
|
)
|
2026-03-09 21:28:58 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-25 20:44:12 +08:00
|
|
|
|
// seedChannels 初始化频道种子数据(仅在表为空时)
|
2026-03-24 22:27:53 +08:00
|
|
|
|
func seedChannels(db *gorm.DB) error {
|
2026-03-25 20:44:12 +08:00
|
|
|
|
// 检查是否已有频道数据
|
|
|
|
|
|
var count int64
|
|
|
|
|
|
if err := db.Model(&Channel{}).Count(&count).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if count > 0 {
|
|
|
|
|
|
zap.L().Info("Channels already exist, skipping seed", zap.Int64("count", count))
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-24 22:27:53 +08:00
|
|
|
|
defaultChannels := []Channel{
|
|
|
|
|
|
{Name: "跳蚤市场", Slug: "market", Description: "二手交易与闲置发布", SortOrder: 10, IsActive: true},
|
|
|
|
|
|
{Name: "课程交流", Slug: "courses", Description: "课程资料、选课与学习讨论", SortOrder: 20, IsActive: true},
|
|
|
|
|
|
{Name: "工作实习", Slug: "jobs", Description: "实习、内推与求职信息", SortOrder: 30, IsActive: true},
|
|
|
|
|
|
{Name: "考研考公", Slug: "exam", Description: "备考经验、资料与互助", SortOrder: 40, IsActive: true},
|
|
|
|
|
|
{Name: "保研出国", Slug: "graduate-abroad", Description: "保研、留学申请与经验分享", SortOrder: 50, IsActive: true},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-25 20:44:12 +08:00
|
|
|
|
if err := db.Create(&defaultChannels).Error; err != nil {
|
|
|
|
|
|
return err
|
2026-03-24 22:27:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
zap.L().Info("Seeded channels successfully", zap.Int("count", len(defaultChannels)))
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-25 20:44:12 +08:00
|
|
|
|
// seedMaterialSubjects 初始化学习资料学科种子数据(仅在表为空时)
|
|
|
|
|
|
func seedMaterialSubjects(db *gorm.DB) error {
|
|
|
|
|
|
// 检查是否已有数据
|
|
|
|
|
|
var count int64
|
|
|
|
|
|
if err := db.Model(&MaterialSubject{}).Count(&count).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if count > 0 {
|
|
|
|
|
|
zap.L().Info("Material subjects already exist, skipping seed", zap.Int64("count", count))
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defaultSubjects := []MaterialSubject{
|
|
|
|
|
|
{Name: "数学", Icon: "calculator-variant", Color: "#FF6B6B", Description: "高等数学、线性代数、概率论等", SortOrder: 10, IsActive: true},
|
|
|
|
|
|
{Name: "英语", Icon: "translate", Color: "#4ECDC4", Description: "四六级、考研英语、托福雅思等", SortOrder: 20, IsActive: true},
|
|
|
|
|
|
{Name: "物理", Icon: "atom", Color: "#45B7D1", Description: "大学物理、电磁学、力学等", SortOrder: 30, IsActive: true},
|
|
|
|
|
|
{Name: "化学", Icon: "flask", Color: "#96CEB4", Description: "有机化学、无机化学、分析化学等", SortOrder: 40, IsActive: true},
|
|
|
|
|
|
{Name: "计算机", Icon: "laptop", Color: "#FFEAA7", Description: "数据结构、算法、操作系统等", SortOrder: 50, IsActive: true},
|
|
|
|
|
|
{Name: "经济管理", Icon: "chart-line", Color: "#DDA0DD", Description: "微观经济学、宏观经济学、管理学等", SortOrder: 60, IsActive: true},
|
|
|
|
|
|
{Name: "法学", Icon: "scale-balance", Color: "#F39C12", Description: "民法、刑法、宪法等", SortOrder: 70, IsActive: true},
|
|
|
|
|
|
{Name: "语文文学", Icon: "book-open-page-variant", Color: "#3498DB", Description: "古代文学、现代文学、写作等", SortOrder: 80, IsActive: true},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := db.Create(&defaultSubjects).Error; err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
zap.L().Info("Seeded material subjects successfully", zap.Int("count", len(defaultSubjects)))
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|