pg_order_good_repository.go
5.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
package repository
import (
"errors"
"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,
RemarkReason: orderModel.RemarkReason,
DataFrom: orderModel.DataFrom,
}
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,
RemarkReason: v.RemarkReason,
DataFrom: v.DataFrom,
}
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
}
func (repository OrderGoodRepository) FindOne(queryOption domain.OrderGoodFindOneQuery) (domain.OrderGood, error) {
var (
good domain.OrderGood
goodModel models.OrderGood
err error
hasCondition bool
)
tx := repository.transactionContext.GetDB()
query := tx.Model(&goodModel)
if queryOption.GoodId > 0 {
query = query.Where("id=?", queryOption.GoodId)
hasCondition = true
}
if !hasCondition {
return good, errors.New("OrderGoodRepository.FindOne 必须使用查询条件")
}
err = query.First()
if err != nil {
return good, fmt.Errorf("获取订单的货品数据失败,%s", err)
}
good, err = repository.transformPgModelToDomainModel(&goodModel)
return good, err
}