wallet_detail.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "reflect"
  6. "strings"
  7. "github.com/beego/beego/v2/client/orm"
  8. )
  9. type WalletDetail struct {
  10. WalletDetailId int `orm:"column(wallet_detail_id);auto" json:"-"`
  11. UserId string `orm:"column(user_id)" json:"user_id"`
  12. InviteCode string `orm:"column(invite_code)" json:"invite_code"`
  13. Addr string `orm:"column(addr)" json:"addr"`
  14. Email string `orm:"column(email)" json:"email"`
  15. TransPassword int `orm:"column(trans_password)" json:"trans_password"`
  16. XaiceStatus int `orm:"column(xaice_status)" json:"xaice_status"`
  17. XaiceReleaseStatus int `orm:"column(xaice_release_status)" json:"xaice_release_status"`
  18. UsdBalance string `orm:"column(usd_balance)" json:"usd_balance"`
  19. AiceBalance string `orm:"column(aice_balance)" json:"aice_balance"`
  20. AiceBalanceValue string `orm:"column(aice_balance_value)" json:"aice_balance_value"`
  21. XaiceBalance string `orm:"column(xaice_balance)" json:"xaice_balance"`
  22. XaiceBalanceValue string `orm:"column(xaice_balance_value)" json:"xaice_balance_value"`
  23. XaiceReleaseBalance string `orm:"column(xaice_release_balance)" json:"xaice_release_balance"`
  24. XaiceReleaseBalanceValue string `orm:"column(xaice_release_balance_value)" json:"xaice_release_balance_value"`
  25. WithdrawLimit string `orm:"column(withdraw_limit)" json:"withdraw_limit"`
  26. AiceWithdrawLimit string `orm:"column(aice_withdraw_limit)" json:"aice_withdraw_limit"`
  27. UsdtWithdrawStatus int `orm:"column(usdt_withdraw_status)" json:"usdt_withdraw_status"`
  28. AiceWithdrawStatus int `orm:"column(aice_withdraw_status)" json:"aice_withdraw_status"`
  29. BindStatus int `orm:"column(bind_status)" json:"bind_status"`
  30. UsdtAddress string `orm:"column(usdt_address)" json:"usdt_address"`
  31. AiceAddress string `orm:"column(aice_address)" json:"aice_address"`
  32. UdoStatus int `orm:"column(udo_status)" json:"udo_status"`
  33. UdoAddr string `orm:"column(udo_addr)" json:"udo_addr"`
  34. UdoBalance string `orm:"column(udo_balance)" json:"udo_balance"`
  35. UdoPrice string `orm:"column(udo_price)" json:"udo_price"`
  36. XaicePrice string `orm:"column(xaice_price)" json:"xaice_price"`
  37. AnygptUserid string `orm:"column(anygpt_userid)" json:"anygpt_userid"`
  38. ReceiveAddressStatus int `orm:"column(receive_address_status)" json:"receive_address_status"`
  39. LegacyTokenBalance string `orm:"column(legacy_token_balance)" json:"legacy_token_balance"`
  40. LegacyCreditBalance string `orm:"column(legacy_credit_balance)" json:"legacy_credit_balance"`
  41. CoalesceCreditBalance string `orm:"column(coalesce_credit_balance)" json:"coalesce_credit_balance"`
  42. ExchangeStatus int `orm:"column(exchange_status)" json:"exchange_status"`
  43. ExchangeFee string `orm:"column(exchange_fee)" json:"exchange_fee"`
  44. LegacyTokenUsdt string `orm:"column(legacy_token_usdt)" json:"legacy_token_usdt"`
  45. CoalesceCreditUsdt string `orm:"column(coalesce_credit_usdt)" json:"coalesce_credit_usdt"`
  46. UsdtWithdrawLimit string `orm:"column(usdt_withdraw_limit)" json:"usdt_withdraw_limit"`
  47. XaiceUsdtAddress string `orm:"column(xaice_usdt_address)" json:"xaice_usdt_address"`
  48. XaiceUsdtWithdrawStatus int `orm:"column(xaice_usdt_withdraw_status)" json:"xaice_usdt_withdraw_status"`
  49. LockAice string `orm:"column(lock_aice)" json:"lock_aice"`
  50. MusicAmount string `orm:"column(music_amount)" json:"music_amount"`
  51. ReleaseAice string `orm:"column(release_aice)" json:"release_aice"`
  52. ReleaseAiceValue string `orm:"column(release_aice_value)" json:"release_aice_value"`
  53. }
  54. func (t *WalletDetail) TableName() string {
  55. return "wallet_detail"
  56. }
  57. func init() {
  58. orm.RegisterModel(new(WalletDetail))
  59. }
  60. // AddWalletDetail insert a new WalletDetail into database and returns
  61. // last inserted Id on success.
  62. func AddWalletDetail(m *WalletDetail) (id int64, err error) {
  63. o := orm.NewOrm()
  64. id, err = o.Insert(m)
  65. return
  66. }
  67. // GetWalletDetailById retrieves WalletDetail by Id. Returns error if
  68. // Id doesn't exist
  69. func GetWalletDetailById(id int) (v *WalletDetail, err error) {
  70. o := orm.NewOrm()
  71. v = &WalletDetail{WalletDetailId: id}
  72. if err = o.Read(v); err == nil {
  73. return v, nil
  74. }
  75. return nil, err
  76. }
  77. // GetAllWalletDetail retrieves all WalletDetail matches certain condition. Returns empty list if
  78. // no records exist
  79. func GetAllWalletDetail(query map[string]string, fields []string, sortby []string, order []string,
  80. offset int64, limit int64) (ml []interface{}, err error) {
  81. o := orm.NewOrm()
  82. qs := o.QueryTable(new(WalletDetail))
  83. // query k=v
  84. for k, v := range query {
  85. // rewrite dot-notation to Object__Attribute
  86. k = strings.Replace(k, ".", "__", -1)
  87. if strings.Contains(k, "isnull") {
  88. qs = qs.Filter(k, (v == "true" || v == "1"))
  89. } else {
  90. qs = qs.Filter(k, v)
  91. }
  92. }
  93. // order by:
  94. var sortFields []string
  95. if len(sortby) != 0 {
  96. if len(sortby) == len(order) {
  97. // 1) for each sort field, there is an associated order
  98. for i, v := range sortby {
  99. orderby := ""
  100. if order[i] == "desc" {
  101. orderby = "-" + v
  102. } else if order[i] == "asc" {
  103. orderby = v
  104. } else {
  105. return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
  106. }
  107. sortFields = append(sortFields, orderby)
  108. }
  109. qs = qs.OrderBy(sortFields...)
  110. } else if len(sortby) != len(order) && len(order) == 1 {
  111. // 2) there is exactly one order, all the sorted fields will be sorted by this order
  112. for _, v := range sortby {
  113. orderby := ""
  114. if order[0] == "desc" {
  115. orderby = "-" + v
  116. } else if order[0] == "asc" {
  117. orderby = v
  118. } else {
  119. return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
  120. }
  121. sortFields = append(sortFields, orderby)
  122. }
  123. } else if len(sortby) != len(order) && len(order) != 1 {
  124. return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
  125. }
  126. } else {
  127. if len(order) != 0 {
  128. return nil, errors.New("Error: unused 'order' fields")
  129. }
  130. }
  131. var l []WalletDetail
  132. qs = qs.OrderBy(sortFields...)
  133. if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
  134. if len(fields) == 0 {
  135. for _, v := range l {
  136. ml = append(ml, v)
  137. }
  138. } else {
  139. // trim unused fields
  140. for _, v := range l {
  141. m := make(map[string]interface{})
  142. val := reflect.ValueOf(v)
  143. for _, fname := range fields {
  144. m[fname] = val.FieldByName(fname).Interface()
  145. }
  146. ml = append(ml, m)
  147. }
  148. }
  149. return ml, nil
  150. }
  151. return nil, err
  152. }
  153. // UpdateWalletDetail updates WalletDetail by Id and returns error if
  154. // the record to be updated doesn't exist
  155. func UpdateWalletDetailById(m *WalletDetail) (err error) {
  156. o := orm.NewOrm()
  157. v := WalletDetail{WalletDetailId: m.WalletDetailId}
  158. // ascertain id exists in the database
  159. if err = o.Read(&v); err == nil {
  160. var num int64
  161. if num, err = o.Update(m); err == nil {
  162. fmt.Println("Number of records updated in database:", num)
  163. }
  164. }
  165. return
  166. }
  167. // DeleteWalletDetail deletes WalletDetail by Id and returns error if
  168. // the record to be deleted doesn't exist
  169. func DeleteWalletDetail(id int) (err error) {
  170. o := orm.NewOrm()
  171. v := WalletDetail{WalletDetailId: id}
  172. // ascertain id exists in the database
  173. if err = o.Read(&v); err == nil {
  174. var num int64
  175. if num, err = o.Delete(&WalletDetail{WalletDetailId: id}); err == nil {
  176. fmt.Println("Number of records deleted in database:", num)
  177. }
  178. }
  179. return
  180. }