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 2.8 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package models
  2. import "code.gitea.io/gitea/modules/timeutil"
  3. type PointAccountStatus int
  4. // Possible PointAccountStatus types.
  5. const (
  6. PointAccountNormal int = iota + 1 // 1
  7. PointAccountFreeze // 2
  8. PointAccountDeleted // 3
  9. )
  10. type PointAccount struct {
  11. ID int64 `xorm:"pk autoincr"`
  12. AccountCode string `xorm:"INDEX NOT NULL"`
  13. Balance int64 `xorm:"NOT NULL DEFAULT 0"`
  14. TotalEarned int64 `xorm:"NOT NULL DEFAULT 0"`
  15. TotalConsumed int64 `xorm:"NOT NULL DEFAULT 0"`
  16. UserId int64 `xorm:"INDEX NOT NULL"`
  17. Status int `xorm:"NOT NULL"`
  18. Version int64 `xorm:"NOT NULL"`
  19. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  20. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  21. }
  22. func (account *PointAccount) Increase(amount int64, sourceId string) error {
  23. sess := x.NewSession()
  24. defer sess.Close()
  25. sql := "update point_account set balance = balance + ?,total_earned = total_earned + ? ,version = version + 1 where account_code = ? "
  26. _, err := sess.Exec(sql, amount, amount, account.AccountCode)
  27. if err != nil {
  28. sess.Rollback()
  29. return err
  30. }
  31. accountLog := &PointAccountLog{
  32. AccountCode: account.AccountCode,
  33. UserId: account.UserId,
  34. Type: IncreaseAccountBalance,
  35. SourceId: sourceId,
  36. PointsAmount: amount,
  37. BalanceBefore: account.Balance,
  38. BalanceAfter: account.Balance + amount,
  39. AccountVersion: account.Version,
  40. }
  41. _, err = sess.Insert(accountLog)
  42. if err != nil {
  43. sess.Rollback()
  44. return err
  45. }
  46. sess.Commit()
  47. return nil
  48. }
  49. func (account *PointAccount) Decrease(amount int64, sourceId string) error {
  50. sess := x.NewSession()
  51. defer sess.Close()
  52. sql := "update point_account set balance = balance - ?,total_consumed = total_consumed + ? ,version = version + 1 where account_code = ? "
  53. _, err := sess.Exec(sql, amount, amount, account.AccountCode)
  54. if err != nil {
  55. sess.Rollback()
  56. return err
  57. }
  58. accountLog := &PointAccountLog{
  59. AccountCode: account.AccountCode,
  60. UserId: account.UserId,
  61. Type: DecreaseAccountBalance,
  62. SourceId: sourceId,
  63. PointsAmount: amount,
  64. BalanceBefore: account.Balance,
  65. BalanceAfter: account.Balance - amount,
  66. AccountVersion: account.Version,
  67. }
  68. _, err = sess.Insert(accountLog)
  69. if err != nil {
  70. sess.Rollback()
  71. return err
  72. }
  73. sess.Commit()
  74. return nil
  75. }
  76. func GetAccountByUserId(userId int64) (*PointAccount, error) {
  77. p := &PointAccount{}
  78. has, err := x.Where("user_id = ?", userId).Get(p)
  79. if err != nil {
  80. return nil, err
  81. }
  82. if !has {
  83. return nil, nil
  84. }
  85. return p, nil
  86. }
  87. func InsertAccount(tl *PointAccount) (int64, error) {
  88. return x.Insert(tl)
  89. }