| | |
| | | import ( |
| | | "aps_crm/pkg/logx" |
| | | "context" |
| | | "errors" |
| | | "sort" |
| | | ) |
| | | |
| | |
| | | // InitDB 初始化 |
| | | func (initDBService *InitDBService) InitDB() (err error) { |
| | | if len(initializerList) == 0 { |
| | | return errors.New("无可用初始化过程,请检查初始化是否已执行完成") |
| | | logx.Warn("无可用初始化过程,请检查初始化是否已执行完成") |
| | | return nil |
| | | } |
| | | sort.Sort(&initializerList) // 保证有依赖的 initializer 排在后面执行 |
| | | |
| | |
| | | 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 |
| | | //} |
| | |
| | | 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 |
| | | //} |