pg_admin_user_repository.go
4.5 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
package repository
import (
"fmt"
"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 AdminUserRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.AdminUserRepository = (*AdminUserRepository)(nil)
)
func NewAdminUserRepository(transactionContext *transaction.TransactionContext) (*AdminUserRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &AdminUserRepository{transactionContext: transactionContext}, nil
}
func (reponsitory AdminUserRepository) transformPgModelToDomainModel(adminuserModel *models.AdminUser) (domain.AdminUser, error) {
result := domain.AdminUser{
Id: adminuserModel.Id,
Account: adminuserModel.Account,
AdminName: adminuserModel.AdminName,
IsDefault: adminuserModel.IsDefault,
CreateAt: adminuserModel.CreateAt,
IsUsable: adminuserModel.IsUsable,
Password: adminuserModel.Password,
Permission: adminuserModel.Permission,
}
return result, nil
}
func (reponsitory AdminUserRepository) FindOne(queryOption domain.AdminUserFindOneQuery) (*domain.AdminUser, error) {
db := reponsitory.transactionContext.PgDd
adminuserModel := new(models.AdminUser)
query := db.Model(adminuserModel)
if queryOption.AdminUserId > 0 {
query = query.Where("id=?", queryOption.AdminUserId)
}
if len(queryOption.AccountEqual) > 0 {
query = query.Where("account=?", queryOption.AccountEqual)
}
err := query.First()
if err != nil {
return nil, err
}
adminUser, err := reponsitory.transformPgModelToDomainModel(adminuserModel)
return &adminUser, err
}
func (reponsitory AdminUserRepository) updateAdminUser(adminuser domain.AdminUser) (*domain.AdminUser, error) {
tx := reponsitory.transactionContext.PgTx
adminUserModel := &models.AdminUser{
Id: adminuser.Id,
Account: adminuser.Account,
AdminName: adminuser.AdminName,
Password: adminuser.Password,
IsUsable: adminuser.IsUsable,
Permission: adminuser.Permission,
}
_, err := tx.Model(adminUserModel).
Where("id=?", adminUserModel.Id).
Column("admin_name", "account", "password", "is_usable", "permission").
Update()
if err != nil {
return nil, fmt.Errorf("更新用户数据失败:%s", err)
}
return &adminuser, nil
}
func (reponsitory AdminUserRepository) addAdminUser(adminuser domain.AdminUser) (*domain.AdminUser, error) {
tx := reponsitory.transactionContext.PgTx
adminuserModel := &models.AdminUser{
Account: adminuser.Account,
AdminName: adminuser.AdminName,
Password: adminuser.Password,
IsDefault: false,
IsUsable: adminuser.IsUsable,
Permission: adminuser.Permission,
}
//添加用户数据
_, err := tx.Model(adminuserModel).Insert()
if err != nil {
return nil, err
}
return nil, nil
}
func (reponsitory AdminUserRepository) Save(adminuser domain.AdminUser) (*domain.AdminUser, error) {
if adminuser.Id == 0 {
return reponsitory.addAdminUser(adminuser)
}
return reponsitory.updateAdminUser(adminuser)
}
func (reponsitory AdminUserRepository) Find(queryOption domain.AdminUserFindQuery) ([]domain.AdminUser, error) {
db := reponsitory.transactionContext.PgDd
adminuserModels := []models.AdminUser{}
query := db.Model(&adminuserModels)
if len(queryOption.AccountLike) > 0 {
query = query.Where("account like ?", "%"+queryOption.AccountLike+"%")
}
if queryOption.Offset > -1 {
query = query.Offset(queryOption.Offset)
}
if queryOption.Limit > 0 {
query = query.Limit(queryOption.Limit)
} else {
query = query.Limit(20)
}
var (
err error
adminuserReturn = make([]domain.AdminUser, 0)
)
query = query.Order("admin_user.id DESC")
err = query.Select()
if err != nil {
return adminuserReturn, err
}
for i := range adminuserModels {
domainAdminUser, err := reponsitory.transformPgModelToDomainModel(&adminuserModels[i])
if err != nil {
return adminuserReturn, err
}
adminuserReturn = append(adminuserReturn, domainAdminUser)
}
return adminuserReturn, nil
}
func (reponsitory AdminUserRepository) CountAll(queryOption domain.AdminUserFindQuery) (int, error) {
db := reponsitory.transactionContext.PgDd
adminuserModels := models.AdminUser{}
query := db.Model(&adminuserModels)
if len(queryOption.AccountLike) > 0 {
query = query.Where("account like ?", "%"+queryOption.AccountLike+"%")
}
cnt, err := query.Count()
return cnt, err
}