feat:完善项目信息
This commit is contained in:
@ -1,28 +1,26 @@
|
||||
package common
|
||||
package migrate
|
||||
|
||||
import (
|
||||
config2 "SciencesServer/app/basic/config"
|
||||
"SciencesServer/app/common/model"
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/serve/orm"
|
||||
"SciencesServer/utils"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
gormDB *gorm.DB
|
||||
}
|
||||
|
||||
type Option func(instance *Instance)
|
||||
|
||||
type synchronized struct {
|
||||
iModel model.IModel
|
||||
iValues func() interface{}
|
||||
Catch func() interface{}
|
||||
}
|
||||
|
||||
type caches struct {
|
||||
iModel model.IModel
|
||||
iValues func() interface{}
|
||||
toCache func(values interface{})
|
||||
}
|
||||
|
||||
func initModel() {
|
||||
db := orm.GetDB()
|
||||
func (this *Instance) Handle() {
|
||||
db := this.gormDB
|
||||
|
||||
function := func(synchronized ...*synchronized) {
|
||||
for _, v := range synchronized {
|
||||
@ -139,62 +137,18 @@ func initModel() {
|
||||
&synchronized{iModel: model.NewActivityExamine()}, &synchronized{iModel: model.NewActivityJoin()},
|
||||
)
|
||||
}
|
||||
func initCacheMode() {
|
||||
db := orm.GetDB()
|
||||
|
||||
function := func(cache ...*caches) {
|
||||
for _, v := range cache {
|
||||
if db.Migrator().HasTable(v.iModel) {
|
||||
if v.iValues != nil {
|
||||
if values := v.iValues(); values != nil {
|
||||
v.toCache(values)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
func WithGormDBOption(db *gorm.DB) Option {
|
||||
return func(instance *Instance) {
|
||||
instance.gormDB = db
|
||||
}
|
||||
function(
|
||||
&caches{iModel: model.NewSysTenant(), iValues: func() interface{} {
|
||||
out := make([]*model.SysConfig, 0)
|
||||
_ = model.Find(model.NewSysConfig(), &out)
|
||||
return out
|
||||
}, toCache: func(values interface{}) {
|
||||
out := values.([]*model.SysConfig)
|
||||
for _, v := range out {
|
||||
config.SystemConfig[v.Key] = v.Value
|
||||
}
|
||||
}},
|
||||
)
|
||||
function(
|
||||
&caches{iModel: model.NewSysIndustry(), iValues: func() interface{} {
|
||||
out := make([]*model.SysIndustry, 0)
|
||||
_ = model.ScanFields(model.NewSysIndustry(), &out, []string{"id", "name"})
|
||||
return out
|
||||
}, toCache: func(values interface{}) {
|
||||
out := values.([]*model.SysIndustry)
|
||||
for _, v := range out {
|
||||
config2.MemoryForIndustryInfo[fmt.Sprintf("%d", v.ID)] = v.Name
|
||||
}
|
||||
}},
|
||||
)
|
||||
function(
|
||||
&caches{iModel: model.NewSysPlatform(), iValues: func() interface{} {
|
||||
out := make([]*model.SysPlatform, 0)
|
||||
_ = model.ScanFields(model.NewSysPlatform(), &out, []string{"id", "key", "link"})
|
||||
return out
|
||||
}, toCache: func(values interface{}) {
|
||||
out := values.([]*model.SysPlatform)
|
||||
for _, v := range out {
|
||||
if v.Link == "" {
|
||||
continue
|
||||
}
|
||||
config2.MemoryForPlatformInfo[v.Link] = v.Key
|
||||
}
|
||||
}},
|
||||
)
|
||||
}
|
||||
|
||||
func Init() {
|
||||
initModel()
|
||||
initCacheMode()
|
||||
func NewInstance(options ...Option) *Instance {
|
||||
out := new(Instance)
|
||||
|
||||
for _, v := range options {
|
||||
v(out)
|
||||
}
|
||||
return out
|
||||
}
|
Reference in New Issue
Block a user