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

184 lines
5.6 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package pc
  2. import (
  3. "fmt"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "hudongzhuanjia/controllers"
  6. "hudongzhuanjia/models"
  7. "hudongzhuanjia/utils"
  8. "hudongzhuanjia/utils/code"
  9. "hudongzhuanjia/utils/define"
  10. "time"
  11. )
  12. type CalorieCtl struct {
  13. controllers.AuthorCtl
  14. }
  15. func (t *CalorieCtl) Ready() {
  16. calorieId := t.MustGetInt64("calorie_id")
  17. calorie := new(models.Calorie)
  18. exist, err := models.Get(calorie, calorieId)
  19. t.CheckErr(err)
  20. t.Assert(exist, code.MSG_CALORIE_NOT_EXIST, "卡路里活动不存在")
  21. if calorie.Status != define.StatusNotBegin {
  22. t.ERROR(fmt.Sprintf("该活动%s", calorie.Status), code.MSG_MODULE_STATUS_ERROR)
  23. }
  24. activity := new(models.Activity)
  25. exist, err = models.Get(activity, calorie.ActivityId)
  26. t.CheckErr(err)
  27. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  28. _, err = calorie.UpdateToStatusByActivityId(calorie.ActivityId, define.StatusReady, define.StatusNotBegin)
  29. t.CheckErr(err)
  30. calorie.StartTime = 0
  31. _, err = calorie.UpdateStatusById(calorieId, define.StatusReady)
  32. t.CheckErr(err)
  33. t.SUCCESS("操作成功")
  34. }
  35. // 开始, 加一个开始时间
  36. func (t *CalorieCtl) Start() {
  37. calorieId := t.MustGetInt64("calorie_id")
  38. calorie := new(models.Calorie)
  39. exist, err := models.Get(calorie, calorieId)
  40. t.CheckErr(err)
  41. t.Assert(exist, code.MSG_CALORIE_NOT_EXIST, "卡路里活动不存在")
  42. if calorie.Status != define.StatusReady {
  43. t.ERROR(fmt.Sprintf("该活动%s", calorie.Status), code.MSG_MODULE_STATUS_ERROR)
  44. }
  45. activity := new(models.Activity)
  46. exist, err = models.Get(activity, calorie.ActivityId)
  47. t.CheckErr(err)
  48. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  49. calorie.StartTime = time.Now().Unix()
  50. _, err = calorie.UpdateStatusById(calorieId, define.StatusRunning)
  51. t.CheckErr(err)
  52. t.SUCCESS("操作成功")
  53. }
  54. // 彩排活动需要恢复
  55. func (t *CalorieCtl) Stop() {
  56. calorieId := t.MustGetInt64("calorie_id")
  57. calorie := new(models.Calorie)
  58. exist, err := models.Get(calorie, calorieId)
  59. t.CheckErr(err)
  60. t.Assert(exist, code.MSG_CALORIE_NOT_EXIST, "卡路里活动不存在")
  61. if calorie.Status != define.StatusRunning {
  62. t.ERROR(fmt.Sprintf("该活动%s", calorie.Status), code.MSG_MODULE_STATUS_ERROR)
  63. }
  64. activity := new(models.Activity)
  65. exist, err = models.Get(activity, calorie.ActivityId)
  66. t.CheckErr(err)
  67. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  68. _, err = calorie.UpdateStatusById(calorieId, define.StatusEnding)
  69. t.CheckErr(err)
  70. t.SUCCESS("操作成功")
  71. }
  72. // 列出轮次, 包括倒计时
  73. func (t *CalorieCtl) List() {
  74. activityId := t.MustGetInt64("activity_id")
  75. calories := make([]*models.Calorie, 0)
  76. err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).Find(&calories)
  77. t.CheckErr(err)
  78. t.JSON(map[string]interface{}{
  79. "list": calories,
  80. "count": len(calories),
  81. })
  82. }
  83. // 二维码
  84. func (t *CalorieCtl) Qrcode() {
  85. activityId := t.MustGetInt64("activity_id")
  86. uid := t.MustGetUID()
  87. calorieId := t.MustGetInt64("calorie_id")
  88. area := new(models.AreaStore)
  89. exist, err := area.GetByCustomerId(uid)
  90. t.CheckErr(err)
  91. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户信息异常")
  92. qrcode, err := utils.GenH5Qrcode(define.H5Calorie, map[string]interface{}{
  93. "activity_id": activityId,
  94. "customer_id": uid,
  95. "area_id": area.Id,
  96. "calorie_id": calorieId,
  97. })
  98. t.CheckErr(err)
  99. t.JSON(map[string]interface{}{
  100. "qrcode": qrcode,
  101. })
  102. }
  103. type CalorieCountResult struct {
  104. Id int64 `json:"id"`
  105. ActivityId int64 `json:"activity_id"`
  106. CalorieId int64 `json:"calorie_id"`
  107. UserId int64 `json:"user_id"`
  108. Avatar string `json:"avatar"`
  109. Nickname string `json:"nickname"`
  110. Score int64 `json:"score"`
  111. }
  112. // 统计一些数据, 包括参与人数, 包括当前排名
  113. func (t *CalorieCtl) Count() {
  114. activityId := t.MustGetInt64("activity_id")
  115. calorieId := t.MustGetInt64("calorie_id")
  116. limit := t.DefaultInt("limit", 10)
  117. activity := new(models.Activity)
  118. exist, err := models.Get(activity, activityId)
  119. t.CheckErr(err)
  120. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  121. t.CheckRunning(activity.Status)
  122. calorie := new(models.Calorie)
  123. exist, err = models.Get(calorie, calorieId)
  124. t.CheckErr(err)
  125. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "卡路里不存在")
  126. // 统计人数
  127. count, err := core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=?",
  128. calorieId, activity.RehearsalId).Count(new(models.CalorieUser))
  129. t.CheckErr(err)
  130. // 统计排名
  131. result := make([]*CalorieCountResult, 0)
  132. err = core.GetXormAuto().Table(new(models.CalorieUser)).Alias("c").
  133. Select("c.id, c.activity_id, c.calorie_id, c.user_id, u.avatar, u.nickname, c.score, c.join_time").
  134. Join("LEFT", new(models.User).Alias("u"), "c.user_id=u.id and u.is_delete=0").
  135. Where("c.is_delete=0 and c.calorie_id=? and c.rehearsal_id=?", calorieId, activity.RehearsalId).
  136. Limit(limit).Desc("c.score").Asc("c.join_time").Find(&result)
  137. t.CheckErr(err)
  138. //duration := calorie.GameDuration - (time.Now().Unix() - calorie.StartTime) + 6 // 增加6s 倒计时
  139. //if calorie.StartTime == 0 {
  140. // duration = calorie.GameDuration
  141. //}
  142. // 3s 倒计时
  143. //var countDown float64 = 0
  144. //if duration-calorie.GameDuration > 0 {
  145. // countDown = math.Ceil(float64(duration-calorie.GameDuration) / 2.0)
  146. //}
  147. //if calorie.Status == define.StatusEnding || duration <= 0 {
  148. //duration = 0
  149. //}
  150. t.JSON(map[string]interface{}{
  151. "list": result, // 排名统计
  152. "count": count, // 人数统计
  153. //"time": duration - 2*int64(countDown), // 时长
  154. //"count_down": countDown, //倒计时
  155. //"status": calorie.Status, // 状态
  156. })
  157. }