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

168 lines
4.7 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
  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. "strconv"
  11. "time"
  12. )
  13. type VoteCtl struct {
  14. controllers.AuthorCtl
  15. }
  16. //二维码
  17. func (t *VoteCtl) Qrcode() {
  18. activityId := t.MustGetInt64("activity_id")
  19. voteActivityId := t.MustGetInt64("vote_activity_id")
  20. uid := t.MustGetUID()
  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.MustGetInt64("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. _, err = vote.UpdateStatusById(vote.Id, define.StatusReady)
  61. t.CheckErr(err)
  62. t.SUCCESS("success")
  63. }
  64. //开始投票
  65. func (t *VoteCtl) StartVote() {
  66. voteId := t.MustGetInt64("vote_activity_id")
  67. vote := new(models.NewVoteActivity)
  68. exist, err := models.Get(vote, voteId)
  69. t.CheckErr(err)
  70. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在")
  71. if vote.VoteStatus != define.StatusReady {
  72. t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR)
  73. }
  74. vote.VoteStatus = define.StatusRunning
  75. vote.UpdatedAt = time.Now()
  76. if vote.VoteLastTime == "" {
  77. vote.VoteLastTime = "0"
  78. }
  79. duration, _ := strconv.ParseInt(vote.VoteLastTime, 10, 64)
  80. vote.VoteEndTime = time.Now().Unix() + duration
  81. _, err = core.GetXormAuto().Where("is_delete=0 and id=?", voteId).
  82. Cols("vote_status", "vote_end_time", "updated_at").Update(vote)
  83. t.CheckErr(err)
  84. t.SUCCESS("操作成功")
  85. }
  86. //停止投票
  87. func (t *VoteCtl) StopVote() {
  88. voteId := t.MustGetInt64("vote_activity_id")
  89. vote := new(models.NewVoteActivity)
  90. exist, err := models.Get(vote, voteId)
  91. t.CheckErr(err)
  92. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在")
  93. if vote.VoteStatus != define.StatusRunning {
  94. t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR)
  95. }
  96. vote.VoteStatus = define.StatusEnding
  97. vote.UpdatedAt = time.Now()
  98. vote.VoteEndTime = 0
  99. _, err = core.GetXormAuto().Where("is_delete=0 and id=?", voteId).
  100. Cols("vote_status", "updated_at", "vote_end_time").Update(vote)
  101. t.CheckErr(err)
  102. t.SUCCESS("操作成功")
  103. }
  104. //获取参与人数
  105. func (t *VoteCtl) JoinTotal() {
  106. voteActivityId := t.MustGetInt64("vote_activity_id")
  107. rehearsalId := t.MustGetInt64("rehearsal_id")
  108. total, err := core.GetXormAuto().Distinct("user_id").Where("vote_activity_id=? and rehearsal_id=? and is_delete=0",
  109. voteActivityId, rehearsalId).Count(new(models.NewVoteActivityHistory))
  110. t.CheckErr(err)
  111. t.JSON(total)
  112. }
  113. //获取所有的投票活动
  114. func (t *VoteCtl) List() {
  115. activityId := t.MustGetInt64("activity_id")
  116. votes, err := models.GetVoteListByActivityId(activityId)
  117. t.CheckErr(err)
  118. ids := make([]int64, 0)
  119. for _, v := range votes {
  120. ids = append(ids, v.Id)
  121. }
  122. ladders := make([]*models.NewVoteActivityLadder, 0)
  123. err = core.GetXormAuto().Where("is_delete=0").In("vote_activity_id", ids).Find(&ladders)
  124. t.CheckErr(err)
  125. for index := range votes {
  126. for i := range ladders {
  127. if votes[index].Id == ladders[i].VoteActivityId {
  128. votes[index].VoteActivityLadders = append(votes[index].VoteActivityLadders, ladders[i])
  129. }
  130. }
  131. }
  132. t.JSON(map[string]interface{}{
  133. "total": len(votes),
  134. "list": votes,
  135. })
  136. }
  137. //获取投票的前几(头像、姓名、票数)
  138. func (t *VoteCtl) History() {
  139. voteActivityId := t.MustGetInt64("vote_activity_id")
  140. total := t.MustGetInt("total")
  141. ladders, err := models.GetNewVoteTopByVoteActivityId(voteActivityId, total)
  142. t.CheckErr(err)
  143. t.JSON(ladders)
  144. }