/
operationinfo.go
279 lines (227 loc) · 6.32 KB
/
operationinfo.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2020 Condensat Tech. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package query
import (
"errors"
"time"
"github.com/condensat/bank-core/database"
"github.com/condensat/bank-core/database/model"
"github.com/jinzhu/gorm"
)
var (
ErrInvalidOperationInfoID = errors.New("Invalid OperationInfo")
ErrOperationInfoUpdateNotPermitted = errors.New("OperationInfo Update Not Permitted")
ErrInvalidTransactionID = errors.New("Invalid Transaction ID")
ErrInvalidOperationAmount = errors.New("Invalid Operation Amount")
)
// AddOperationInfo
func AddOperationInfo(db database.Context, operation model.OperationInfo) (model.OperationInfo, error) {
gdb := db.DB().(*gorm.DB)
if db == nil {
return model.OperationInfo{}, database.ErrInvalidDatabase
}
if operation.ID != 0 {
return model.OperationInfo{}, ErrOperationInfoUpdateNotPermitted
}
if operation.CryptoAddressID == 0 {
return model.OperationInfo{}, ErrInvalidCryptoAddressID
}
if len(operation.TxID) == 0 {
return model.OperationInfo{}, ErrInvalidTransactionID
}
if operation.Amount < 0.0 {
return model.OperationInfo{}, ErrInvalidOperationAmount
}
operation.Timestamp = time.Now().UTC().Truncate(time.Second)
err := gdb.
Assign(operation).
Create(&operation).Error
if err != nil {
return model.OperationInfo{}, err
}
return operation, nil
}
// GetOperationInfo
func GetOperationInfo(db database.Context, operationID model.OperationInfoID) (model.OperationInfo, error) {
gdb := db.DB().(*gorm.DB)
if db == nil {
return model.OperationInfo{}, database.ErrInvalidDatabase
}
if operationID == 0 {
return model.OperationInfo{}, ErrInvalidOperationInfoID
}
var result model.OperationInfo
err := gdb.
Where(model.OperationInfo{
ID: operationID,
}).
First(&result).Error
if err != nil {
return model.OperationInfo{}, err
}
return result, nil
}
// GetOperationInfoByTxId
func GetOperationInfoByTxId(db database.Context, txID model.TxID) (model.OperationInfo, error) {
gdb := db.DB().(*gorm.DB)
if db == nil {
return model.OperationInfo{}, database.ErrInvalidDatabase
}
if len(txID) == 0 {
return model.OperationInfo{}, ErrInvalidTransactionID
}
var result model.OperationInfo
err := gdb.
Where(model.OperationInfo{
TxID: txID,
}).
First(&result).Error
if err != nil {
return model.OperationInfo{}, err
}
return result, nil
}
// GetOperationInfoByCryptoAddress
func GetOperationInfoByCryptoAddress(db database.Context, cryptoAddressID model.CryptoAddressID) ([]model.OperationInfo, error) {
gdb := db.DB().(*gorm.DB)
if db == nil {
return nil, database.ErrInvalidDatabase
}
if cryptoAddressID == 0 {
return nil, ErrInvalidCryptoAddressID
}
var list []*model.OperationInfo
err := gdb.
Where(model.OperationInfo{
CryptoAddressID: cryptoAddressID,
}).
Find(&list).Error
if err != nil {
return nil, err
}
return convertOperationInfoList(list), nil
}
func FindCryptoAddressesNotInOperationInfo(db database.Context, chain model.String) ([]model.CryptoAddress, error) {
gdb := db.DB().(*gorm.DB)
if db == nil {
return nil, database.ErrInvalidDatabase
}
/*
select l.* from crypto_address as l
WHERE l.chain = 'bitcoin-testnet'
AND l.id NOT IN
(
SELECT r.crypto_address_id
FROM operation_info as r
);
*/
var list []*model.CryptoAddress
notInQuery := gdb.Model(&model.OperationInfo{}).
Select("crypto_address_id").
SubQuery()
err := gdb.Model(&model.CryptoAddress{}).
Where("chain = ?", chain).
Where("id NOT IN ?", notInQuery).
Find(&list).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return converCryptoAddressList(list), nil
}
func FindCryptoAddressesByOperationInfoState(db database.Context, chain model.String, states ...model.String) ([]model.CryptoAddress, error) {
gdb := db.DB().(*gorm.DB)
uniq := make(map[model.String]model.String)
for _, state := range states {
// skip empty states
if len(state) == 0 {
continue
}
uniq[state] = state
}
// slice uniq map
var slice []model.String
for state := range uniq {
slice = append(slice, state)
}
// state must not be empty
if len(slice) == 0 {
return nil, ErrInvalidOperationStatus
}
subQueryState := gdb.Model(&model.OperationStatus{}).
Select("operation_info_id").
Where("state IN (?)", slice).
SubQuery()
subQueryInfo := gdb.Model(&model.OperationInfo{}).
Select("id, crypto_address_id").
SubQuery()
var list []*model.CryptoAddress
err := gdb.Model(&model.CryptoAddress{}).
Joins("JOIN (?) AS oi ON crypto_address.id = oi.crypto_address_id", subQueryInfo).
Joins("JOIN (?) AS os ON oi.id = os.operation_info_id", subQueryState).
Where("chain = ?", chain).
Find(&list).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return converCryptoAddressList(list), nil
}
func DepositPagingCount(db database.Context, countByPage int) (int, error) {
if countByPage <= 0 {
countByPage = 1
}
switch gdb := db.DB().(type) {
case *gorm.DB:
var result int
err := gdb.
Model(&model.OperationInfo{}).
Count(&result).Error
var partialPage int
if result%countByPage > 0 {
partialPage = 1
}
return result/countByPage + partialPage, err
default:
return 0, database.ErrInvalidDatabase
}
}
func DepositPage(db database.Context, operationInfoID model.OperationInfoID, countByPage int) ([]model.OperationInfoID, error) {
switch gdb := db.DB().(type) {
case *gorm.DB:
if countByPage <= 0 {
countByPage = 1
}
var list []*model.OperationInfo
err := gdb.Model(&model.User{}).
Select("id").
Where("id >= ?", operationInfoID).
Order("id ASC").
Limit(countByPage).
Find(&list).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return convertOperationInfoIDs(list), nil
default:
return nil, database.ErrInvalidDatabase
}
}
func convertOperationInfoIDs(list []*model.OperationInfo) []model.OperationInfoID {
var result []model.OperationInfoID
for _, curr := range list {
if curr != nil {
result = append(result, curr.ID)
}
}
return result[:]
}
func convertOperationInfoList(list []*model.OperationInfo) []model.OperationInfo {
var result []model.OperationInfo
for _, curr := range list {
if curr == nil {
continue
}
result = append(result, *curr)
}
return result[:]
}