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

185 lines
5.1 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
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
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. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/models"
  6. "hudongzhuanjia/utils"
  7. "hudongzhuanjia/utils/code"
  8. "hudongzhuanjia/utils/define"
  9. "strconv"
  10. "time"
  11. "github.com/ouxuanserver/osmanthuswine/src/core"
  12. )
  13. type VoteCtl struct {
  14. controllers.AuthorCtl
  15. }
  16. //二维码
  17. func (t *VoteCtl) Qrcode() {
  18. activityId := t.MustGetInt("activity_id")
  19. voteActivityId := t.MustGetInt("vote_activity_id")
  20. uid := t.GetAccountId()
  21. area := new(models.AreaStore)
  22. exist, err := area.GetByCustomerId(uid, activityId)
  23. t.CheckErr(err)
  24. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户信息异常")
  25. qrcode, err := utils.GenH5Qrcode(define.H5Vote, map[string]interface{}{
  26. "activity_id": activityId,
  27. "area_id": area.Id,
  28. "customer_id": uid,
  29. "vote_activity_id": voteActivityId,
  30. })
  31. t.CheckErr(err)
  32. //直接输出二维码
  33. t.JSON(map[string]interface{}{
  34. "qrcode": qrcode,
  35. })
  36. }
  37. // 装备数据
  38. func (t *VoteCtl) ReadyVote() {
  39. voteId := t.MustGetInt("vote_activity_id")
  40. vote := new(models.NewVoteActivity)
  41. exist, err := models.Get(vote, voteId)
  42. t.CheckErr(err)
  43. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在")
  44. if vote.VoteStatus != define.StatusNotBegin {
  45. t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR)
  46. }
  47. _, err = vote.UpdateToStatusByAid(vote.ActivityId, define.StatusReady, define.StatusNotBegin)
  48. t.CheckErr(err)
  49. // 初始化某些数据, 例如投票的初始票数
  50. // 彩排结束恢复某些数据
  51. ladders := make([]*models.NewVoteActivityLadder, 0)
  52. err = core.GetXormAuto().Where("is_delete=0 and vote_activity_id=?", vote.Id).Find(&ladders)
  53. t.CheckErr(err)
  54. for _, ladder := range ladders {
  55. ladder.TotalNumber = ladder.VoteNumber
  56. _, err = core.GetXormAuto().Where("is_delete=0 and id=?", ladder.Id).
  57. Cols("total_number").Update(ladder)
  58. t.CheckErr(err)
  59. }
  60. vote.VoteStatus = define.StatusReady
  61. _, err = models.Update(voteId, vote, "vote_status")
  62. t.CheckErr(err)
  63. t.SUCCESS("success")
  64. }
  65. //开始投票
  66. func (t *VoteCtl) StartVote() {
  67. voteId := t.MustGetInt("vote_activity_id")
  68. vote := new(models.NewVoteActivity)
  69. exist, err := models.Get(vote, voteId)
  70. t.CheckErr(err)
  71. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在")
  72. if vote.VoteStatus != define.StatusReady {
  73. t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR)
  74. }
  75. // 将所有被投票人置0
  76. vote.VoteStatus = define.StatusRunning
  77. vote.UpdatedAt = time.Now()
  78. if vote.VoteLastTime == "" {
  79. vote.VoteLastTime = "0"
  80. }
  81. duration, _ := strconv.ParseInt(vote.VoteLastTime, 10, 64)
  82. vote.VoteEndTime = time.Now().Unix() + duration
  83. _, err = models.Update(voteId, vote, "vote_status", "vote_end_time")
  84. t.CheckErr(err)
  85. t.SUCCESS("操作成功")
  86. }
  87. //停止投票
  88. func (t *VoteCtl) StopVote() {
  89. voteId := t.MustGetInt("vote_activity_id")
  90. vote := new(models.NewVoteActivity)
  91. exist, err := models.Get(vote, voteId)
  92. t.CheckErr(err)
  93. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在")
  94. if vote.VoteStatus != define.StatusRunning {
  95. t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR)
  96. }
  97. vote.VoteStatus = define.StatusEnding
  98. vote.UpdatedAt = time.Now()
  99. vote.VoteEndTime = 0
  100. _, err = models.Update(voteId, vote, "vote_status", "vote_end_time")
  101. t.CheckErr(err)
  102. t.SUCCESS("操作成功")
  103. }
  104. //获取参与人数
  105. func (t *VoteCtl) JoinTotal() {
  106. voteActivityId := t.MustGetInt("vote_activity_id")
  107. vote := models.NewVoteActivity{}
  108. exist, err := models.Get(vote, voteActivityId)
  109. t.CheckErr(err)
  110. t.Assert(exist, code.MSG_VOTE_ACTIVITY_NOT_EXIST, "投票活动不存在")
  111. activity := new(models.Activity)
  112. exist, err = models.Get(activity, vote.ActivityId)
  113. t.CheckErr(err)
  114. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  115. total, err := core.GetXormAuto().Distinct("user_id").Where("vote_activity_id=? and rehearsal_id=? "+
  116. " arch_id=? and is_delete=0", voteActivityId, activity.RehearsalId, activity.ArchId).
  117. Count(new(models.NewVoteActivityHistory))
  118. t.CheckErr(err)
  119. t.JSON(total)
  120. }
  121. //获取所有的投票活动
  122. func (t *VoteCtl) List() {
  123. activityId := t.MustGetInt("activity_id")
  124. activity := new(models.Activity)
  125. exist, err := models.Get(activity, activityId)
  126. t.CheckErr(err)
  127. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  128. votes, err := models.GetVoteListByActivityId(activityId, activity.ArchId)
  129. t.CheckErr(err)
  130. ids := make([]int, 0)
  131. for _, v := range votes {
  132. ids = append(ids, v.Id)
  133. }
  134. ladders := make([]*models.NewVoteActivityLadder, 0)
  135. err = core.GetXormAuto().Where("is_delete=0").In("vote_activity_id", ids).Find(&ladders)
  136. t.CheckErr(err)
  137. for index := range votes {
  138. for i := range ladders {
  139. if votes[index].Id == ladders[i].VoteActivityId {
  140. votes[index].VoteActivityLadders = append(votes[index].VoteActivityLadders, ladders[i])
  141. }
  142. }
  143. }
  144. t.JSON(map[string]interface{}{
  145. "total": len(votes),
  146. "list": votes,
  147. })
  148. }
  149. //获取投票的前几(头像、姓名、票数)
  150. func (t *VoteCtl) History() {
  151. voteId := t.MustGetInt("vote_activity_id")
  152. total := t.MustGetInt("total")
  153. ladders, err := models.GetNewVoteTopByVoteActivityId(voteId, total)
  154. t.CheckErr(err)
  155. t.JSON(ladders)
  156. }