up_account.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package income
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/admin/main/growup/model/income"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. // GetUpAccount get up_account
  10. func (s *Service) GetUpAccount(c context.Context, query string, isDeleted int) (ups []*model.UpAccount, err error) {
  11. from, limit := 0, 2000
  12. for {
  13. var up []*model.UpAccount
  14. up, err = s.ListUpAccount(c, query, isDeleted, from, limit)
  15. if err != nil {
  16. return
  17. }
  18. ups = append(ups, up...)
  19. if len(up) < limit {
  20. break
  21. }
  22. from += limit
  23. }
  24. return
  25. }
  26. // TxUpAccountBreach up_account breach
  27. func (s *Service) TxUpAccountBreach(c context.Context, tx *sql.Tx, mid, preMonthBreach, thisMonthBreach int64) error {
  28. if mid <= 0 {
  29. return fmt.Errorf("请输入正确的mid,请确认")
  30. }
  31. if preMonthBreach < 0 {
  32. return fmt.Errorf("未提现金额必须大于等于零,请确认")
  33. }
  34. if thisMonthBreach < 0 {
  35. return fmt.Errorf("当前月未提现金额必须大于等于零,请确认")
  36. }
  37. times := 0
  38. for {
  39. upAccount, err := s.dao.GetUpAccount(c, mid)
  40. if err != nil {
  41. log.Error("s.dao.GetUpAccount error(%v)", err)
  42. return err
  43. }
  44. total := upAccount.TotalIncome - preMonthBreach - thisMonthBreach
  45. unwithdraw := upAccount.TotalUnwithdrawIncome - preMonthBreach
  46. if total < 0 {
  47. log.Info("up_account(%d) total(%d) < 0", mid, total)
  48. total = 0
  49. }
  50. if unwithdraw < 0 {
  51. log.Info("up_account(%d) total_unwithdraw_income(%d) < 0", mid, unwithdraw)
  52. unwithdraw = 0
  53. }
  54. rows, err := s.dao.TxBreachUpAccount(tx, total, unwithdraw, mid, upAccount.Version+1, upAccount.Version)
  55. if err != nil {
  56. tx.Rollback()
  57. log.Error("s.dao.TxBreachUpAccount error(%v)", err)
  58. return err
  59. }
  60. if rows == 1 {
  61. break
  62. }
  63. times++
  64. if times >= 5 {
  65. tx.Rollback()
  66. return fmt.Errorf("更新up主金额错误")
  67. }
  68. }
  69. return nil
  70. }
  71. // UpAccountCount get up_account count
  72. func (s *Service) UpAccountCount(c context.Context, query string, isDeleted int) (total int64, err error) {
  73. if query != "" {
  74. query += " AND"
  75. }
  76. return s.dao.UpAccountCount(c, query, isDeleted)
  77. }
  78. // ListUpAccount list up account bu query
  79. func (s *Service) ListUpAccount(c context.Context, query string, isDeleted, from, limit int) (ups []*model.UpAccount, err error) {
  80. if query != "" {
  81. query += " AND"
  82. }
  83. return s.dao.ListUpAccount(c, query, isDeleted, from, limit)
  84. }