pg_partner_info_repository.go
4.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package repository
import (
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"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 PartnerInfoRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *PartnerInfoRepository) Save(dm *domain.PartnerInfo) (*domain.PartnerInfo, error) {
var (
err error
m = &models.PartnerInfo{}
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("id").Insert(m); err != nil {
return nil, err
}
return dm, nil
}
if _, err = tx.Model(m).Where("id=?", m.Id).Update(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *PartnerInfoRepository) Remove(PartnerInfo *domain.PartnerInfo) (*domain.PartnerInfo, error) {
var (
tx = repository.transactionContext.PgTx
PartnerInfoModel = &models.PartnerInfo{Id: PartnerInfo.Identify().(int64)}
)
if _, err := tx.Model(PartnerInfoModel).Where("id = ?", PartnerInfo.Id).Delete(); err != nil {
return PartnerInfo, err
}
return PartnerInfo, nil
}
func (repository *PartnerInfoRepository) FindOne(queryOptions map[string]interface{}) (*domain.PartnerInfo, error) {
tx := repository.transactionContext.PgTx
PartnerInfoModel := new(models.PartnerInfo)
query := NewQuery(tx.Model(PartnerInfoModel), queryOptions)
query.SetWhere("partner_info.id = ?", "id")
query.SetWhere("partner_info.account = ?", "account")
query.SetWhere("partner_info.status = ?", "status")
query.SetWhere("partner_info.company_id = ?", "companyId")
query.SetWhere("partner_info.company_id <> ?", "isNot")
//if inParterIds,ok :=queryOptions["inParterIds"];ok{
// query.Relation("PartnerInfo", func(q *orm.Query) (*orm.Query, error) {
// return q.Where("id in (?)",pg.In(inParterIds.([]int64))),nil
// })
//}
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此合伙人")
}
if PartnerInfoModel.Id == 0 {
return nil, nil
}
return repository.transformPgModelToDomainModel(PartnerInfoModel)
}
func (repository *PartnerInfoRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.PartnerInfo, error) {
tx := repository.transactionContext.PgTx
var PartnerInfoModels []*models.PartnerInfo
PartnerInfos := make([]*domain.PartnerInfo, 0)
query := NewQuery(tx.Model(&PartnerInfoModels), queryOptions)
query.
SetWhere("partner_info.account = ?", "account").
SetWhere(`partner_info.status = ?`, "status").
SetWhere(`partner_info.partner_category = ?`, "partnerCategory").
SetWhere("partner_info.company_id <> ?", "isNot").
SetLimit().
SetOrder("partner_info.create_at", "sortByCreateTime").
SetOrder("partner_info.update_at", "sortByUpdateTime")
if inPartnerIds, ok := queryOptions["inPartnerIds"]; ok {
query.Where("id in (?)", pg.In(inPartnerIds.([]int64)))
}
// 合伙人类型
if inPartnerCategory, ok := queryOptions["inPartnerCategory"]; ok {
query.Query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
if arrayInPartnerCategory, ok := inPartnerCategory.([]int64); ok {
for i := range arrayInPartnerCategory {
q.WhereOr(`partner_info.partner_category_infos @>'[{"id":?}]')`, arrayInPartnerCategory[i])
}
}
return q, nil
})
}
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, PartnerInfos, err
}
for _, PartnerInfoModel := range PartnerInfoModels {
if PartnerInfo, err := repository.transformPgModelToDomainModel(PartnerInfoModel); err != nil {
return 0, PartnerInfos, err
} else {
PartnerInfos = append(PartnerInfos, PartnerInfo)
}
}
return int64(query.AffectRow), PartnerInfos, nil
}
func (repository *PartnerInfoRepository) transformPgModelToDomainModel(PartnerInfoModel *models.PartnerInfo) (*domain.PartnerInfo, error) {
m := &domain.PartnerInfo{}
err := GobModelTransform(m, PartnerInfoModel)
return m, err
}
func NewPartnerInfoRepository(transactionContext *transaction.TransactionContext) (*PartnerInfoRepository, error) {
if transactionContext == nil {
return nil, ERR_EMPTY_TC
}
return &PartnerInfoRepository{transactionContext: transactionContext}, nil
}