From cef1a1d6e4aeb7189ff05370139ff367736b7e92 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 19 十月 2023 14:52:54 +0800 Subject: [PATCH] 去掉无用初始化 --- source/user.go | 132 ++++++++++++++++---------------- service/initdb.go | 4 source/menu.go | 101 ++++++++++++------------ 3 files changed, 119 insertions(+), 118 deletions(-) diff --git a/service/initdb.go b/service/initdb.go index f377d0d..fd7149e 100644 --- a/service/initdb.go +++ b/service/initdb.go @@ -3,7 +3,6 @@ import ( "aps_crm/pkg/logx" "context" - "errors" "sort" ) @@ -63,7 +62,8 @@ // InitDB 鍒濆鍖� func (initDBService *InitDBService) InitDB() (err error) { if len(initializerList) == 0 { - return errors.New("鏃犲彲鐢ㄥ垵濮嬪寲杩囩▼锛岃妫�鏌ュ垵濮嬪寲鏄惁宸叉墽琛屽畬鎴�") + logx.Warn("鏃犲彲鐢ㄥ垵濮嬪寲杩囩▼锛岃妫�鏌ュ垵濮嬪寲鏄惁宸叉墽琛屽畬鎴�") + return nil } sort.Sort(&initializerList) // 淇濊瘉鏈変緷璧栫殑 initializer 鎺掑湪鍚庨潰鎵ц diff --git a/source/menu.go b/source/menu.go index 18ed3aa..08b1403 100644 --- a/source/menu.go +++ b/source/menu.go @@ -1,52 +1,53 @@ package system -import ( - "aps_crm/model" - "aps_crm/pkg/logx" - "aps_crm/service" - "context" - "github.com/pkg/errors" - "gorm.io/gorm" -) - -type initMenu struct{} - -// auto run -func init() { - service.RegisterInit(initMenuSequence, &initMenu{}) -} - -func (i initMenu) InitializerName() string { - return model.Menu{}.TableName() -} - -func (i *initMenu) InitializeData(ctx context.Context) (next context.Context, err error) { - entities := []*model.Menu{ - {ID: 1, ParentId: 0, Path: "", Name: "", Title: "瀹㈡埛绠$悊"}, - {ID: 2, ParentId: 1, Path: "/client", Name: "", Title: "瀹㈡埛绠$悊"}, - - {ID: 3, ParentId: 0, Path: "", Name: "", Title: "閿�鍞鐞�"}, - {ID: 4, ParentId: 3, Path: "/saleChance", Name: "", Title: "閿�鍞満浼�"}, - - {ID: 5, ParentId: 0, Path: "", Name: "", Title: "鏈嶅姟绠$悊"}, - {ID: 6, ParentId: 5, Path: "/serviceContract", Name: "", Title: "鏈嶅姟鍚堝悓"}, - - {ID: 7, ParentId: 0, Path: "", Name: "", Title: "鍚庡彴璁剧疆"}, - {ID: 8, ParentId: 7, Path: "/member", Name: "", Title: "鎴愬憳绠$悊"}, - {ID: 9, ParentId: 7, Path: "/role", Name: "", Title: "瑙掕壊绠$悊"}, - } - if err = model.NewMenuSearch(nil).CreateBatch(entities); err != nil { - return ctx, errors.Wrap(err, i.InitializerName()+"琛ㄦ暟鎹垵濮嬪寲澶辫触!") - } - next = context.WithValue(ctx, i.InitializerName(), entities) - logx.Infof("InitializeData success initName:%v", i.InitializerName()) - return next, nil -} - -func (i *initMenu) DataInserted(ctx context.Context) bool { - _, err := model.NewMenuSearch(nil).SetId(22).First() - if errors.Is(err, gorm.ErrRecordNotFound) { // 鍒ゆ柇鏄惁瀛樺湪鏁版嵁 - return false - } - return true -} +// +//import ( +// "aps_crm/model" +// "aps_crm/pkg/logx" +// "aps_crm/service" +// "context" +// "github.com/pkg/errors" +// "gorm.io/gorm" +//) +// +//type initMenu struct{} +// +//// auto run +//func init() { +// service.RegisterInit(initMenuSequence, &initMenu{}) +//} +// +//func (i initMenu) InitializerName() string { +// return model.Menu{}.TableName() +//} +// +//func (i *initMenu) InitializeData(ctx context.Context) (next context.Context, err error) { +// entities := []*model.Menu{ +// {ID: 1, ParentId: 0, Path: "", Name: "", Title: "瀹㈡埛绠$悊"}, +// {ID: 2, ParentId: 1, Path: "/client", Name: "", Title: "瀹㈡埛绠$悊"}, +// +// {ID: 3, ParentId: 0, Path: "", Name: "", Title: "閿�鍞鐞�"}, +// {ID: 4, ParentId: 3, Path: "/saleChance", Name: "", Title: "閿�鍞満浼�"}, +// +// {ID: 5, ParentId: 0, Path: "", Name: "", Title: "鏈嶅姟绠$悊"}, +// {ID: 6, ParentId: 5, Path: "/serviceContract", Name: "", Title: "鏈嶅姟鍚堝悓"}, +// +// {ID: 7, ParentId: 0, Path: "", Name: "", Title: "鍚庡彴璁剧疆"}, +// {ID: 8, ParentId: 7, Path: "/member", Name: "", Title: "鎴愬憳绠$悊"}, +// {ID: 9, ParentId: 7, Path: "/role", Name: "", Title: "瑙掕壊绠$悊"}, +// } +// if err = model.NewMenuSearch(nil).CreateBatch(entities); err != nil { +// return ctx, errors.Wrap(err, i.InitializerName()+"琛ㄦ暟鎹垵濮嬪寲澶辫触!") +// } +// next = context.WithValue(ctx, i.InitializerName(), entities) +// logx.Infof("InitializeData success initName:%v", i.InitializerName()) +// return next, nil +//} +// +//func (i *initMenu) DataInserted(ctx context.Context) bool { +// _, err := model.NewMenuSearch(nil).SetId(22).First() +// if errors.Is(err, gorm.ErrRecordNotFound) { // 鍒ゆ柇鏄惁瀛樺湪鏁版嵁 +// return false +// } +// return true +//} diff --git a/source/user.go b/source/user.go index b8e334e..c3c3db8 100644 --- a/source/user.go +++ b/source/user.go @@ -1,68 +1,68 @@ package system -import ( - "aps_crm/constvar" - "aps_crm/model" - "aps_crm/pkg/encrypt" - "aps_crm/pkg/logx" - "aps_crm/pkg/snowflake" - "aps_crm/service" - "context" - "fmt" - "github.com/pkg/errors" - "gorm.io/gorm" -) - -const ( - initMenuSequence = iota + 1 - initUserSequence -) - -type initUser struct{} - -// auto run -func init() { - service.RegisterInit(initUserSequence, &initUser{}) -} - -func (i initUser) InitializerName() string { - return model.User{}.TableName() -} - -func (i *initUser) InitializeData(ctx context.Context) (next context.Context, err error) { - adminPassword := encrypt.BcryptHash("123456") - entities := []*model.User{ - { - UUID: fmt.Sprintf("u%v", snowflake.GenerateId()), - Username: "admin", - Password: adminPassword, - NickName: "admin", - UserType: constvar.UserTypeSuper, - //ParentId: "basic", // 瓒呯骇绠$悊鍛樿处鎴风殑鐖朵翰涓虹┖瀛楃涓诧紝鎴栬�呰捣涓悕瀛� - //Enable: true, - }, - } - if err = model.NewUserSearch(nil).CreateBatch(entities); err != nil { - return ctx, errors.Wrap(err, model.User{}.TableName()+"琛ㄦ暟鎹垵濮嬪寲澶辫触!") - } - next = context.WithValue(ctx, i.InitializerName(), entities) - - menuEntities, ok := ctx.Value(initMenu{}.InitializerName()).([]*model.Menu) - if !ok { - return next, errors.New("no find menus") - } - - if err = model.NewUserSearch(nil).ReplaceMenu(entities[0], menuEntities); err != nil { - return next, err - } - logx.Infof("InitializeData success initName:%v", i.InitializerName()) - return next, err -} - -func (i *initUser) DataInserted(ctx context.Context) bool { - _, err := model.NewUserSearch(nil).SetUserName("admin").First() - if errors.Is(err, gorm.ErrRecordNotFound) { // 鍒ゆ柇鏄惁瀛樺湪鏁版嵁 - return false - } - return true -} +//import ( +// "aps_crm/constvar" +// "aps_crm/model" +// "aps_crm/pkg/encrypt" +// "aps_crm/pkg/logx" +// "aps_crm/pkg/snowflake" +// "aps_crm/service" +// "context" +// "fmt" +// "github.com/pkg/errors" +// "gorm.io/gorm" +//) +// +//const ( +// initMenuSequence = iota + 1 +// initUserSequence +//) +// +//type initUser struct{} +// +//// auto run +//func init() { +// service.RegisterInit(initUserSequence, &initUser{}) +//} +// +//func (i initUser) InitializerName() string { +// return model.User{}.TableName() +//} +// +//func (i *initUser) InitializeData(ctx context.Context) (next context.Context, err error) { +// adminPassword := encrypt.BcryptHash("123456") +// entities := []*model.User{ +// { +// UUID: fmt.Sprintf("u%v", snowflake.GenerateId()), +// Username: "admin", +// Password: adminPassword, +// NickName: "admin", +// UserType: constvar.UserTypeSuper, +// //ParentId: "basic", // 瓒呯骇绠$悊鍛樿处鎴风殑鐖朵翰涓虹┖瀛楃涓诧紝鎴栬�呰捣涓悕瀛� +// //Enable: true, +// }, +// } +// if err = model.NewUserSearch(nil).CreateBatch(entities); err != nil { +// return ctx, errors.Wrap(err, model.User{}.TableName()+"琛ㄦ暟鎹垵濮嬪寲澶辫触!") +// } +// next = context.WithValue(ctx, i.InitializerName(), entities) +// +// menuEntities, ok := ctx.Value(initMenu{}.InitializerName()).([]*model.Menu) +// if !ok { +// return next, errors.New("no find menus") +// } +// +// if err = model.NewUserSearch(nil).ReplaceMenu(entities[0], menuEntities); err != nil { +// return next, err +// } +// logx.Infof("InitializeData success initName:%v", i.InitializerName()) +// return next, err +//} +// +//func (i *initUser) DataInserted(ctx context.Context) bool { +// _, err := model.NewUserSearch(nil).SetUserName("admin").First() +// if errors.Is(err, gorm.ErrRecordNotFound) { // 鍒ゆ柇鏄惁瀛樺湪鏁版嵁 +// return false +// } +// return true +//} -- Gitblit v1.8.0