-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch.go
61 lines (58 loc) · 1.38 KB
/
batch.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
package utils
import (
"github.com/samber/lo"
"github.com/zhanjunjie2019/clover/global/defs"
)
// ConvIDsToUint64 批量将id转为uint64类型
func ConvIDsToUint64(ids []defs.ID) []uint64 {
return lo.Map(ids, func(item defs.ID, index int) uint64 {
return item.UInt64()
})
}
// LoadChangeByArrays 传入2个集合进行比较,最后对比出集合变更项,顺序不敏感。
// newArray:新结果集合;oldArray:原集合;compare:比较方法
func LoadChangeByArrays[T any](newArray, oldArray []T, compare func(newObject, oldObject *T) bool) (inserts, updates, deletes []T) {
// 从新集合中找出新增/修改的对象
var ups map[int]struct{}
for x := range newArray {
newObject := newArray[x]
var isNew = true
for y := range oldArray {
_, ok := ups[y]
if ok {
continue
}
oldObject := oldArray[y]
if compare(&newObject, &oldObject) {
ups[y] = struct{}{}
isNew = false
break
}
}
if isNew {
inserts = append(inserts, newObject)
} else {
updates = append(updates, newObject)
}
}
// 从旧集合中找出删除的对象
for y := range oldArray {
_, ok := ups[y]
if ok {
continue
}
oldObject := oldArray[y]
var isDel = true
for x := range newArray {
newObject := newArray[x]
if compare(&newObject, &oldObject) {
isDel = false
break
}
}
if isDel {
deletes = append(deletes, oldObject)
}
}
return
}