2026-05-02 13:02:38 +08:00

110 lines
2.3 KiB
Go

package repository
import (
"gorm.io/gorm"
"hyapp-admin-server/internal/config"
"hyapp-admin-server/internal/model"
)
type Store struct {
db *gorm.DB
}
type ListOptions struct {
Page int
PageSize int
Keyword string
Role string
Status string
Sort string
}
type SearchResult struct {
Type string `json:"type"`
ID string `json:"id"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
Target string `json:"target"`
}
type DashboardOverview struct {
UsersTotal int `json:"usersTotal"`
ActiveUsers int `json:"activeUsers"`
LockedUsers int `json:"lockedUsers"`
DisabledUsers int `json:"disabledUsers"`
RolesTotal int `json:"rolesTotal"`
MenuTotal int `json:"menuTotal"`
LogsToday int `json:"logsToday"`
UnreadNotifications int `json:"unreadNotifications"`
Alerts []map[string]string `json:"alerts"`
Series map[string][]int `json:"series"`
UpdatedAt string `json:"updatedAt"`
}
type MenuSortItem struct {
ID uint `json:"id"`
Sort int `json:"sort"`
}
type DataAccess struct {
UserID uint
All bool
Restricted bool
SelfOnly bool
Teams []string
}
func New(db *gorm.DB) *Store {
return &Store{db: db}
}
func (s *Store) AutoMigrate() error {
return s.db.AutoMigrate(
&model.User{},
&model.Role{},
&model.Permission{},
&model.Menu{},
&model.AppConfig{},
&model.RefreshToken{},
&model.LoginLog{},
&model.OperationLog{},
&model.Notification{},
&model.DataScope{},
&model.AdminJob{},
)
}
func (s *Store) Ping() error {
sqlDB, err := s.db.DB()
if err != nil {
return err
}
return sqlDB.Ping()
}
func (s *Store) Seed(cfg config.Config) error {
if !cfg.Bootstrap.Enabled {
return nil
}
if err := s.seedPermissions(); err != nil {
return err
}
if err := s.seedMenus(); err != nil {
return err
}
if err := s.pruneDeprecatedDefaults(); err != nil {
return err
}
if err := s.seedRoles(); err != nil {
return err
}
if err := s.seedUsers(cfg); err != nil {
return err
}
if !cfg.Bootstrap.SeedDemoData {
return nil
}
return s.seedNotifications()
}