-
Notifications
You must be signed in to change notification settings - Fork 79
/
model.go
176 lines (125 loc) · 2.84 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
164
165
166
167
168
169
170
171
172
173
174
175
176
package profile
import (
"github.com/opentdp/go-helper/dborm"
"github.com/opentdp/wrest-chat/dbase/tables"
)
// 创建配置
type CreateParam struct {
Rd uint `json:"rd"`
Wxid string `json:"wxid" binding:"required"`
Roomid string `json:"roomid"`
Level int32 `json:"level"`
Remark string `json:"remark"`
AiModel string `json:"ai_model"`
BanExpire int64 `json:"ban_expire"`
}
func Create(data *CreateParam) (uint, error) {
item := &tables.Profile{
Wxid: data.Wxid,
Roomid: data.Roomid,
Level: data.Level,
Remark: data.Remark,
AiModel: data.AiModel,
BanExpire: data.BanExpire,
}
result := dborm.Db.Create(item)
return item.Rd, result.Error
}
// 更新配置
type UpdateParam = CreateParam
func Update(data *UpdateParam) error {
result := dborm.Db.
Where(&tables.Profile{
Rd: data.Rd,
}).
Updates(tables.Profile{
Wxid: data.Wxid,
Roomid: data.Roomid,
Level: data.Level,
Remark: data.Remark,
AiModel: data.AiModel,
BanExpire: data.BanExpire,
})
return result.Error
}
// 合并配置
type ReplaceParam = CreateParam
func Replace(data *ReplaceParam) error {
rq := &FetchParam{Rd: data.Rd}
if rq.Rd == 0 {
rq.Wxid = data.Wxid
rq.Roomid = data.Roomid
}
item, err := Fetch(rq)
if err == nil && item.Rd > 0 {
data.Rd = item.Rd
err = Update(data)
} else {
_, err = Create(data)
}
return err
}
// 获取配置
type FetchParam struct {
Rd uint `json:"rd"`
Wxid string `json:"wxid"`
Roomid string `json:"roomid"`
}
func Fetch(data *FetchParam) (*tables.Profile, error) {
var item *tables.Profile
result := dborm.Db.
Where(&tables.Profile{
Rd: data.Rd,
Wxid: data.Wxid,
Roomid: data.Roomid,
}).
First(&item)
if item == nil {
item = &tables.Profile{Wxid: data.Wxid, Roomid: data.Roomid}
}
return item, result.Error
}
// 删除配置
type DeleteParam = FetchParam
func Delete(data *DeleteParam) error {
var item *tables.Profile
result := dborm.Db.
Where(&tables.Profile{
Rd: data.Rd,
Wxid: data.Wxid,
Roomid: data.Roomid,
}).
Delete(&item)
return result.Error
}
// 获取配置列表
type FetchAllParam struct {
Wxid string `json:"wxid"`
Roomid string `json:"roomid"`
Level int32 `json:"level"`
}
func FetchAll(data *FetchAllParam) ([]*tables.Profile, error) {
var items []*tables.Profile
result := dborm.Db.
Where(&tables.Profile{
Wxid: data.Wxid,
Roomid: data.Roomid,
Level: data.Level,
}).
Find(&items)
return items, result.Error
}
// 获取配置总数
type CountParam = FetchAllParam
func Count(data *CountParam) (int64, error) {
var count int64
result := dborm.Db.
Model(&tables.Profile{}).
Where(&tables.Profile{
Wxid: data.Wxid,
Roomid: data.Roomid,
Level: data.Level,
}).
Count(&count)
return count, result.Error
}