pg_order_good_repository.go
3.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
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 OrderGoodRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *OrderGoodRepository) Save(dm *domain.OrderGood) (*domain.OrderGood, error) {
var (
err error
m = &models.OrderGood{}
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.Model(m).Returning("id").Insert(m); err != nil {
return nil, err
}
return dm, nil
}
if _, err = tx.Model(m).Where("id=?", m.Id).Update(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *OrderGoodRepository) Remove(OrderGood *domain.OrderGood) (*domain.OrderGood, error) {
var (
tx = repository.transactionContext.PgTx
OrderGoodModel = &models.OrderGood{Id: OrderGood.Identify().(int64)}
)
if _, err := tx.Model(OrderGoodModel).Where("id = ?", OrderGood.Id).Delete(); err != nil {
return OrderGood, err
}
return OrderGood, nil
}
func (repository *OrderGoodRepository) FindOne(queryOptions map[string]interface{}) (*domain.OrderGood, error) {
tx := repository.transactionContext.PgTx
OrderGoodModel := new(models.OrderGood)
query := NewQuery(tx.Model(OrderGoodModel), queryOptions)
query.SetWhere(`id = ?`, "id")
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此订单")
}
if OrderGoodModel.Id == 0 {
return nil, nil
}
return repository.transformPgModelToDomainModel(OrderGoodModel)
}
func (repository *OrderGoodRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.OrderGood, error) {
tx := repository.transactionContext.PgTx
var OrderGoodModels []*models.OrderGood
OrderGoods := make([]*domain.OrderGood, 0)
query := NewQuery(tx.Model(&OrderGoodModels), queryOptions)
query.
SetWhere(`partner_id= ?`, "partnerId").
SetWhere(`create_at >= ?`, "beginTime").
SetWhere(`create_at < ?`, "endTime").
SetLimit().
SetOrder(`create_at`, "sortByCreateTime").
SetOrder(`update_at`, "sortByUpdateTime")
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, OrderGoods, err
}
for _, OrderGoodModel := range OrderGoodModels {
if OrderGood, err := repository.transformPgModelToDomainModel(OrderGoodModel); err != nil {
return 0, OrderGoods, err
} else {
OrderGoods = append(OrderGoods, OrderGood)
}
}
return int64(query.AffectRow), OrderGoods, nil
}
func (repository *OrderGoodRepository) transformPgModelToDomainModel(OrderGoodModel *models.OrderGood) (*domain.OrderGood, error) {
m := &domain.OrderGood{}
err := GobModelTransform(m, OrderGoodModel)
return m, err
}
func NewOrderGoodRepository(transactionContext *transaction.TransactionContext) (*OrderGoodRepository, error) {
if transactionContext == nil {
return nil, ERR_EMPTY_TC
}
return &OrderGoodRepository{transactionContext: transactionContext}, nil
}