You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
1.8 KiB
88 lines
1.8 KiB
package internal
|
|
|
|
import (
|
|
"errors"
|
|
"sorbet/internal/entities"
|
|
"sorbet/internal/runtime"
|
|
"sorbet/internal/services/company"
|
|
"sorbet/internal/services/config"
|
|
"sorbet/internal/services/feature"
|
|
"sorbet/internal/services/resource"
|
|
"sorbet/internal/services/system"
|
|
"sorbet/internal/util"
|
|
"sorbet/pkg/db"
|
|
"sorbet/pkg/env"
|
|
"sorbet/pkg/log"
|
|
)
|
|
|
|
func Init() error {
|
|
if err := syncEntities(); err != nil {
|
|
if !errors.Is(err, db.ErrNoCodeFirst) {
|
|
return err
|
|
}
|
|
if !env.IsEnv("prod") {
|
|
log.Warn("同步数据表结构需要开启 [DB_CODE_FIRST],在生产模式下请务必关闭。")
|
|
}
|
|
}
|
|
if err := initSystemUser(); err != nil {
|
|
return err
|
|
}
|
|
return useServlets()
|
|
}
|
|
|
|
// 同步数据库结构
|
|
func syncEntities() error {
|
|
return db.Sync(
|
|
&entities.Company{},
|
|
&entities.CompanyDepartment{},
|
|
&entities.CompanyEmployee{},
|
|
&entities.CompanyAuthTicket{},
|
|
&entities.ConfigGroup{},
|
|
&entities.Config{},
|
|
&entities.Feature{},
|
|
&entities.FeatureCategory{},
|
|
&entities.FeatureConfig{},
|
|
&entities.FeatureContent{},
|
|
&entities.FeatureContentChapter{},
|
|
&entities.FeatureContentDetail{},
|
|
&entities.ResourceCategory{},
|
|
&entities.Resource{},
|
|
&entities.SystemLog{},
|
|
&entities.SystemMenu{},
|
|
&entities.SystemPermission{},
|
|
&entities.SystemRole{},
|
|
&entities.SystemRolePower{},
|
|
&entities.SystemUser{},
|
|
)
|
|
}
|
|
|
|
func initSystemUser() error {
|
|
var count int64
|
|
err := db.
|
|
Model(&entities.SystemUser{}).
|
|
Where("username", "admin").
|
|
Count(&count).
|
|
Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count > 0 {
|
|
return nil
|
|
}
|
|
hash, _ := util.PasswordHash("111111")
|
|
_, err = db.Create(&entities.SystemUser{
|
|
Username: "admin",
|
|
Password: hash,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func useServlets() error {
|
|
return runtime.Use(
|
|
&config.Service{},
|
|
&company.Service{},
|
|
&resource.Service{},
|
|
&feature.Service{},
|
|
&system.Service{},
|
|
)
|
|
}
|
|
|