repository.go
3.9 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package domain
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"gitlab.fjmaimaimai.com/allied-creation/su-micro/pkg/transaction"
"reflect"
)
func OffsetLimit(page, size int) (offset int, limit int) {
if page == 0 {
page = 1
}
if size == 0 {
size = 20
}
offset = (page - 1) * size
limit = size
return
}
type QueryOptions map[string]interface{}
func NewQueryOptions() QueryOptions {
options := make(map[string]interface{})
return options
}
func (options QueryOptions) WithOffsetLimit(page, size int) QueryOptions {
offset, limit := OffsetLimit(page, size)
options["offset"] = offset
options["limit"] = limit
return options
}
func (options QueryOptions) WithKV(key string, value interface{}) QueryOptions {
if isEmptyOrZeroValue(value) {
return options
}
options[key] = value
return options
}
func isEmptyOrZeroValue(i interface{}) bool {
if i == nil {
return true // 如果接口为空,返回true
}
// 使用反射判断接口的值是否为零值
v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
default:
return false // 其他类型默认不为空
}
}
func (options QueryOptions) MustWithKV(key string, value interface{}) QueryOptions {
options[key] = value
return options
}
func (options QueryOptions) Copy() QueryOptions {
newOptions := NewQueryOptions()
for k, v := range options {
newOptions[k] = v
}
return newOptions
}
type IndexQueryOptionFunc func() QueryOptions
// 自定义的一些查询条件
func (options QueryOptions) WithCountOnly() QueryOptions {
options["countOnly"] = true
return options
}
func (options QueryOptions) WithFindOnly() QueryOptions {
options["findOnly"] = true
return options
}
func LazyLoad[K comparable, T any](source map[K]T, ctx context.Context, conn transaction.Conn, k K, load func(context.Context, transaction.Conn, K) (T, error)) (T, error) {
if isEmptyOrZeroValue(k) {
var t T
return t, fmt.Errorf("empty key ‘%v’", k)
}
if v, ok := source[k]; ok {
return v, nil
}
if v, err := load(ctx, conn, k); err != nil {
return v, err
} else {
source[k] = v
return v, nil
}
}
func Values[T any, V any](list []T, each func(item T) V) []V {
var result []V
for _, item := range list {
value := each(item)
result = append(result, value)
}
return result
}
func MustValues[T any, V any](list []T, each func(item T) V) []V {
var result []V
for _, item := range list {
value := each(item)
if isEmptyOrZeroValue(value) {
continue
}
result = append(result, value)
}
return result
}
type LazyLoadService[K comparable, T any] struct {
FoundMap map[K]T // 已找到对象Map
NotFoundMap map[K]T // 未找到对象Map
load func(context.Context, transaction.Conn, K) (T, error)
}
func NewLazyLoadService[K comparable, T any](load func(context.Context, transaction.Conn, K) (T, error)) *LazyLoadService[K, T] {
return &LazyLoadService[K, T]{
FoundMap: map[K]T{},
NotFoundMap: map[K]T{},
load: load,
}
}
func (s *LazyLoadService[K, T]) Load(ctx context.Context, conn transaction.Conn, k K) (T, error) {
if isEmptyOrZeroValue(k) {
var t T
return t, fmt.Errorf("empty key ‘%v’", k)
}
if v, ok := s.NotFoundMap[k]; ok {
return v, sqlx.ErrNotFound
}
if v, ok := s.FoundMap[k]; ok {
return v, nil
}
if v, err := s.load(ctx, conn, k); err != nil {
if errors.Is(err, sqlx.ErrNotFound) {
s.NotFoundMap[k] = v
}
return v, err
} else {
s.FoundMap[k] = v
return v, nil
}
}