pg_order_repository.go
5.0 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
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 OrderRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.OrderRepository = (*OrderRepository)(nil)
)
func NewOrderRepository(transactionContext *transaction.TransactionContext) (*OrderRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &OrderRepository{transactionContext: transactionContext}, nil
}
func (reponsitory OrderRepository) transformPgModelToDomainModel(orderModel *models.Order) (domain.Order, error) {
result := domain.Order{
Id: orderModel.Id,
OrderType: orderModel.OrderType,
OrderCode: orderModel.OrderCode,
OrderName: orderModel.OrderName,
OrderStatus: orderModel.OrderStatus,
OrderCount: orderModel.OrderCount,
OrderActualCount: orderModel.OrderActualCount,
OrderAmount: orderModel.OrderAmount,
OrderActualAmount: orderModel.OrderActualAmount,
OrderPaymentAmount: orderModel.OrderPaymentAmount,
OrderRegionInfo: orderModel.OrderRegionInfo,
Buyer: orderModel.Buyer,
PartnerInfo: domain.Partner{
Id: orderModel.PartnerId,
},
PartnerBonusPercent: orderModel.PartnerBonusPercent,
SalesmanBonusPercent: orderModel.SalesmanBonusPercent,
LastViewTime: orderModel.LastViewTime,
UpdateAt: orderModel.UpdateAt,
CreateAt: orderModel.CreateAt,
}
return result, nil
}
func (repository OrderRepository) Save(orderInfo *domain.Order) error {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.Order{
Id: orderInfo.Id,
OrderType: orderInfo.OrderType,
OrderCode: orderInfo.OrderCode,
OrderName: orderInfo.OrderName,
OrderStatus: orderInfo.OrderStatus,
OrderCount: orderInfo.OrderCount,
OrderActualCount: orderInfo.OrderActualCount,
OrderAmount: orderInfo.OrderAmount,
OrderActualAmount: orderInfo.OrderActualAmount,
OrderPaymentAmount: orderInfo.OrderPaymentAmount,
OrderRegionInfo: orderInfo.OrderRegionInfo,
Buyer: orderInfo.Buyer,
PartnerId: orderInfo.PartnerInfo.Id,
PartnerBonusPercent: orderInfo.PartnerBonusPercent,
SalesmanBonusPercent: orderInfo.SalesmanBonusPercent,
CreateAt: orderInfo.CreateAt,
UpdateAt: orderInfo.UpdateAt,
LastViewTime: orderInfo.LastViewTime,
}
if m.Id == 0 {
err = tx.Insert(m)
orderInfo.Id = m.Id
} else {
err = tx.Update(m)
}
return err
}
func (repository OrderRepository) Find(queryOption domain.OrderFindQuery) ([]domain.Order, error) {
db := repository.transactionContext.PgDd
orderModels := []models.Order{}
query := db.Model(&orderModels)
if queryOption.PartnerId > 0 {
query = query.Where("partner_id=?", queryOption.PartnerId)
}
if len(queryOption.OrderCode) > 0 {
query = query.Where("order_code like ?", "%"+queryOption.OrderCode+"%")
}
if queryOption.OrderType > 0 {
query = query.Where("order_type=?", queryOption.OrderType)
}
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
ordersReturn = make([]domain.Order, 0)
)
query = query.Order("order.id DESC")
err = query.Select()
if err != nil {
return ordersReturn, err
}
for i := range orderModels {
domainOrder, err := repository.transformPgModelToDomainModel(&orderModels[i])
if err != nil {
return ordersReturn, err
}
ordersReturn = append(ordersReturn, domainOrder)
}
return ordersReturn, nil
}
func (repository OrderRepository) CountAll(queryOption domain.OrderFindQuery) (int, error) {
db := repository.transactionContext.PgDd
orderModels := []models.Order{}
query := db.Model(&orderModels)
if queryOption.PartnerId > 0 {
query = query.Where("partner_id=?", queryOption.PartnerId)
}
if len(queryOption.OrderCode) > 0 {
query = query.Where("order_code like ?", "%"+queryOption.OrderCode+"%")
}
if queryOption.OrderType > 0 {
query = query.Where("order_type=?", queryOption.OrderType)
}
var (
err error
)
cnt, err := query.Count()
if err != nil {
return cnt, err
}
return cnt, nil
}
func (repository OrderRepository) FindOne(qureyOptions domain.OrderFindOneQuery) (*domain.Order, error) {
var (
err error
tx = repository.transactionContext.PgDd
)
m := new(models.Order)
err = tx.Model(m).
Where("id=?", qureyOptions.OrderId).
First()
if err != nil {
return nil, err
}
result, err := repository.transformPgModelToDomainModel(m)
return &result, err
}
func (repository OrderRepository) Remove(id int64) error {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.Order{
Id: id,
}
_, err = tx.Model(m).WherePK().Delete()
return err
}