You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

point_account.go 3.7 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/log"
  4. "code.gitea.io/gitea/modules/timeutil"
  5. )
  6. type PointAccountStatus int
  7. // Possible PointAccountStatus types.
  8. const (
  9. PointAccountNormal int = iota + 1 // 1
  10. PointAccountFreeze // 2
  11. PointAccountDeleted // 3
  12. )
  13. type PointAccount struct {
  14. ID int64 `xorm:"pk autoincr"`
  15. AccountCode string `xorm:"INDEX NOT NULL"`
  16. Balance int64 `xorm:"NOT NULL DEFAULT 0"`
  17. TotalEarned int64 `xorm:"NOT NULL DEFAULT 0"`
  18. TotalConsumed int64 `xorm:"NOT NULL DEFAULT 0"`
  19. UserId int64 `xorm:"INDEX NOT NULL"`
  20. Status int `xorm:"NOT NULL"`
  21. Version int64 `xorm:"NOT NULL"`
  22. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  23. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  24. }
  25. func (account *PointAccount) Increase(amount int64, sourceId string) error {
  26. sess := x.NewSession()
  27. defer sess.Close()
  28. sql := "update point_account set balance = balance + ?,total_earned = total_earned + ? ,version = version + 1 where account_code = ? "
  29. _, err := sess.Exec(sql, amount, amount, account.AccountCode)
  30. if err != nil {
  31. sess.Rollback()
  32. return err
  33. }
  34. accountLog := &PointAccountLog{
  35. AccountCode: account.AccountCode,
  36. UserId: account.UserId,
  37. Type: IncreaseAccountBalance,
  38. SourceId: sourceId,
  39. PointsAmount: amount,
  40. BalanceBefore: account.Balance,
  41. BalanceAfter: account.Balance + amount,
  42. AccountVersion: account.Version,
  43. }
  44. _, err = sess.Insert(accountLog)
  45. if err != nil {
  46. sess.Rollback()
  47. return err
  48. }
  49. sess.Commit()
  50. return nil
  51. }
  52. func (account *PointAccount) Decrease(amount int64, sourceId string) error {
  53. sess := x.NewSession()
  54. defer sess.Close()
  55. sql := "update point_account set balance = balance - ?,total_consumed = total_consumed + ? ,version = version + 1 where account_code = ? "
  56. _, err := sess.Exec(sql, amount, amount, account.AccountCode)
  57. if err != nil {
  58. sess.Rollback()
  59. return err
  60. }
  61. accountLog := &PointAccountLog{
  62. AccountCode: account.AccountCode,
  63. UserId: account.UserId,
  64. Type: DecreaseAccountBalance,
  65. SourceId: sourceId,
  66. PointsAmount: amount,
  67. BalanceBefore: account.Balance,
  68. BalanceAfter: account.Balance - amount,
  69. AccountVersion: account.Version,
  70. }
  71. _, err = sess.Insert(accountLog)
  72. if err != nil {
  73. sess.Rollback()
  74. return err
  75. }
  76. sess.Commit()
  77. return nil
  78. }
  79. func GetAccountByUserId(userId int64) (*PointAccount, error) {
  80. p := &PointAccount{}
  81. has, err := x.Where("user_id = ?", userId).Get(p)
  82. if err != nil {
  83. return nil, err
  84. }
  85. if !has {
  86. return nil, ErrRecordNotExist{}
  87. }
  88. return p, nil
  89. }
  90. func InsertAccount(tl *PointAccount) (int64, error) {
  91. return x.Insert(tl)
  92. }
  93. type SearchPointAccountOpts struct {
  94. ListOptions
  95. Keyword string
  96. }
  97. type SearchPointAccountResponse struct {
  98. Records []*UserPointAccount
  99. PageSize int
  100. Page int
  101. Total int64
  102. }
  103. type UserPointAccount struct {
  104. UserId int64
  105. UserName string
  106. Email string
  107. Balance int64
  108. TotalEarned int64
  109. TotalConsumed int64
  110. }
  111. func (UserPointAccount) TableName() string {
  112. return "user"
  113. }
  114. func GetPointAccountMapByUserIds(userIds []int64) (map[int64]*PointAccount, error) {
  115. if len(userIds) == 0 {
  116. return make(map[int64]*PointAccount, 0), nil
  117. }
  118. accounts := make([]*PointAccount, 0)
  119. err := x.In("user_id", userIds).Find(&accounts)
  120. if err != nil {
  121. log.Error("GetPointAccountMapByUserIds error.%v", err)
  122. return nil, err
  123. }
  124. accountMap := make(map[int64]*PointAccount, 0)
  125. for _, v := range accounts {
  126. accountMap[v.UserId] = v
  127. }
  128. return accountMap, nil
  129. }