/
b_demo.go
114 lines (95 loc) · 2.74 KB
/
b_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package bll
import (
"context"
"github.com/tanjiancheng/gin-amis-admin/internal/app/bll"
"github.com/tanjiancheng/gin-amis-admin/internal/app/iutil"
"github.com/tanjiancheng/gin-amis-admin/internal/app/model"
"github.com/tanjiancheng/gin-amis-admin/internal/app/schema"
"github.com/tanjiancheng/gin-amis-admin/pkg/errors"
"github.com/google/wire"
)
var _ bll.IDemo = (*Demo)(nil)
// DemoSet 注入Demo
var DemoSet = wire.NewSet(wire.Struct(new(Demo), "*"), wire.Bind(new(bll.IDemo), new(*Demo)))
// Demo 示例程序
type Demo struct {
DemoModel model.IDemo
}
// Query 查询数据
func (a *Demo) Query(ctx context.Context, params schema.DemoQueryParam, opts ...schema.DemoQueryOptions) (*schema.DemoQueryResult, error) {
return a.DemoModel.Query(ctx, params, opts...)
}
// Get 查询指定数据
func (a *Demo) Get(ctx context.Context, id string, opts ...schema.DemoQueryOptions) (*schema.Demo, error) {
item, err := a.DemoModel.Get(ctx, id, opts...)
if err != nil {
return nil, err
} else if item == nil {
return nil, errors.ErrNotFound
}
return item, nil
}
func (a *Demo) checkCode(ctx context.Context, code string) error {
result, err := a.DemoModel.Query(ctx, schema.DemoQueryParam{
PaginationParam: schema.PaginationParam{
OnlyCount: true,
},
Code: code,
})
if err != nil {
return err
} else if result.PageResult.Total > 0 {
return errors.New400Response("编号已经存在")
}
return nil
}
// Create 创建数据
func (a *Demo) Create(ctx context.Context, item schema.Demo) (*schema.IDResult, error) {
err := a.checkCode(ctx, item.Code)
if err != nil {
return nil, err
}
item.ID = iutil.NewID()
err = a.DemoModel.Create(ctx, item)
if err != nil {
return nil, err
}
return schema.NewIDResult(item.ID), nil
}
// Update 更新数据
func (a *Demo) Update(ctx context.Context, id string, item schema.Demo) error {
oldItem, err := a.DemoModel.Get(ctx, id)
if err != nil {
return err
} else if oldItem == nil {
return errors.ErrNotFound
} else if oldItem.Code != item.Code {
if err := a.checkCode(ctx, item.Code); err != nil {
return err
}
}
item.ID = oldItem.ID
item.Creator = oldItem.Creator
item.CreatedAt = oldItem.CreatedAt
return a.DemoModel.Update(ctx, id, item)
}
// Delete 删除数据
func (a *Demo) Delete(ctx context.Context, id string) error {
oldItem, err := a.DemoModel.Get(ctx, id)
if err != nil {
return err
} else if oldItem == nil {
return errors.ErrNotFound
}
return a.DemoModel.Delete(ctx, id)
}
// UpdateStatus 更新状态
func (a *Demo) UpdateStatus(ctx context.Context, id string, status int) error {
oldItem, err := a.DemoModel.Get(ctx, id)
if err != nil {
return err
} else if oldItem == nil {
return errors.ErrNotFound
}
return a.DemoModel.UpdateStatus(ctx, id, status)
}