pg_order_payment_reponsitory.go
3.5 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
package repository
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
type OrderPaymentRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *OrderPaymentRepository) Save(dm *domain.OrderPayment) (*domain.OrderPayment, error) {
var (
err error
m = &models.OrderPayment{}
tx = repository.transactionContext.PgTx
)
if err = GobModelTransform(m, dm); err != nil {
return nil, err
}
if dm.Identify() == nil {
if dm.Id, err = NewSnowflakeId(); err != nil {
return dm, err
}
m.Id = dm.Id
if err = tx.Insert(m); err != nil {
return nil, err
}
return dm, nil
}
if err = tx.Update(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *OrderPaymentRepository) Remove(OrderPayment *domain.OrderPayment) (*domain.OrderPayment, error) {
var (
tx = repository.transactionContext.PgTx
OrderPaymentModel = &models.OrderPayment{Id: OrderPayment.Identify().(int64)}
)
if _, err := tx.Model(OrderPaymentModel).Where("id = ?", OrderPayment.Id).Delete(); err != nil {
return OrderPayment, err
}
return OrderPayment, nil
}
func (repository *OrderPaymentRepository) FindOne(queryOptions map[string]interface{}) (*domain.OrderPayment, error) {
tx := repository.transactionContext.PgTx
OrderPaymentModel := new(models.OrderPayment)
query := NewQuery(tx.Model(OrderPaymentModel), queryOptions)
query.SetWhere("order_payment.id = ?", "id")
query.SetWhere("order_payment.order_id = ?", "orderId")
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此订单")
}
if OrderPaymentModel.Id == 0 {
return nil, nil
}
return repository.transformPgModelToDomainModel(OrderPaymentModel)
}
func (repository *OrderPaymentRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.OrderPayment, error) {
tx := repository.transactionContext.PgTx
var OrderPaymentModels []*models.OrderPayment
OrderPayments := make([]*domain.OrderPayment, 0)
query := NewQuery(tx.Model(&OrderPaymentModels), queryOptions)
query.
SetWhere("order_payment.partner_id = ?", "partnerId").
SetWhere("order_payment.order_id = ?", "orderId").
SetWhere(`order_payment.create_at >= ?`, "beginTime").
SetWhere(`order_payment.create_at < ?`, "endTime").
SetLimit().
SetOrder("order_payment.create_at", "sortByCreateTime").
SetOrder("order_payment.update_at", "sortByUpdateTime")
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, OrderPayments, err
}
for _, OrderPaymentModel := range OrderPaymentModels {
if OrderPayment, err := repository.transformPgModelToDomainModel(OrderPaymentModel); err != nil {
return 0, OrderPayments, err
} else {
OrderPayments = append(OrderPayments, OrderPayment)
}
}
return int64(query.AffectRow), OrderPayments, nil
}
func (repository *OrderPaymentRepository) transformPgModelToDomainModel(OrderPaymentModel *models.OrderPayment) (*domain.OrderPayment, error) {
m := &domain.OrderPayment{}
err := GobModelTransform(m, OrderPaymentModel)
return m, err
}
func NewOrderPaymentRepository(transactionContext *transaction.TransactionContext) (*OrderPaymentRepository, error) {
if transactionContext == nil {
return nil, ERR_EMPTY_TC
}
return &OrderPaymentRepository{transactionContext: transactionContext}, nil
}