This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
service.go
98 lines (79 loc) · 2.04 KB
/
service.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
package goods
import (
"context"
"strconv"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
"github.com/why444216978/go-util/assert"
"github.com/why444216978/go-util/orm"
"github.com/why444216978/gin-api/app/module/goods/respository"
"github.com/why444216978/gin-api/app/resource"
)
type GoodsInterface interface {
GetGoodsName(ctx context.Context, id int) (string, error)
CrudGoods(ctx context.Context) (goods respository.Test, err error)
}
var Instance GoodsInterface
type GoodsService struct{}
func init() {
Instance = &GoodsService{}
}
const (
GOODS_NAME_KEY = "goods::name::"
GOODS_PRICE_KEY = "goods::price::"
)
func (gs *GoodsService) GetGoodsName(ctx context.Context, id int) (string, error) {
data, err := resource.RedisDefault.Get(ctx, GOODS_NAME_KEY+strconv.Itoa(id)).Result()
if errors.Is(err, redis.Nil) {
return "", nil
}
if err != nil {
return "", errors.Wrap(err, "redis get goods price error:")
}
return data, nil
}
func (gs *GoodsService) CrudGoods(ctx context.Context) (goods respository.Test, err error) {
if assert.IsNil(resource.TestDB) {
err = errors.New("db is nil")
return
}
db := resource.TestDB.DB.WithContext(ctx).Begin()
defer func() {
if err != nil {
db.WithContext(ctx).Rollback()
return
}
err = db.WithContext(ctx).Commit().Error
}()
err = db.WithContext(ctx).Select("*").First(&goods).Error
if err != nil {
return
}
_, err = orm.Insert(ctx, db, &respository.Test{
GoodsId: 333,
Name: "a",
})
if err != nil {
return
}
where := map[string]interface{}{"goods_id": 333}
update := map[string]interface{}{"name": 333}
_, err = orm.Update(ctx, db, &respository.Test{}, where, update)
if err != nil {
return
}
_, err = orm.Delete(ctx, db, &respository.Test{}, where)
if err != nil {
return
}
var name string
err = db.WithContext(ctx).Table("test").Where("id = ?", 1).Select("name").Row().Scan(&name)
if err != nil {
return
}
err = db.WithContext(ctx).Raw("select * from test where id = 1 limit 1").Scan(&goods).Error
if err != nil {
return
}
return
}