互动
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.

102 lines
3.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package calorie_service
  2. import (
  3. "errors"
  4. "hudongzhuanjia/models"
  5. "time"
  6. "github.com/ouxuanserver/osmanthuswine/src/core"
  7. )
  8. func GetCurrentCalorie(aid, uid, rid int, archId int) (map[string]interface{}, error) {
  9. calorie := new(models.Calorie)
  10. exist, err := calorie.GetCurrent(aid)
  11. if err != nil {
  12. return nil, err
  13. }
  14. if !exist {
  15. return nil, errors.New("轮次尚未开启")
  16. }
  17. calorieUser := new(models.CalorieUser)
  18. exist, err = calorieUser.GetByCalorieIdAndUserId(calorie.Id, archId, uid, rid)
  19. if err != nil {
  20. return nil, err
  21. }
  22. if !exist { // 不存在,代表最后一名
  23. calorieUser.ActivityId = aid
  24. calorieUser.RehearsalId = rid
  25. calorieUser.UserId = uid
  26. calorieUser.ArchId = archId
  27. calorieUser.CalorieId = calorie.Id
  28. calorieUser.Score = 0
  29. calorieUser.IsDelete = false
  30. calorieUser.JoinTime = time.Now().UnixNano()
  31. calorieUser.CreatedAt = time.Now()
  32. calorieUser.UpdatedAt = time.Now()
  33. _, err := models.Add(calorieUser)
  34. if err != nil {
  35. return nil, err
  36. }
  37. rank, err := core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=?",
  38. calorie.Id, rid).Count(new(models.CalorieUser))
  39. if err != nil {
  40. return nil, err
  41. }
  42. return map[string]interface{}{
  43. "rank": rank,
  44. "score": calorieUser.Score,
  45. "calorie_id": calorie.Id,
  46. }, nil
  47. } else { // 存在
  48. // 计算总数
  49. count, err := core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=?", calorie.Id, rid).Count(new(models.CalorieUser))
  50. if err != nil {
  51. return nil, err
  52. }
  53. // 计算分数 和 用户偏移量
  54. users := make([]*models.CalorieUser, 0)
  55. err = core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=? and score<=?",
  56. calorie.Id, rid, calorieUser.Score).Desc("score").Asc("join_time").Find(&users) // desc score asc updated_at
  57. if err != nil {
  58. return nil, err
  59. }
  60. var rank int
  61. for index, u := range users {
  62. if u.Id == calorieUser.Id {
  63. rank = int(count) - len(users) + index + 1 // 总数
  64. break
  65. }
  66. }
  67. return map[string]interface{}{
  68. "rank": rank,
  69. "score": calorieUser.Score,
  70. "calorie_id": calorie.Id,
  71. }, nil
  72. }
  73. }
  74. type CalorieCountResult struct {
  75. Id int `json:"id"`
  76. ActivityId int `json:"activity_id"`
  77. CalorieId int `json:"calorie_id"`
  78. UserId int `json:"user_id"`
  79. Avatar string `json:"avatar"`
  80. Nickname string `json:"nickname"`
  81. Score int `json:"score"`
  82. }
  83. func RankCalorieUser(calorieId, archId, rehearsalId interface{}, limit int) ([]*CalorieCountResult, error) {
  84. // 统计排名
  85. result := make([]*CalorieCountResult, 0)
  86. err := core.GetXormAuto().Table(new(models.CalorieUser)).Alias("c").
  87. Select("c.id, c.activity_id, c.calorie_id, c.user_id, u.avatar, u.nickname, c.score, c.join_time").
  88. Join("LEFT", new(models.User).Alias("u"), "c.user_id=u.id and u.is_delete=0").
  89. Where("c.is_delete=0 and c.calorie_id=? and c.arch_id=? and c.rehearsal_id=?", calorieId, archId, rehearsalId).
  90. Limit(limit).Desc("c.score").Asc("c.join_time").Find(&result)
  91. return result, err
  92. }