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 }