pg_partner_sub_account_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
package repository
import (
"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 PartnerSubAccountRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *PartnerSubAccountRepository) Save(dm *domain.PartnerSubAccount) (*domain.PartnerSubAccount, error) {
var (
err error
m = &models.PartnerSubAccount{}
tx = repository.transactionContext.PgTx
)
if err = GobModelTransform(m, dm); err != nil {
return nil, err
}
if dm.Identify() == nil {
if dm.Id, err = NewSnowflakeId(); err != nil {
return dm, err
}
m.Id = dm.Id
if _, err = tx.Model(m).Returning("*").Insert(m); err != nil {
return nil, err
}
return dm, nil
}
if _, err = tx.Model(m).Update(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *PartnerSubAccountRepository) Remove(PartnerSubAccount *domain.PartnerSubAccount) (*domain.PartnerSubAccount, error) {
var (
tx = repository.transactionContext.PgTx
PartnerSubAccountModel = &models.PartnerSubAccount{Id: PartnerSubAccount.Identify().(int64)}
)
if _, err := tx.Model(PartnerSubAccountModel).Where("id = ?", PartnerSubAccount.Id).Delete(); err != nil {
return PartnerSubAccount, err
}
return PartnerSubAccount, nil
}
func (repository *PartnerSubAccountRepository) FindOne(queryOptions map[string]interface{}) (*domain.PartnerSubAccount, error) {
tx := repository.transactionContext.PgTx
PartnerSubAccountModel := new(models.PartnerSubAccount)
query := NewQuery(tx.Model(PartnerSubAccountModel), queryOptions)
query.SetWhere("id = ?", "id")
query.SetWhere("account = ?", "account")
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此合伙人")
}
if PartnerSubAccountModel.Id == 0 {
return nil, nil
}
return repository.transformPgModelToDomainModel(PartnerSubAccountModel)
}
func (repository *PartnerSubAccountRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.PartnerSubAccount, error) {
tx := repository.transactionContext.PgTx
var PartnerSubAccountModels []*models.PartnerSubAccount
PartnerSubAccounts := make([]*domain.PartnerSubAccount, 0)
query := NewQuery(tx.Model(&PartnerSubAccountModels), queryOptions)
query.
SetWhere("account = ?", "account").
SetLimit()
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, PartnerSubAccounts, err
}
for _, PartnerSubAccountModel := range PartnerSubAccountModels {
if PartnerSubAccount, err := repository.transformPgModelToDomainModel(PartnerSubAccountModel); err != nil {
return 0, PartnerSubAccounts, err
} else {
PartnerSubAccounts = append(PartnerSubAccounts, PartnerSubAccount)
}
}
return int64(query.AffectRow), PartnerSubAccounts, nil
}
func (repository *PartnerSubAccountRepository) transformPgModelToDomainModel(PartnerSubAccountModel *models.PartnerSubAccount) (*domain.PartnerSubAccount, error) {
m := &domain.PartnerSubAccount{}
err := GobModelTransform(m, PartnerSubAccountModel)
return m, err
}
func NewPartnerSubAccountRepository(transactionContext *transaction.TransactionContext) (*PartnerSubAccountRepository, error) {
if transactionContext == nil {
return nil, ERR_EMPTY_TC
}
return &PartnerSubAccountRepository{transactionContext: transactionContext}, nil
}