github.com/condensat/bank-core@v0.1.0/database/query/withdrawinfo.go (about) 1 // Copyright 2020 Condensat Tech. All rights reserved. 2 // Use of this source code is governed by a MIT 3 // license that can be found in the LICENSE file. 4 5 package query 6 7 import ( 8 "errors" 9 "time" 10 11 "github.com/condensat/bank-core/database" 12 "github.com/condensat/bank-core/database/model" 13 14 "github.com/jinzhu/gorm" 15 ) 16 17 var ( 18 ErrInvalidWithdrawInfoID = errors.New("Invalid WithdrawInfoID") 19 ErrInvalidWithdrawStatus = errors.New("Invalid WithdrawInfo Status") 20 ErrInvalidWithdrawInfoData = errors.New("Invalid WithdrawInfo Data") 21 ) 22 23 func AddWithdrawInfo(db database.Context, withdrawID model.WithdrawID, status model.WithdrawStatus, data model.WithdrawInfoData) (model.WithdrawInfo, error) { 24 gdb := db.DB().(*gorm.DB) 25 if db == nil { 26 return model.WithdrawInfo{}, database.ErrInvalidDatabase 27 } 28 29 if withdrawID == 0 { 30 return model.WithdrawInfo{}, ErrInvalidWithdrawID 31 } 32 if len(status) == 0 { 33 return model.WithdrawInfo{}, ErrInvalidWithdrawStatus 34 } 35 36 timestamp := time.Now().UTC().Truncate(time.Second) 37 result := model.WithdrawInfo{ 38 Timestamp: timestamp, 39 WithdrawID: withdrawID, 40 Status: status, 41 Data: data, 42 } 43 err := gdb.Create(&result).Error 44 if err != nil { 45 return model.WithdrawInfo{}, err 46 } 47 48 return result, nil 49 50 } 51 52 func GetWithdrawInfo(db database.Context, ID model.WithdrawInfoID) (model.WithdrawInfo, error) { 53 gdb := db.DB().(*gorm.DB) 54 if db == nil { 55 return model.WithdrawInfo{}, database.ErrInvalidDatabase 56 } 57 58 if ID == 0 { 59 return model.WithdrawInfo{}, ErrInvalidWithdrawInfoID 60 } 61 62 var result model.WithdrawInfo 63 err := gdb. 64 Where(&model.WithdrawInfo{ID: ID}). 65 First(&result).Error 66 if err != nil { 67 return model.WithdrawInfo{}, err 68 } 69 70 return result, nil 71 } 72 73 func GetLastWithdrawInfo(db database.Context, withdrawID model.WithdrawID) (model.WithdrawInfo, error) { 74 gdb := db.DB().(*gorm.DB) 75 if db == nil { 76 return model.WithdrawInfo{}, database.ErrInvalidDatabase 77 } 78 79 if withdrawID == 0 { 80 return model.WithdrawInfo{}, ErrInvalidWithdrawInfoID 81 } 82 83 var result model.WithdrawInfo 84 err := gdb. 85 Where(&model.WithdrawInfo{WithdrawID: withdrawID}). 86 Last(&result).Error 87 if err != nil { 88 return model.WithdrawInfo{}, err 89 } 90 91 return result, nil 92 93 } 94 95 func GetWithdrawHistory(db database.Context, withdrawID model.WithdrawID) ([]model.WithdrawInfo, error) { 96 gdb := db.DB().(*gorm.DB) 97 if db == nil { 98 return nil, database.ErrInvalidDatabase 99 } 100 101 if withdrawID == 0 { 102 return nil, ErrInvalidWithdrawID 103 } 104 105 var list []*model.WithdrawInfo 106 err := gdb. 107 Where(model.WithdrawInfo{ 108 WithdrawID: withdrawID, 109 }). 110 Order("id ASC"). 111 Find(&list).Error 112 113 if err != nil && err != gorm.ErrRecordNotFound { 114 return nil, err 115 } 116 117 return convertWithdrawInfoList(list), nil 118 } 119 120 func ListCancelingWithdrawsAccountOperations(db database.Context) ([]model.AccountOperation, error) { 121 gdb := db.DB().(*gorm.DB) 122 if gdb == nil { 123 return nil, database.ErrInvalidDatabase 124 } 125 126 subQueryLastWitdrawInfo := gdb.Model(&model.WithdrawInfo{}). 127 Select("MAX(id)"). 128 Group("withdraw_id"). 129 SubQuery() 130 131 var list []*model.AccountOperation 132 err := gdb. 133 Joins("JOIN (withdraw_info) ON account_operation.reference_id = withdraw_info.withdraw_id"). 134 Where("withdraw_info.id IN (?)", subQueryLastWitdrawInfo). 135 Where(model.AccountOperation{OperationType: model.OperationTypeTransfer}). 136 Where(model.WithdrawInfo{Status: model.WithdrawStatusCanceling}). 137 Order("id ASC"). 138 Find(&list).Error 139 140 if err != nil && err != gorm.ErrRecordNotFound { 141 return nil, err 142 } 143 144 return convertAccountOperationList(list), nil 145 } 146 147 func convertWithdrawInfoList(list []*model.WithdrawInfo) []model.WithdrawInfo { 148 var result []model.WithdrawInfo 149 for _, curr := range list { 150 if curr != nil { 151 result = append(result, *curr) 152 } 153 } 154 155 return result[:] 156 } 157 158 func WithdrawPagingCount(db database.Context, countByPage int) (int, error) { 159 if countByPage <= 0 { 160 countByPage = 1 161 } 162 163 switch gdb := db.DB().(type) { 164 case *gorm.DB: 165 166 var result int 167 err := gdb. 168 Model(&model.WithdrawInfo{}). 169 Group("withdraw_id"). 170 Count(&result).Error 171 var partialPage int 172 if result%countByPage > 0 { 173 partialPage = 1 174 } 175 return result/countByPage + partialPage, err 176 177 default: 178 return 0, database.ErrInvalidDatabase 179 } 180 } 181 182 func WithdrawPage(db database.Context, withdrawID model.WithdrawID, countByPage int) ([]model.WithdrawID, error) { 183 switch gdb := db.DB().(type) { 184 case *gorm.DB: 185 186 if countByPage <= 0 { 187 countByPage = 1 188 } 189 190 subQueryLast := gdb.Model(&model.WithdrawInfo{}). 191 Select("MAX(id)"). 192 Group("withdraw_id"). 193 SubQuery() 194 195 var list []*model.WithdrawInfo 196 err := gdb.Model(&model.WithdrawInfo{}). 197 Where("withdraw_info.id IN (?)", subQueryLast). 198 Where("id >= ?", withdrawID). 199 Order("withdraw_id ASC"). 200 Limit(countByPage). 201 Find(&list).Error 202 203 if err != nil && err != gorm.ErrRecordNotFound { 204 return nil, err 205 } 206 207 return convertWithdrawInfoIDs(list), nil 208 209 default: 210 return nil, database.ErrInvalidDatabase 211 } 212 } 213 214 func convertWithdrawInfoIDs(list []*model.WithdrawInfo) []model.WithdrawID { 215 var result []model.WithdrawID 216 for _, curr := range list { 217 if curr != nil { 218 result = append(result, curr.WithdrawID) 219 } 220 } 221 222 return result[:] 223 }