pg_order_good_repository.go
4.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
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 OrderGoodRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.OrderGoodRepository = (*OrderGoodRepository)(nil)
)
func NewOrderGoodRepository(transactionContext *transaction.TransactionContext) (*OrderGoodRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &OrderGoodRepository{transactionContext: transactionContext}, nil
}
func (reponsitory OrderGoodRepository) transformPgModelToDomainModel(orderModel *models.OrderGood) (order domain.OrderGood, err error) {
order = domain.OrderGood{
Id: orderModel.Id, OrderId: orderModel.OrderId, GoodName: orderModel.GoodName,
PlanGoodNumber: orderModel.PlanGoodNumber, UseGoodNumber: orderModel.UseGoodNumber,
Price: orderModel.Price, PartnerBonusPercent: orderModel.PartnerBonusPercent,
BonusStatus: orderModel.BonusStatus, Remark: orderModel.Remark,
GoodCompute: domain.GoodCompute{
PlanAmount: orderModel.PlanAmount,
UseAmount: orderModel.UseAmount,
PlanPartnerBonus: orderModel.PlanPartnerBonus,
UsePartnerBonus: orderModel.UsePartnerBonus,
PartnerBonusHas: orderModel.PartnerBonusHas,
PartnerBonusNot: orderModel.PartnerBonusNot,
PartnerBonusExpense: orderModel.PartnerBonusExpense,
},
CompanyId: orderModel.CompanyId,
}
switch orderModel.BonusStatus {
case domain.OrderGoodWaitPay:
order.CurrentBonusStatus = domain.OrderGoodBonusWaitPay{}
case domain.OrderGoodHasPay:
order.CurrentBonusStatus = domain.OrderGoodBonusHasPay{}
}
return order, nil
}
func (repository OrderGoodRepository) Save(data []domain.OrderGood) error {
var (
err error
tx = repository.transactionContext.PgTx
)
for k, v := range data {
m := &models.OrderGood{
Id: v.Id, OrderId: v.OrderId, GoodName: v.GoodName,
PlanGoodNumber: v.PlanGoodNumber, Price: v.Price,
PlanAmount: v.GoodCompute.PlanAmount, UseGoodNumber: v.UseGoodNumber,
UseAmount: v.GoodCompute.UseAmount, PartnerBonusPercent: v.PartnerBonusPercent,
PlanPartnerBonus: v.GoodCompute.PlanPartnerBonus, UsePartnerBonus: v.GoodCompute.UsePartnerBonus,
PartnerBonusHas: v.GoodCompute.PartnerBonusHas, PartnerBonusNot: v.GoodCompute.PartnerBonusNot,
PartnerBonusExpense: v.GoodCompute.PartnerBonusExpense, BonusStatus: v.BonusStatus,
Remark: v.Remark,
CompanyId: v.CompanyId,
}
if v.Id == 0 {
_, err = tx.Model(m).
Returning("*").
Insert()
if err != nil {
return err
}
data[k].Id = m.Id
} else {
_, err = tx.Model(m).
WherePK().
Update()
if err != nil {
return err
}
}
}
return nil
}
func (repository OrderGoodRepository) Find(queryOption domain.OrderGoodFindQuery) ([]domain.OrderGood, int, error) {
db := repository.transactionContext.PgDd
goodModes := []models.OrderGood{}
query := db.Model(&goodModes)
if queryOption.OrderId > 0 {
query = query.Where("order_id=?", queryOption.OrderId)
}
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(100)
}
var (
err error
orderGoods = make([]domain.OrderGood, 0)
)
query = query.Order("order_good.id")
count, err := query.SelectAndCount()
if err != nil {
return orderGoods, 0, err
}
for i := range goodModes {
domainOrderGood, err := repository.transformPgModelToDomainModel(&goodModes[i])
if err != nil {
return orderGoods, 0, err
}
orderGoods = append(orderGoods, domainOrderGood)
}
return orderGoods, count, nil
}
func (repository OrderGoodRepository) Remove(orderId int64, companyId int64, goodids ...int64) error {
var (
err error
tx = repository.transactionContext.PgTx
)
m := &models.OrderGood{}
query := tx.Model(m).
Where("order_id=?", orderId).
Where("company_id=?", companyId)
if len(goodids) > 0 {
query = query.WhereIn("id in(?)", goodids)
}
_, err = query.Delete()
return err
}