-
Notifications
You must be signed in to change notification settings - Fork 12
/
m_demo.go
99 lines (82 loc) · 2.78 KB
/
m_demo.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package model
import (
"context"
"github.com/tanjiancheng/gin-amis-admin/internal/app/model"
"github.com/tanjiancheng/gin-amis-admin/internal/app/model/impl/gorm/entity"
"github.com/tanjiancheng/gin-amis-admin/internal/app/schema"
"github.com/tanjiancheng/gin-amis-admin/pkg/errors"
"github.com/google/wire"
"github.com/jinzhu/gorm"
)
var _ model.IDemo = (*Demo)(nil)
// DemoSet 注入Demo
var DemoSet = wire.NewSet(wire.Struct(new(Demo), "*"), wire.Bind(new(model.IDemo), new(*Demo)))
// Demo 示例存储
type Demo struct {
DB *gorm.DB
}
func (a *Demo) getQueryOption(opts ...schema.DemoQueryOptions) schema.DemoQueryOptions {
var opt schema.DemoQueryOptions
if len(opts) > 0 {
opt = opts[0]
}
return opt
}
// Query 查询数据
func (a *Demo) Query(ctx context.Context, params schema.DemoQueryParam, opts ...schema.DemoQueryOptions) (*schema.DemoQueryResult, error) {
opt := a.getQueryOption(opts...)
db := entity.GetDemoDB(ctx, a.DB)
if v := params.Code; v != "" {
db = db.Where("code=?", v)
}
if v := params.QueryValue; v != "" {
v = "%" + v + "%"
db = db.Where("code LIKE ? OR name LIKE ? OR memo LIKE ?", v, v, v)
}
opt.OrderFields = append(opt.OrderFields, schema.NewOrderField("id", schema.OrderByDESC))
db = db.Order(ParseOrder(opt.OrderFields))
var list entity.Demos
pr, err := WrapPageQuery(ctx, db, params.PaginationParam, &list)
if err != nil {
return nil, errors.WithStack(err)
}
qr := &schema.DemoQueryResult{
PageResult: pr,
Data: list.ToSchemaDemos(),
}
return qr, nil
}
// Get 查询指定数据
func (a *Demo) Get(ctx context.Context, id string, opts ...schema.DemoQueryOptions) (*schema.Demo, error) {
db := entity.GetDemoDB(ctx, a.DB).Where("id=?", id)
var item entity.Demo
ok, err := FindOne(ctx, db, &item)
if err != nil {
return nil, errors.WithStack(err)
} else if !ok {
return nil, nil
}
return item.ToSchemaDemo(), nil
}
// Create 创建数据
func (a *Demo) Create(ctx context.Context, item schema.Demo) error {
eitem := entity.SchemaDemo(item).ToDemo()
result := entity.GetDemoDB(ctx, a.DB).Create(eitem)
return errors.WithStack(result.Error)
}
// Update 更新数据
func (a *Demo) Update(ctx context.Context, id string, item schema.Demo) error {
eitem := entity.SchemaDemo(item).ToDemo()
result := entity.GetDemoDB(ctx, a.DB).Where("id=?", id).Updates(eitem)
return errors.WithStack(result.Error)
}
// Delete 删除数据
func (a *Demo) Delete(ctx context.Context, id string) error {
result := entity.GetDemoDB(ctx, a.DB).Where("id=?", id).Delete(entity.Demo{})
return errors.WithStack(result.Error)
}
// UpdateStatus 更新状态
func (a *Demo) UpdateStatus(ctx context.Context, id string, status int) error {
result := entity.GetDemoDB(ctx, a.DB).Where("id=?", id).Update("status", status)
return errors.WithStack(result.Error)
}