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

149 lines
3.7KB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/setting"
  8. "code.gitea.io/gitea/modules/timeutil"
  9. "xorm.io/builder"
  10. )
  11. type Transfer struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. FromID string
  14. ToID string
  15. Why string
  16. Qty int
  17. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  18. }
  19. // SearchTransOptions contains the options for searching
  20. type SearchTransOptions struct {
  21. FromId string
  22. ToId string
  23. Why string
  24. Keyword string
  25. OrderBy SearchOrderBy
  26. Page int
  27. PageSize int // Can be smaller than or equal to setting.UI.ExplorePagingNum
  28. }
  29. func (opts *SearchTransOptions) toConds() builder.Cond {
  30. var cond builder.Cond = builder.Gt{"ID": 0}
  31. return cond
  32. }
  33. func SearchTrans(opts *SearchTransOptions) (trans []*Transfer, _ int64, _ error) {
  34. condition := Transfer{
  35. FromID: opts.FromId,
  36. ToID: opts.ToId,
  37. }
  38. x2 := x.Where("why like ?","%"+ opts.Why+"%")
  39. count, err := x2.Count(new(Transfer),condition)
  40. if err != nil {
  41. return nil, 0, fmt.Errorf("Count: %v", err)
  42. }
  43. if opts.PageSize == 0 || opts.PageSize > setting.UI.ExplorePagingNum {
  44. opts.PageSize = setting.UI.ExplorePagingNum
  45. }
  46. if opts.Page <= 0 {
  47. opts.Page = 1
  48. }
  49. if len(opts.OrderBy) == 0 {
  50. opts.OrderBy = SearchOrderByIDReverse
  51. }
  52. sess := x.NewSession()
  53. sess = sess.Where("why like ?","%"+ opts.Why+"%")
  54. if opts.PageSize > 0 {
  55. sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  56. }
  57. if opts.PageSize == -1 {
  58. opts.PageSize = int(count)
  59. }
  60. trans = make([]*Transfer, 0, opts.PageSize)
  61. return trans, count, sess.OrderBy(opts.OrderBy.String()).Find(&trans,condition)
  62. }
  63. func TransferPoint(FromID string, Why string, ToID string, Qty int) (err error) {
  64. sess := x.NewSession()
  65. defer sess.Close()
  66. if err = sess.Begin(); err != nil {
  67. return err
  68. }
  69. if _, err = sess.Insert(&Transfer{FromID: FromID, ToID: ToID, Why: Why, Qty: Qty}); err != nil {
  70. return err
  71. }
  72. if _, err = sess.Exec("UPDATE `user` SET point = point + ? WHERE name = ?", Qty, ToID); err != nil {
  73. return err
  74. }
  75. if _, err = sess.Exec("UPDATE `user` SET point = point - ? WHERE name = ?", Qty, FromID); err != nil {
  76. return err
  77. }
  78. return sess.Commit()
  79. }
  80. // Fund contains the fund information
  81. type Fund struct {
  82. ID int64 `xorm:"pk autoincr"`
  83. Name string
  84. RepoID int64 `xorm:"INDEX"`
  85. Qty int64
  86. }
  87. type FundList []*Fund
  88. // GetFunds returns a list of Funds of given repository.
  89. func GetFunds(repoID int64, page int) (FundList, error) {
  90. funds := make([]*Fund, 0, setting.UI.IssuePagingNum)
  91. sess := x.Where("repo_id = ? ", repoID)
  92. if page > 0 {
  93. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  94. }
  95. return funds, sess.Find(&funds)
  96. }
  97. func NewFund(name string, repoID int64, qty int64)(err error){
  98. sess := x.NewSession()
  99. defer sess.Close()
  100. if err = sess.Begin(); err != nil {
  101. return err
  102. }
  103. if _, err = sess.Insert(&Fund{Name: name, RepoID: repoID, Qty: qty}); err != nil {
  104. return err
  105. }
  106. if _, err = sess.Exec("UPDATE `repository` SET point = point + ? WHERE id = ?", qty, repoID); err != nil {
  107. return err
  108. }
  109. if _, err = sess.Exec("UPDATE `repository` SET next_point = point * percent * 0.01 WHERE id = ?", repoID); err != nil {
  110. return err
  111. }
  112. return sess.Commit()
  113. }
  114. func GetRepoPointByUserID(userID int64) (int, error){
  115. repos, _, err := SearchRepository(&SearchRepoOptions{OwnerID:userID})
  116. if err != nil {
  117. return 0,err
  118. }
  119. var repoPoint int
  120. for _,v:= range repos{
  121. repoPoint += int(v.Point)
  122. }
  123. return repoPoint, nil
  124. }
上海开阖软件有限公司 沪ICP备12045867号-1