add
wangpengfei
2023-07-14 5f00d720ea52ababeb10954da4d33ab6c46e93b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
}