|
|
1
|
+package repository
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "fmt"
|
|
|
5
|
+ "github.com/go-pg/pg/v10"
|
|
|
6
|
+
|
|
|
7
|
+ "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
|
|
|
8
|
+ pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
9
|
+ "github.com/linmadan/egglib-go/utils/snowflake"
|
|
|
10
|
+ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
|
|
|
11
|
+ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
|
|
|
12
|
+ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/transform"
|
|
|
13
|
+)
|
|
|
14
|
+
|
|
|
15
|
+type OrderGoodRepository struct {
|
|
|
16
|
+ transactionContext *pgTransaction.TransactionContext
|
|
|
17
|
+}
|
|
|
18
|
+
|
|
|
19
|
+func (repository *OrderGoodRepository) nextIdentify() (int64, error) {
|
|
|
20
|
+ IdWorker, err := snowflake.NewIdWorker(1)
|
|
|
21
|
+ if err != nil {
|
|
|
22
|
+ return 0, err
|
|
|
23
|
+ }
|
|
|
24
|
+ id, err := IdWorker.NextId()
|
|
|
25
|
+ return id, err
|
|
|
26
|
+}
|
|
|
27
|
+func (repository *OrderGoodRepository) Save(orderGood *domain.OrderGood) (*domain.OrderGood, error) {
|
|
|
28
|
+ sqlBuildFields := []string{
|
|
|
29
|
+ "order_good_id",
|
|
|
30
|
+ "order_good_amount",
|
|
|
31
|
+ "order_good_name",
|
|
|
32
|
+ "order_good_price",
|
|
|
33
|
+ "order_good_quantity",
|
|
|
34
|
+ "dividends_order_number",
|
|
|
35
|
+ "cooperation_contract_number",
|
|
|
36
|
+ "order_good_expense",
|
|
|
37
|
+ "created_at",
|
|
|
38
|
+ "deleted_at",
|
|
|
39
|
+ "updated_at",
|
|
|
40
|
+ }
|
|
|
41
|
+ insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
42
|
+ insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
|
|
|
43
|
+ returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
44
|
+ updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "orderGood_id")
|
|
|
45
|
+ updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
|
|
|
46
|
+ tx := repository.transactionContext.PgTx
|
|
|
47
|
+ if orderGood.Identify() == nil {
|
|
|
48
|
+ orderGoodId, err := repository.nextIdentify()
|
|
|
49
|
+ if err != nil {
|
|
|
50
|
+ return orderGood, err
|
|
|
51
|
+ } else {
|
|
|
52
|
+ orderGood.OrderGoodId = orderGoodId
|
|
|
53
|
+ }
|
|
|
54
|
+ if _, err := tx.QueryOne(
|
|
|
55
|
+ pg.Scan(
|
|
|
56
|
+ &orderGood.OrderGoodId,
|
|
|
57
|
+ &orderGood.OrderGoodAmount,
|
|
|
58
|
+ &orderGood.OrderGoodName,
|
|
|
59
|
+ &orderGood.OrderGoodPrice,
|
|
|
60
|
+ &orderGood.OrderGoodQuantity,
|
|
|
61
|
+ &orderGood.DividendsOrderNumber,
|
|
|
62
|
+ &orderGood.CooperationContractNumber,
|
|
|
63
|
+ &orderGood.OrderGoodExpense,
|
|
|
64
|
+ &orderGood.CreatedAt,
|
|
|
65
|
+ &orderGood.DeletedAt,
|
|
|
66
|
+ &orderGood.UpdatedAt,
|
|
|
67
|
+ ),
|
|
|
68
|
+ fmt.Sprintf("INSERT INTO order_goods (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
|
|
|
69
|
+ orderGood.OrderGoodId,
|
|
|
70
|
+ orderGood.OrderGoodAmount,
|
|
|
71
|
+ orderGood.OrderGoodName,
|
|
|
72
|
+ orderGood.OrderGoodPrice,
|
|
|
73
|
+ orderGood.OrderGoodQuantity,
|
|
|
74
|
+ orderGood.DividendsOrderNumber,
|
|
|
75
|
+ orderGood.CooperationContractNumber,
|
|
|
76
|
+ orderGood.OrderGoodExpense,
|
|
|
77
|
+ orderGood.CreatedAt,
|
|
|
78
|
+ orderGood.DeletedAt,
|
|
|
79
|
+ orderGood.UpdatedAt,
|
|
|
80
|
+ ); err != nil {
|
|
|
81
|
+ return orderGood, err
|
|
|
82
|
+ }
|
|
|
83
|
+ } else {
|
|
|
84
|
+ if _, err := tx.QueryOne(
|
|
|
85
|
+ pg.Scan(
|
|
|
86
|
+ &orderGood.OrderGoodId,
|
|
|
87
|
+ &orderGood.OrderGoodAmount,
|
|
|
88
|
+ &orderGood.OrderGoodName,
|
|
|
89
|
+ &orderGood.OrderGoodPrice,
|
|
|
90
|
+ &orderGood.OrderGoodQuantity,
|
|
|
91
|
+ &orderGood.DividendsOrderNumber,
|
|
|
92
|
+ &orderGood.CooperationContractNumber,
|
|
|
93
|
+ &orderGood.OrderGoodExpense,
|
|
|
94
|
+ &orderGood.CreatedAt,
|
|
|
95
|
+ &orderGood.DeletedAt,
|
|
|
96
|
+ &orderGood.UpdatedAt,
|
|
|
97
|
+ ),
|
|
|
98
|
+ fmt.Sprintf("UPDATE order_goods SET %s WHERE order_good_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
99
|
+ orderGood.OrderGoodId,
|
|
|
100
|
+ orderGood.OrderGoodAmount,
|
|
|
101
|
+ orderGood.OrderGoodName,
|
|
|
102
|
+ orderGood.OrderGoodPrice,
|
|
|
103
|
+ orderGood.OrderGoodQuantity,
|
|
|
104
|
+ orderGood.DividendsOrderNumber,
|
|
|
105
|
+ orderGood.CooperationContractNumber,
|
|
|
106
|
+ orderGood.OrderGoodExpense,
|
|
|
107
|
+ orderGood.CreatedAt,
|
|
|
108
|
+ orderGood.DeletedAt,
|
|
|
109
|
+ orderGood.UpdatedAt,
|
|
|
110
|
+ orderGood.Identify(),
|
|
|
111
|
+ ); err != nil {
|
|
|
112
|
+ return orderGood, err
|
|
|
113
|
+ }
|
|
|
114
|
+ }
|
|
|
115
|
+ return orderGood, nil
|
|
|
116
|
+}
|
|
|
117
|
+func (repository *OrderGoodRepository) Remove(orderGood *domain.OrderGood) (*domain.OrderGood, error) {
|
|
|
118
|
+ tx := repository.transactionContext.PgTx
|
|
|
119
|
+ orderGoodModel := new(models.OrderGood)
|
|
|
120
|
+ orderGoodModel.OrderGoodId = orderGood.Identify().(int64)
|
|
|
121
|
+ if _, err := tx.Model(orderGoodModel).WherePK().Delete(); err != nil {
|
|
|
122
|
+ return orderGood, err
|
|
|
123
|
+ }
|
|
|
124
|
+ return orderGood, nil
|
|
|
125
|
+}
|
|
|
126
|
+func (repository *OrderGoodRepository) FindOne(queryOptions map[string]interface{}) (*domain.OrderGood, error) {
|
|
|
127
|
+ tx := repository.transactionContext.PgTx
|
|
|
128
|
+ orderGoodModel := new(models.OrderGood)
|
|
|
129
|
+ query := sqlbuilder.BuildQuery(tx.Model(orderGoodModel), queryOptions)
|
|
|
130
|
+ query.SetWhereByQueryOption("order_good.order_good_id = ?", "orderGoodId")
|
|
|
131
|
+ if err := query.First(); err != nil {
|
|
|
132
|
+ if err.Error() == "pg: no rows in result set" {
|
|
|
133
|
+ return nil, fmt.Errorf("没有此资源")
|
|
|
134
|
+ } else {
|
|
|
135
|
+ return nil, err
|
|
|
136
|
+ }
|
|
|
137
|
+ }
|
|
|
138
|
+ if orderGoodModel.OrderGoodId == 0 {
|
|
|
139
|
+ return nil, nil
|
|
|
140
|
+ } else {
|
|
|
141
|
+ return transform.TransformToOrderGoodDomainModelFromPgModels(orderGoodModel)
|
|
|
142
|
+ }
|
|
|
143
|
+}
|
|
|
144
|
+func (repository *OrderGoodRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.OrderGood, error) {
|
|
|
145
|
+ tx := repository.transactionContext.PgTx
|
|
|
146
|
+ var orderGoodModels []*models.OrderGood
|
|
|
147
|
+ orderGoods := make([]*domain.OrderGood, 0)
|
|
|
148
|
+ query := sqlbuilder.BuildQuery(tx.Model(&orderGoodModels), queryOptions)
|
|
|
149
|
+ query.SetOffsetAndLimit(20)
|
|
|
150
|
+ query.SetOrderDirect("order_good_id", "DESC")
|
|
|
151
|
+ if count, err := query.SelectAndCount(); err != nil {
|
|
|
152
|
+ return 0, orderGoods, err
|
|
|
153
|
+ } else {
|
|
|
154
|
+ for _, orderGoodModel := range orderGoodModels {
|
|
|
155
|
+ if orderGood, err := transform.TransformToOrderGoodDomainModelFromPgModels(orderGoodModel); err != nil {
|
|
|
156
|
+ return 0, orderGoods, err
|
|
|
157
|
+ } else {
|
|
|
158
|
+ orderGoods = append(orderGoods, orderGood)
|
|
|
159
|
+ }
|
|
|
160
|
+ }
|
|
|
161
|
+ return int64(count), orderGoods, nil
|
|
|
162
|
+ }
|
|
|
163
|
+}
|
|
|
164
|
+func NewOrderGoodRepository(transactionContext *pgTransaction.TransactionContext) (*OrderGoodRepository, error) {
|
|
|
165
|
+ if transactionContext == nil {
|
|
|
166
|
+ return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
167
|
+ } else {
|
|
|
168
|
+ return &OrderGoodRepository{
|
|
|
169
|
+ transactionContext: transactionContext,
|
|
|
170
|
+ }, nil
|
|
|
171
|
+ }
|
|
|
172
|
+} |