pg_partner_info_repository.go
6.1 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
package repository
import (
"errors"
"fmt"
"github.com/go-pg/pg/v10/orm"
"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 PartnerInfoRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.PartnerInfoRepository = (*PartnerInfoRepository)(nil)
)
func (repository *PartnerInfoRepository) transformPgModelToDomainModel(partnerInfoModel *models.PartnerInfo) (domain.PartnerInfo, error) {
m := domain.PartnerInfo{
Partner: domain.Partner{
Id: partnerInfoModel.Id,
PartnerName: partnerInfoModel.PartnerName,
Account: partnerInfoModel.Account,
},
PartnerCategory: partnerInfoModel.PartnerCategory,
Password: partnerInfoModel.Password,
Status: partnerInfoModel.Status,
CreateAt: partnerInfoModel.CreateAt,
Salesman: partnerInfoModel.Salesman,
RegionInfo: partnerInfoModel.RegionInfo,
CooperateTime: partnerInfoModel.CooperateTime,
CompanyId: partnerInfoModel.CompanyId,
}
p := []domain.PartnerCategory{}
for _, v := range partnerInfoModel.PartnerCategoryInfos {
catagory := domain.PartnerCategory{
Id: v.Id,
}
p = append(p, catagory)
}
m.PartnerCategoryInfos = p
return m, nil
}
func NewPartnerInfoRepository(transactionContext *transaction.TransactionContext) (*PartnerInfoRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &PartnerInfoRepository{transactionContext: transactionContext}, nil
}
func (repository *PartnerInfoRepository) Save(dm *domain.PartnerInfo) error {
var (
err error
tx = repository.transactionContext.PgTx
)
categorys := []domain.PartnerCategory{}
for _, v := range dm.PartnerCategoryInfos {
categorys = append(categorys, domain.PartnerCategory{
Id: v.Id,
})
}
m := &models.PartnerInfo{
Id: dm.Partner.Id,
PartnerName: dm.Partner.PartnerName,
Account: dm.Partner.Account,
Password: dm.Password,
Status: dm.Status,
PartnerCategory: dm.PartnerCategory,
Salesman: dm.Salesman,
RegionInfo: dm.RegionInfo,
CooperateTime: dm.CooperateTime,
CompanyId: dm.CompanyId,
PartnerCategoryInfos: categorys,
}
if m.Id == 0 {
err = tx.Insert(m)
dm.Partner.Id = m.Id
if err != nil {
return err
}
} else {
_, err = tx.Model(m).WherePK().
Column("partner_name", "account", "password", "status", "partner_category", "salesman",
"region_info", "cooperate_time", "update_at", "partner_category_infos").
Update()
if err != nil {
return err
}
}
return nil
}
func (repository *PartnerInfoRepository) FindOne(queryOptions domain.PartnerFindOneQuery) (*domain.PartnerInfo, error) {
tx := repository.transactionContext.PgTx
PartnerInfoModel := new(models.PartnerInfo)
var hasCondition bool
query := tx.Model(PartnerInfoModel)
if len(queryOptions.AccountEqual) > 0 {
hasCondition = true
query = query.Where("account=?", queryOptions.AccountEqual)
}
if queryOptions.UserId > 0 {
hasCondition = true
query = query.Where("id=?", queryOptions.UserId)
}
if queryOptions.CompanyId > 0 {
hasCondition = true
query = query.Where("company_id=?", queryOptions.CompanyId)
}
if !hasCondition {
return nil, errors.New("FindOne 必须要有查询条件")
}
if err := query.First(); err != nil {
return nil, err
}
result, err := repository.transformPgModelToDomainModel(PartnerInfoModel)
return &result, err
}
func (repository *PartnerInfoRepository) Find(queryOption domain.PartnerFindQuery) ([]domain.PartnerInfo, error) {
db := repository.transactionContext.PgTx
partnerModels := []models.PartnerInfo{}
query := db.Model(&partnerModels)
if len(queryOption.PartnerName) > 0 {
query = query.Where("partner_name like ?", "%"+queryOption.PartnerName+"%")
}
if queryOption.RegionInfo != nil {
query = query.Where("region_info::jsonb->>'regionName' like ?", "%"+queryOption.RegionInfo.RegionName+"%")
}
//合伙人类型
if len(queryOption.PartnerCategory) > 0 {
query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
for _, value := range queryOption.PartnerCategory {
q = q.WhereOr(`partner_category_infos@> '[{"id":?}]'`, value)
}
return q, nil
})
}
if queryOption.CompanyId > 0 {
query = query.Where("company_id=?", queryOption.CompanyId)
}
if len(queryOption.Ids) > 0 {
query = query.WhereIn("id in(?)", queryOption.Ids)
}
if queryOption.Offset > -1 {
query = query.Offset(queryOption.Offset)
}
if queryOption.Limit > 0 {
query = query.Limit(queryOption.Limit)
} else {
query = query.Limit(1000)
}
var (
err error
partnerReturn = make([]domain.PartnerInfo, 0)
)
query = query.Order("id DESC")
err = query.Select()
if err != nil {
return partnerReturn, err
}
for i := range partnerModels {
domainPartner, err := repository.transformPgModelToDomainModel(&partnerModels[i])
if err != nil {
return partnerReturn, err
}
partnerReturn = append(partnerReturn, domainPartner)
}
return partnerReturn, nil
}
func (repository PartnerInfoRepository) CountAll(queryOption domain.PartnerFindQuery) (int, error) {
db := repository.transactionContext.PgTx
partnerModels := models.PartnerInfo{}
query := db.Model(&partnerModels)
if len(queryOption.PartnerName) > 0 {
query = query.Where("partner_name like ?", "%"+queryOption.PartnerName+"%")
}
if queryOption.RegionInfo != nil {
query = query.Where("region_info::jsonb->>'regionName' like ?", "%"+queryOption.RegionInfo.RegionName+"%")
}
//合伙人类型
if len(queryOption.PartnerCategory) > 0 {
query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
for _, value := range queryOption.PartnerCategory {
q = q.WhereOr(`partner_category_infos@> '[{"id":?}]'`, value)
}
return q, nil
})
}
if queryOption.CompanyId > 0 {
query = query.Where("company_id=?", queryOption.CompanyId)
}
if len(queryOption.Ids) > 0 {
query = query.WhereIn("id in(?)", queryOption.Ids)
}
cnt, err := query.Count()
return cnt, err
}