-
Notifications
You must be signed in to change notification settings - Fork 79
/
model.go
163 lines (112 loc) · 2.5 KB
/
model.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
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
package contact
import (
"github.com/opentdp/go-helper/dborm"
"github.com/opentdp/wrest-chat/dbase/tables"
)
// 创建联系人
type CreateParam struct {
Wxid string `json:"wxid" binding:"required"`
Code string `json:"code"`
Remark string `json:"remark"`
Name string `json:"name"`
Country string `json:"country"`
Province string `json:"province"`
City string `json:"city"`
Gender int32 `json:"gender"`
}
func Create(data *CreateParam) (uint, error) {
item := &tables.Contact{
Wxid: data.Wxid,
Code: data.Code,
Remark: data.Remark,
Name: data.Name,
Country: data.Country,
City: data.City,
Gender: data.Gender,
}
result := dborm.Db.Create(item)
return item.Rd, result.Error
}
// 更新联系人
type UpdateParam = CreateParam
func Update(data *UpdateParam) error {
result := dborm.Db.
Where(&tables.Contact{
Wxid: data.Wxid,
}).
Updates(tables.Contact{
Code: data.Code,
Remark: data.Remark,
Name: data.Name,
Country: data.Country,
City: data.City,
Gender: data.Gender,
})
return result.Error
}
// 合并联系人
type ReplaceParam = CreateParam
func Replace(data *ReplaceParam) error {
rq := &FetchParam{
Wxid: data.Wxid,
}
item, err := Fetch(rq)
if err == nil && item.Rd > 0 {
err = Update(data)
} else {
_, err = Create(data)
}
return err
}
// 获取联系人
type FetchParam struct {
Wxid string `json:"wxid" binding:"required"`
}
func Fetch(data *FetchParam) (*tables.Contact, error) {
var item *tables.Contact
result := dborm.Db.
Where(&tables.Contact{
Wxid: data.Wxid,
}).
First(&item)
if item == nil {
item = &tables.Contact{Wxid: data.Wxid}
}
return item, result.Error
}
// 删除联系人
type DeleteParam = FetchParam
func Delete(data *DeleteParam) error {
var item *tables.Contact
result := dborm.Db.
Where(&tables.Contact{
Wxid: data.Wxid,
}).
Delete(&item)
return result.Error
}
// 获取联系人列表
type FetchAllParam struct {
Gender int32 `json:"gender"`
}
func FetchAll(data *FetchAllParam) ([]*tables.Contact, error) {
var items []*tables.Contact
result := dborm.Db.
Where(&tables.Contact{
Gender: data.Gender,
}).
Find(&items)
return items, result.Error
}
// 获取联系人总数
type CountParam = FetchAllParam
func Count(data *CountParam) (int64, error) {
var count int64
result := dborm.Db.
Model(&tables.Contact{}).
Where(&tables.Contact{
Gender: data.Gender,
}).
Count(&count)
return count, result.Error
}