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.go 4.5 kB

3 years ago
2 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
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package point
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/base"
  5. "code.gitea.io/gitea/modules/context"
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/routers/response"
  8. "code.gitea.io/gitea/services/reward"
  9. "code.gitea.io/gitea/services/reward/point/account"
  10. "code.gitea.io/gitea/services/task"
  11. "errors"
  12. "net/http"
  13. )
  14. const tplPoint base.TplName = "reward/point"
  15. const tplPointRule base.TplName = "reward/point/rule"
  16. type AccountResponse struct {
  17. Balance int64
  18. TotalEarned int64
  19. TotalConsumed int64
  20. }
  21. func GetPointAccount(ctx *context.Context) {
  22. userId := ctx.User.ID
  23. a, err := account.GetAccount(userId)
  24. if err != nil {
  25. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  26. return
  27. }
  28. res := &AccountResponse{
  29. Balance: a.Balance,
  30. TotalEarned: a.TotalEarned,
  31. TotalConsumed: a.TotalConsumed,
  32. }
  33. ctx.JSON(http.StatusOK, response.SuccessWithData(res))
  34. }
  35. func GetPointRecordList(ctx *context.Context) {
  36. operateType := ctx.Query("Operate")
  37. page := ctx.QueryInt("Page")
  38. var orderBy models.RewardOperateOrderBy
  39. switch ctx.Query("sort") {
  40. default:
  41. orderBy = models.RewardOrderByIDDesc
  42. }
  43. t := models.GetRewardOperateTypeInstance(operateType)
  44. if t == "" {
  45. ctx.JSON(http.StatusOK, response.ServerError("param error"))
  46. return
  47. }
  48. r, err := reward.GetRewardRecordList(&models.RewardRecordListOpts{
  49. ListOptions: models.ListOptions{PageSize: 10, Page: page},
  50. UserId: ctx.User.ID,
  51. OperateType: t,
  52. RewardType: models.RewardTypePoint,
  53. OrderBy: orderBy,
  54. IsAdmin: false,
  55. UserName: ctx.User.Name,
  56. })
  57. if err != nil {
  58. log.Error("GetPointRecordList error.%v", err)
  59. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  60. return
  61. }
  62. ctx.JSON(http.StatusOK, response.SuccessWithData(r))
  63. return
  64. }
  65. func OperatePointAccountBalance(ctx *context.Context, req models.AdminRewardOperateReq) {
  66. req.RewardType = models.RewardTypePoint
  67. if req.OperateType.Name() == "" || req.Remark == "" {
  68. ctx.JSON(http.StatusOK, "param error")
  69. return
  70. }
  71. err := reward.AdminBalanceOperate(req, ctx.User)
  72. if err != nil {
  73. log.Error("OperatePointAccountBalance error.%v", err)
  74. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  75. return
  76. }
  77. ctx.JSON(http.StatusOK, response.Success())
  78. }
  79. func GetPointPage(ctx *context.Context) {
  80. ctx.HTML(200, tplPoint)
  81. }
  82. func GetRulePage(ctx *context.Context) {
  83. ctx.HTML(200, tplPointRule)
  84. }
  85. func GetRuleConfig(ctx *context.Context) {
  86. r, err := task.GetPointRule()
  87. if err != nil {
  88. log.Error("GetRuleConfig error.%v", err)
  89. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  90. return
  91. }
  92. ctx.JSON(http.StatusOK, response.SuccessWithData(r))
  93. }
  94. func GetAdminRewardList(ctx *context.Context) {
  95. opts, err := buildAdminRewardRecordListOpts(ctx)
  96. if err != nil {
  97. log.Error("buildAdminRewardRecordListOpts error.%v", err)
  98. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  99. return
  100. }
  101. username := ctx.Query("userName")
  102. if username != "" {
  103. user, err := models.GetUserByName(username)
  104. if err != nil {
  105. log.Error("GetUserByName error.%v", err)
  106. if models.IsErrUserNotExist(err) {
  107. ctx.JSON(http.StatusOK, response.ServerError("user not exist"))
  108. } else {
  109. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  110. }
  111. return
  112. }
  113. opts.UserId = user.ID
  114. opts.UserName = user.Name
  115. }
  116. r, err := reward.GetRewardRecordList(opts)
  117. if err != nil {
  118. log.Error("GetRewardRecordList error.%v", err)
  119. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  120. return
  121. }
  122. ctx.JSON(http.StatusOK, response.SuccessWithData(r))
  123. }
  124. func buildAdminRewardRecordListOpts(ctx *context.Context) (*models.RewardRecordListOpts, error) {
  125. operateType := ctx.Query("operate")
  126. sourceType := ctx.Query("source")
  127. taskType := ctx.Query("action")
  128. serialNo := ctx.Query("serialNo")
  129. status := ctx.Query("status")
  130. page := ctx.QueryInt("page")
  131. var orderBy models.RewardOperateOrderBy
  132. switch ctx.Query("sort") {
  133. default:
  134. orderBy = models.RewardOrderByIDDesc
  135. }
  136. t := models.GetRewardOperateTypeInstance(operateType)
  137. if t == "" {
  138. return nil, errors.New("param error")
  139. }
  140. opts := &models.RewardRecordListOpts{
  141. ListOptions: models.ListOptions{PageSize: 10, Page: page},
  142. OperateType: t,
  143. RewardType: models.RewardTypePoint,
  144. OrderBy: orderBy,
  145. SourceType: sourceType,
  146. TaskType: taskType,
  147. SerialNo: serialNo,
  148. IsAdmin: true,
  149. Status: status,
  150. }
  151. return opts, nil
  152. }