pg_users_repository.go
3.4 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
package repository
import (
"fmt"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
type UsersRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *UsersRepository) Save(dm *domain.Users) (*domain.Users, error) {
var (
err error
m = &models.Users{}
tx = repository.transactionContext.PgTx
)
if err = GobModelTransform(m, dm); err != nil {
return nil, err
}
if dm.Identify() == nil {
if err = tx.Insert(m); err != nil {
return nil, err
}
return dm, nil
}
if err = tx.Update(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *UsersRepository) Remove(Users *domain.Users) (*domain.Users, error) {
var (
tx = repository.transactionContext.PgTx
UsersModel = &models.Users{Id: Users.Identify().(int64)}
)
if _, err := tx.Model(UsersModel).Where("id = ?", Users.Id).Delete(); err != nil {
return Users, err
}
return Users, nil
}
func (repository *UsersRepository) FindOne(queryOptions map[string]interface{}) (*domain.Users, error) {
tx := repository.transactionContext.PgTx
UsersModel := new(models.Users)
query := NewQuery(tx.Model(UsersModel), queryOptions)
query.SetWhere("id = ?", "id")
query.SetWhere("open_id = ?", "openId")
query.SetWhere("status = ?", "status")
query.SetWhere("phone = ?", "phone")
query.SetWhere("company_id = ?", "companyId")
query.SetWhere(`delete_at is null`, "deleteAtIsNull")
if err := query.First(); err != nil {
return nil, fmt.Errorf("query row not found")
}
if UsersModel.Id == 0 {
return nil, fmt.Errorf("query row not found")
}
return repository.transformPgModelToDomainModel(UsersModel)
}
func (repository *UsersRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Users, error) {
tx := repository.transactionContext.PgTx
var UsersModels []*models.Users
Userss := make([]*domain.Users, 0)
query := NewQuery(tx.Model(&UsersModels), queryOptions).
SetWhere("id = ?", "id").
SetWhere("phone = ?", "phone").
SetWhere("company_id = ?", "companyId").
SetWhere(`status = ?`, "status").
SetWhere(`delete_at is null`, "deleteAtIsNull").
SetWhere("is_senior = ?", "isSenior").
SetOrder("create_at", "sortByCreateTime").
SetOrder("update_at", "sortByUpdateTime")
if v, ok := queryOptions["inCompanyIds"]; ok {
query.Where("company_id in (?)", pg.In(v))
}
query.SetWhere("admin_type=?", "adminType")
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, Userss, err
}
for _, UsersModel := range UsersModels {
if Users, err := repository.transformPgModelToDomainModel(UsersModel); err != nil {
return 0, Userss, err
} else {
Userss = append(Userss, Users)
}
}
return int64(query.AffectRow), Userss, nil
}
func (repository *UsersRepository) transformPgModelToDomainModel(UsersModel *models.Users) (*domain.Users, error) {
m := &domain.Users{}
err := GobModelTransform(m, UsersModel)
return m, err
}
func NewUsersRepository(transactionContext *transaction.TransactionContext) (*UsersRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &UsersRepository{transactionContext: transactionContext}, nil
}