pg_users_repository.go
6.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package repository
import (
"errors"
"fmt"
"time"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type UsersRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.UsersRepository = (*UsersRepository)(nil)
)
func NewUsersRepository(transactionContext *transaction.TransactionContext) (*UsersRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &UsersRepository{transactionContext: transactionContext}, nil
}
func (repository UsersRepository) transformPgModelToDomainModel(m *models.Users) (domain.Users, error) {
return domain.Users{
Id: m.Id,
CompanyId: m.CompanyId,
OpenId: m.OpenId,
Name: m.Name,
Sex: m.Sex,
JobNum: m.JobNum,
Phone: m.Phone,
PrivatePhone: m.PrivatePhone,
Email: m.Email,
ExtensionNum: m.ExtensionNum,
EntryTime: m.EntryTime,
Workspace: m.Workspace,
Status: m.Status,
Avatar: m.Avatar,
Remarks: m.Remarks,
ChargeStatus: m.ChargeStatus,
CreateAt: m.CreateAt,
UpdateAt: m.UpdateAt,
Permission: m.Permission,
AccessPartners: m.AccessPartners,
AdminType: m.AdminType,
IsSenior: m.IsSenior,
}, nil
}
func (reponsitory UsersRepository) Add(u *domain.Users) error {
var (
err error
tx = reponsitory.transactionContext.PgTx
)
m := &models.Users{
Id: u.Id,
CompanyId: u.CompanyId,
OpenId: u.OpenId,
Name: u.Name,
Sex: u.Sex,
JobNum: u.JobNum,
Phone: u.Phone,
PrivatePhone: u.PrivatePhone,
Email: u.Email,
ExtensionNum: u.ExtensionNum,
EntryTime: u.EntryTime,
Workspace: u.Workspace,
Status: u.Status,
Avatar: u.Avatar,
Remarks: u.Remarks,
ChargeStatus: u.ChargeStatus,
CreateAt: u.CreateAt,
UpdateAt: u.UpdateAt,
Permission: u.Permission,
AccessPartners: u.AccessPartners,
AdminType: u.AdminType,
IsSenior: u.IsSenior,
}
_, err = tx.Model(m).Insert()
return err
}
func (reponsitory UsersRepository) Edit(u *domain.Users) error {
var (
err error
tx = reponsitory.transactionContext.PgTx
)
m := &models.Users{
Id: u.Id,
CompanyId: u.CompanyId,
OpenId: u.OpenId,
Name: u.Name,
Sex: u.Sex,
JobNum: u.JobNum,
Phone: u.Phone,
PrivatePhone: u.PrivatePhone,
Email: u.Email,
ExtensionNum: u.ExtensionNum,
EntryTime: u.EntryTime,
Workspace: u.Workspace,
Status: u.Status,
Avatar: u.Avatar,
Remarks: u.Remarks,
ChargeStatus: u.ChargeStatus,
CreateAt: u.CreateAt,
UpdateAt: u.UpdateAt,
Permission: u.Permission,
AccessPartners: u.AccessPartners,
AdminType: u.AdminType,
IsSenior: u.IsSenior,
}
_, err = tx.Model(m).WherePK().Update()
return err
}
func (reponsitory UsersRepository) FindOne(queryOptions domain.UsersFindOneQuery) (domain.Users, error) {
var (
err error
tx = reponsitory.transactionContext.PgTx
m models.Users
hasCondition bool
)
query := tx.Model(&m)
query = query.Where("delete_at ISNULL")
if queryOptions.Id > 0 {
hasCondition = true
query = query.Where("id=?", queryOptions.Id)
}
if len(queryOptions.Phone) > 0 {
hasCondition = true
query = query.Where("phone=?", queryOptions.Phone)
}
if queryOptions.CompanyId > 0 {
hasCondition = true
query = query.Where("company_id=?", queryOptions.CompanyId)
}
if queryOptions.OpenId > 0 {
hasCondition = true
query = query.Where("open_id=?", queryOptions.OpenId)
}
if !hasCondition {
return domain.Users{}, errors.New("UsersRepository.FindOne 必须要有查询条件")
}
err = query.First()
if err != nil {
return domain.Users{}, err
}
return reponsitory.transformPgModelToDomainModel(&m)
}
//FindOneWithExist 查询一个用户,
//当数据不存在时不返回错误,替代的是返回bool,true表示数据存在,false表示数据不存在
func (reponsitory UsersRepository) FindOneWithExist(queryOptions domain.UsersFindOneQuery) (*domain.Users, bool, error) {
var (
err error
tx = reponsitory.transactionContext.PgTx
m models.Users
hasCondition bool
)
query := tx.Model(&m)
query = query.Where("delete_at ISNULL")
if queryOptions.Id > 0 {
hasCondition = true
query = query.Where("id=?", queryOptions.Id)
}
if len(queryOptions.Phone) > 0 {
hasCondition = true
query = query.Where("phone=?", queryOptions.Phone)
}
if queryOptions.CompanyId > 0 {
hasCondition = true
query = query.Where("company_id=?", queryOptions.CompanyId)
}
if queryOptions.OpenId > 0 {
hasCondition = true
query = query.Where("open_id=?", queryOptions.OpenId)
}
if !hasCondition {
return nil, false, errors.New("UsersRepository.FindOne 必须要有查询条件")
}
err = query.First()
if err != nil && err == pg.ErrNoRows {
return nil, false, nil
}
if err != nil {
return nil, false, err
}
searchResult, err := reponsitory.transformPgModelToDomainModel(&m)
return &searchResult, true, err
}
func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int, []domain.Users, error) {
db := reponsitory.transactionContext.PgTx
usersModels := []models.Users{}
query := db.Model(&usersModels)
query.Where("delete_at ISNULL")
if queryOption.CompanyId > 0 {
query = query.Where("company_id=?", queryOption.CompanyId)
}
if queryOption.ChargeStatus > 0 {
query = query.Where("charge_status=?", queryOption.ChargeStatus)
}
if queryOption.AdminType > 0 {
query = query.Where("admin_type=?", queryOption.AdminType)
}
if len(queryOption.UserNameMatch) > 0 {
query = query.Where("name like ?", "%"+queryOption.UserNameMatch+"%")
}
if queryOption.Offset >= 0 {
query = query.Offset(queryOption.Offset)
}
if queryOption.Limit > 0 {
query = query.Limit(queryOption.Limit)
} else {
query = query.Limit(1000)
}
var (
err error
usersReturn = make([]domain.Users, 0)
cnt int
)
//query = query.Order("id DESC")
query = query.Order("admin_type DESC")
cnt, err = query.SelectAndCount()
if err != nil {
return 0, usersReturn, err
}
for i := range usersModels {
domainUsers, err := reponsitory.transformPgModelToDomainModel(&usersModels[i])
if err != nil {
return 0, usersReturn, err
}
usersReturn = append(usersReturn, domainUsers)
}
return cnt, usersReturn, nil
}
func (reponsitory UsersRepository) Remove(ids []int64) error {
if len(ids) == 0 {
return nil
}
var (
err error
tx = reponsitory.transactionContext.PgTx
)
_, err = tx.Model(&models.Users{}).
Set("delete_at=?", time.Now()).
WhereIn("id in(?)", ids).
Update()
return err
}