pg_order_base_repository.go
6.2 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
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 OrderBaseRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.OrderBaseRepository = (*OrderBaseRepository)(nil)
)
func NewOrderBaseRepository(transactionContext *transaction.TransactionContext) (*OrderBaseRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &OrderBaseRepository{transactionContext: transactionContext}, nil
}
func (repository OrderBaseRepository) transformPgModelToDomainModel(orderModel *models.OrderBase) (order domain.OrderBase, err error) {
order = domain.OrderBase{
Id: orderModel.Id, OrderType: orderModel.OrderType, OrderCode: orderModel.OrderCode,
DeliveryCode: orderModel.DeliveryCode, Buyer: orderModel.Buyer, RegionInfo: orderModel.RegionInfo,
PartnerId: orderModel.PartnerId, SalesmanBonusPercent: orderModel.SalesmanBonusPercent,
CreateTime: orderModel.CreateTime, DeliveryTime: orderModel.DeliveryTime, UpdateTime: orderModel.UpdateTime,
SaleDate: orderModel.SaleDate,
IsDisable: orderModel.IsDisable,
OrderCompute: domain.OrderCompute{
PlanPartnerBonus: orderModel.PlanPartnerBonus, UsePartnerBonus: orderModel.UsePartnerBonus,
PartnerBonusHas: orderModel.PartnerBonusHas, PartnerBonusNot: orderModel.PartnerBonusNot,
PartnerBonusExpense: orderModel.PartnerBonusExpense, SalesmanBonus: orderModel.SalesmanBonus,
PlanOrderCount: orderModel.PlanOrderCount, PlanOrderAmount: orderModel.PlanOrderAmount,
UseOrderCount: orderModel.UseOrderCount, UseOrderAmount: orderModel.UseOrderAmount,
},
PartnerInfo: domain.Partner{
Id: orderModel.PartnerId,
},
BonusStatus: orderModel.BonusStatus,
CompanyId: orderModel.CompanyId,
DataFrom: orderModel.DataFrom,
Remark: orderModel.Remark,
PartnerCategory: orderModel.PartnerCategory,
}
return order, nil
}
func (repository OrderBaseRepository) Save(orderInfo *domain.OrderBase) error {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.OrderBase{
Id: orderInfo.Id,
OrderType: orderInfo.OrderType,
OrderCode: orderInfo.OrderCode,
DeliveryCode: orderInfo.DeliveryCode,
Buyer: orderInfo.Buyer,
RegionInfo: orderInfo.RegionInfo,
PartnerId: orderInfo.PartnerId,
SalesmanBonusPercent: orderInfo.SalesmanBonusPercent,
SalesmanBonus: orderInfo.OrderCompute.SalesmanBonus,
PlanOrderCount: orderInfo.OrderCompute.PlanOrderCount,
PlanOrderAmount: orderInfo.OrderCompute.PlanOrderAmount,
UseOrderCount: orderInfo.OrderCompute.UseOrderCount,
UseOrderAmount: orderInfo.OrderCompute.UseOrderAmount,
DeliveryTime: orderInfo.DeliveryTime,
PlanPartnerBonus: orderInfo.OrderCompute.PlanPartnerBonus,
UsePartnerBonus: orderInfo.OrderCompute.UsePartnerBonus,
PartnerBonusHas: orderInfo.OrderCompute.PartnerBonusHas,
PartnerBonusNot: orderInfo.OrderCompute.PartnerBonusNot,
PartnerBonusExpense: orderInfo.OrderCompute.PartnerBonusExpense,
IsDisable: orderInfo.IsDisable,
CreateTime: orderInfo.CreateTime,
SaleDate: orderInfo.SaleDate,
BonusStatus: orderInfo.BonusStatus,
CompanyId: orderInfo.CompanyId,
DataFrom: orderInfo.DataFrom,
Remark: orderInfo.Remark,
PartnerCategory: orderInfo.PartnerCategory,
}
if m.OrderType > 2 { // TODO 非平台自建订单,默认销售日期取订单创建日期
m.SaleDate = orderInfo.CreateTime
}
if m.Id == 0 {
_, err = tx.Model(m).
Returning("*").
Insert()
orderInfo.Id = m.Id
orderInfo.CreateTime = m.CreateTime
orderInfo.UpdateTime = m.UpdateTime
orderInfo.SaleDate = m.CreateTime
} else {
_, err = tx.Model(m).
WherePK().
Returning("*").
Update()
orderInfo.UpdateTime = m.UpdateTime
}
return err
}
func (repository OrderBaseRepository) Find(queryOption domain.OrderBaseFindQuery) ([]domain.OrderBase, int, error) {
db := repository.transactionContext.GetDB()
orderModels := []models.OrderBase{}
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 len(queryOption.DeliveryCode) > 0 {
query = query.Where("delivery_code like ?", "%"+queryOption.DeliveryCode+"%")
}
if queryOption.OrderType > 0 {
query = query.Where("order_type=?", queryOption.OrderType)
}
if queryOption.CompanyId > 0 {
query = query.Where("company_id=?", queryOption.CompanyId)
}
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.OrderBase, 0)
)
query = query.Order("order_base.create_time DESC")
count, err := query.SelectAndCount()
if err != nil {
return ordersReturn, 0, err
}
for i := range orderModels {
domainOrder, err := repository.transformPgModelToDomainModel(&orderModels[i])
if err != nil {
return ordersReturn, 0, err
}
ordersReturn = append(ordersReturn, domainOrder)
}
return ordersReturn, count, nil
}
func (repository OrderBaseRepository) FindOne(queryOptions domain.OrderBaseFindOneQuery) (*domain.OrderBase, error) {
var (
err error
tx = repository.transactionContext.PgDd
)
m := new(models.OrderBase)
query := tx.Model(m).
Where("id=?", queryOptions.OrderId)
if queryOptions.CompanyId > 0 {
query = query.Where("company_id=?", queryOptions.CompanyId)
}
err = query.First()
if err != nil {
return nil, err
}
result, err := repository.transformPgModelToDomainModel(m)
return &result, err
}
func (repository OrderBaseRepository) Remove(id int64, companyId int64) error {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.OrderBase{
Id: id,
}
_, err = tx.Model(m).
Where("id=?", id).
Where("company_id=?", companyId).
Delete()
return err
}