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

164 lines
4.7 KiB

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. qrcode, err := utils.GenH5Qrcode(define.H5Vote, map[string]interface{}{
  19. "activity_id": t.MustGetInt64("activity_id"),
  20. "customer_id": t.MustGetUID(),
  21. "vote_activity_id": t.MustGetInt64("vote_activity_id"),
  22. })
  23. t.CheckErr(err)
  24. //直接输出二维码
  25. t.JSON(map[string]interface{}{
  26. "qrcode": qrcode,
  27. })
  28. }
  29. // 装备数据
  30. func (t *VoteCtl) ReadyVote() {
  31. voteId := t.MustGetInt64("vote_activity_id")
  32. vote := new(models.NewVoteActivity)
  33. exist, err := models.GetById(vote, voteId)
  34. t.CheckErr(err)
  35. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在")
  36. if vote.VoteStatus != define.StatusNotBegin {
  37. t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR)
  38. }
  39. _, err = vote.UpdateToStatusByAid(vote.ActivityId, define.StatusReady, define.StatusNotBegin)
  40. t.CheckErr(err)
  41. // 初始化某些数据, 例如投票的初始票数
  42. // 彩排结束恢复某些数据
  43. ladders := make([]*models.NewVoteActivityLadder, 0)
  44. err = core.GetXormAuto().Where("is_delete=0 and vote_activity_id=?", vote.Id).Find(&ladders)
  45. t.CheckErr(err)
  46. for _, ladder := range ladders {
  47. ladder.TotalNumber = ladder.VoteNumber
  48. _, err = core.GetXormAuto().Where("is_delete=0 and id=?", ladder.Id).
  49. Cols("total_number").Update(ladder)
  50. t.CheckErr(err)
  51. }
  52. _, err = vote.UpdateStatusById(vote.Id, define.StatusReady)
  53. t.CheckErr(err)
  54. t.SUCCESS("success")
  55. }
  56. //开始投票
  57. func (t *VoteCtl) StartVote() {
  58. voteId := t.MustGetInt64("vote_activity_id")
  59. vote := new(models.NewVoteActivity)
  60. exist, err := models.GetById(vote, voteId)
  61. t.CheckErr(err)
  62. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在")
  63. if vote.VoteStatus != define.StatusReady {
  64. t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR)
  65. }
  66. vote.VoteStatus = define.StatusRunning
  67. vote.UpdatedAt = time.Now()
  68. if vote.VoteLastTime == "" {
  69. vote.VoteLastTime = "0"
  70. }
  71. duration, _ := strconv.ParseInt(vote.VoteLastTime, 10, 64)
  72. vote.VoteEndTime = time.Now().Unix() + duration
  73. _, err = core.GetXormAuto().Where("is_delete=0 and id=?", voteId).
  74. Cols("vote_status", "vote_end_time", "updated_at").Update(vote)
  75. t.CheckErr(err)
  76. t.STRING("操作成功")
  77. }
  78. //停止投票
  79. func (t *VoteCtl) StopVote() {
  80. voteId := t.MustGetInt64("vote_activity_id")
  81. vote := new(models.NewVoteActivity)
  82. exist, err := models.GetById(vote, voteId)
  83. t.CheckErr(err)
  84. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在")
  85. if vote.VoteStatus != define.StatusRunning {
  86. t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR)
  87. }
  88. vote.VoteStatus = define.StatusEnding
  89. vote.UpdatedAt = time.Now()
  90. vote.VoteEndTime = 0
  91. _, err = core.GetXormAuto().Where("is_delete=0 and id=?", voteId).
  92. Cols("vote_status", "updated_at", "vote_end_time").Update(vote)
  93. t.CheckErr(err)
  94. t.STRING("操作成功")
  95. }
  96. //获取参与人数
  97. func (t *VoteCtl) JoinTotal() {
  98. voteActivityId := t.MustGetInt64("vote_activity_id")
  99. rehearsalId := t.MustGetInt64("rehearsal_id")
  100. total, err := core.GetXormAuto().Distinct("user_id").Where("vote_activity_id=? and rehearsal_id=? and is_delete=0",
  101. voteActivityId, rehearsalId).Count(new(models.NewVoteActivityHistory))
  102. t.CheckErr(err)
  103. t.RAW(total)
  104. }
  105. //获取所有的投票活动
  106. func (t *VoteCtl) List() {
  107. activityId := t.MustGetInt64("activity_id")
  108. list := make([]*models.NewVoteActivity, 0)
  109. total, err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).
  110. Asc("created_at").FindAndCount(&list)
  111. t.CheckErr(err)
  112. ids := make([]int64, 0)
  113. for _, v := range list {
  114. ids = append(ids, v.Id)
  115. }
  116. ladders := make([]*models.NewVoteActivityLadder, 0)
  117. err = core.GetXormAuto().Where("is_delete=0").In("vote_activity_id", ids).Find(&ladders)
  118. t.CheckErr(err)
  119. for index := range list {
  120. for i := range ladders {
  121. if list[index].Id == ladders[i].VoteActivityId {
  122. list[index].VoteActivityLadders = append(list[index].VoteActivityLadders, ladders[i])
  123. }
  124. }
  125. }
  126. t.JSON(map[string]interface{}{
  127. "total": total,
  128. "list": list,
  129. })
  130. }
  131. //获取投票的前几(头像、姓名、票数)
  132. func (t *VoteCtl) History() {
  133. voteActivityId := t.MustGetInt64("vote_activity_id")
  134. //rehearsalId := t.MustGetInt64("rehearsal_id")
  135. total := t.MustGetInt("total")
  136. ladders := make([]*models.NewVoteActivityLadder, 0)
  137. err := core.GetXormAuto().Where("is_delete=0 and vote_activity_id=?", voteActivityId).
  138. Desc("total_number").Asc("updated_at").Limit(total).Find(&ladders)
  139. t.CheckErr(err)
  140. t.RAW(ladders)
  141. }