pg_order_base_repository.go
4.0 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
package repository
import (
"encoding/json"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"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 OrderBaseRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *OrderBaseRepository) Save(dm *domain.OrderBase) (*domain.OrderBase, error) {
var (
err error
m = &models.OrderBase{}
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("*").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 *OrderBaseRepository) Remove(OrderBase *domain.OrderBase) (*domain.OrderBase, error) {
var (
tx = repository.transactionContext.PgTx
OrderBaseModel = &models.OrderBase{Id: OrderBase.Identify().(int64)}
)
if _, err := tx.Model(OrderBaseModel).Where("id = ?", OrderBase.Id).Delete(); err != nil {
return OrderBase, err
}
return OrderBase, nil
}
func (repository *OrderBaseRepository) FindOne(queryOptions map[string]interface{}) (*domain.OrderBase, error) {
tx := repository.transactionContext.PgTx
OrderBaseModel := new(models.OrderBase)
query := NewQuery(tx.Model(OrderBaseModel), queryOptions)
query.ColumnExpr("order_base.*")
query.Relation("OrderGood", func(q *orm.Query) (*orm.Query, error) {
q.ColumnExpr("order_good.*")
q.Order("id")
return q, nil
})
query.SetWhere(`"order_base".id = ?`, "id")
query.SetWhere(`"order_base".order_code = ?`, "order_code")
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此订单")
}
if OrderBaseModel.Id == 0 {
return nil, nil
}
return repository.transformPgModelToDomainModel(OrderBaseModel)
}
func (repository *OrderBaseRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.OrderBase, error) {
tx := repository.transactionContext.PgTx
var OrderBaseModels []*models.OrderBase
OrderBases := make([]*domain.OrderBase, 0)
query := NewQuery(tx.Model(&OrderBaseModels), queryOptions)
query.SetWhere(`"order_base".partner_id= ?`, "partnerId").
SetWhere(`"order_base".order_type= ?`, "orderType").
SetWhere(`"order_base".sale_date >= ?`, "beginTime").
SetWhere(`"order_base".sale_date < ?`, "endTime").
//SetWhere(`"order_base".is_disable = ?`, "isDisable").
SetLimit().
SetOrder(`order_base.create_time`, "sortByCreateTime").
SetOrder(`order_base.update_time`, "sortByUpdateTime").
SetOrder(`order_base.sale_date`, "sortBySalesTime")
if v, ok := queryOptions["orderTypes"]; ok {
query.Where(`"order_base".order_type in (?)`, pg.In(v))
}
if v, ok := queryOptions["partnerCategoryId"]; ok {
catId, _ := (v.(json.Number)).Int64()
query.Where(`"order_base".partner_category @>'{"id":?}'`, catId)
}
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, OrderBases, err
}
for _, OrderBaseModel := range OrderBaseModels {
if OrderBase, err := repository.transformPgModelToDomainModel(OrderBaseModel); err != nil {
return 0, OrderBases, err
} else {
OrderBases = append(OrderBases, OrderBase)
}
}
return int64(query.AffectRow), OrderBases, nil
}
func (repository *OrderBaseRepository) transformPgModelToDomainModel(OrderBaseModel *models.OrderBase) (*domain.OrderBase, error) {
m := &domain.OrderBase{}
err := GobModelTransform(m, OrderBaseModel)
return m, err
}
func NewOrderBaseRepository(transactionContext *transaction.TransactionContext) (*OrderBaseRepository, error) {
if transactionContext == nil {
return nil, ERR_EMPTY_TC
}
return &OrderBaseRepository{transactionContext: transactionContext}, nil
}