pg_order_repository.go
3.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
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 OrderRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *OrderRepository) Save(dm *domain.Order) (*domain.Order, error) {
var (
err error
m = &models.Order{}
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 *OrderRepository) Remove(Order *domain.Order) (*domain.Order, error) {
var (
tx = repository.transactionContext.PgTx
OrderModel = &models.Order{Id: Order.Identify().(int64)}
)
if _, err := tx.Model(OrderModel).Where("id = ?", Order.Id).Delete(); err != nil {
return Order, err
}
return Order, nil
}
func (repository *OrderRepository) FindOne(queryOptions map[string]interface{}) (*domain.Order, error) {
tx := repository.transactionContext.PgTx
OrderModel := new(models.Order)
query := NewQuery(tx.Model(OrderModel), queryOptions)
query.SetWhere(`"order".id = ?`, "id")
query.SetWhere(`"order".order_code = ?`, "orderCode")
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此订单")
}
if OrderModel.Id == 0 {
return nil, nil
}
return repository.transformPgModelToDomainModel(OrderModel)
}
func (repository *OrderRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Order, error) {
tx := repository.transactionContext.PgTx
var OrderModels []*models.Order
Orders := make([]*domain.Order, 0)
query := NewQuery(tx.Model(&OrderModels), queryOptions)
query.
SetWhere(`"order".partner_id= ?`, "partnerId").
SetWhere(`"order".order_type= ?`, "orderType").
SetWhere(`"order".order_status= ?`, "orderStatus").
SetWhere(`"order".create_at >= ?`, "beginTime").
SetWhere(`"order".create_at < ?`, "endTime").
SetLimit().
SetOrder(`order.create_at`, "sortByCreateTime").
SetOrder(`order.update_at`, "sortByUpdateTime")
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, Orders, err
}
for _, OrderModel := range OrderModels {
if Order, err := repository.transformPgModelToDomainModel(OrderModel); err != nil {
return 0, Orders, err
} else {
Orders = append(Orders, Order)
}
}
return int64(query.AffectRow), Orders, nil
}
func (repository *OrderRepository) transformPgModelToDomainModel(OrderModel *models.Order) (*domain.Order, error) {
m := &domain.Order{}
err := GobModelTransform(m, OrderModel)
return m, err
}
func NewOrderRepository(transactionContext *transaction.TransactionContext) (*OrderRepository, error) {
if transactionContext == nil {
return nil, ERR_EMPTY_TC
}
return &OrderRepository{transactionContext: transactionContext}, nil
}