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

127 lines
3.5 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
  1. package client
  2. import (
  3. "fmt"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "hudongzhuanjia/controllers"
  6. "hudongzhuanjia/models"
  7. "hudongzhuanjia/utils/code"
  8. "time"
  9. )
  10. type VoteCtl struct {
  11. controllers.AuthorCtl
  12. }
  13. //投票
  14. func (t *VoteCtl) Vote() {
  15. uid := t.MustGetUID()
  16. ladderId := t.MustGetInt64("vote_activity_ladder_id")
  17. //检查是否可以投票
  18. ladder := new(models.NewVoteActivityLadder)
  19. exist, err := models.Get(ladder, ladderId)
  20. t.CheckErr(err)
  21. t.Assert(exist, code.MSG_VOTE_LADDER_NOT_EXIST, "投票人不存在")
  22. vote := new(models.NewVoteActivity)
  23. exist, err = models.Get(vote, ladder.VoteActivityId)
  24. t.CheckErr(err)
  25. t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在")
  26. t.CheckRunning(vote.VoteStatus)
  27. activity := new(models.Activity)
  28. exist, err = models.Get(activity, vote.ActivityId)
  29. t.CheckErr(err)
  30. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  31. t.CheckRunning(activity.Status)
  32. history := new(models.NewVoteActivityHistory)
  33. exist, err = history.ExistByLadderId(uid, ladderId, activity.RehearsalId)
  34. t.CheckErr(err)
  35. t.Assert(!exist, code.MSG_VOTE_HISTORY_EXIST, "您已经投过票了")
  36. if vote.Model == "单选" {
  37. //检查是否已经投过票了
  38. exist, err = history.ExistByVoteId(uid, vote.Id, activity.RehearsalId)
  39. t.CheckErr(err)
  40. t.Assert(!exist, code.MSG_VOTE_HISTORY_EXIST, "您已经投过票了")
  41. } else {
  42. num, err := history.CountUser(uid, activity.RehearsalId, vote.Id)
  43. t.CheckErr(err)
  44. if num >= vote.ModelNum {
  45. t.ERROR(fmt.Sprintf("您投票人数已超过%d人", vote.ModelNum), code.MSG_ERR)
  46. }
  47. }
  48. // done: 增加人数限制
  49. history.UserId = uid
  50. history.RehearsalId = activity.RehearsalId
  51. history.VoteActivityId = vote.Id
  52. history.VoteActivityLadderId = ladderId
  53. history.IsDelete = false
  54. history.CreatedAt = time.Now()
  55. history.UpdatedAt = time.Now()
  56. _, err = models.Add(history)
  57. t.CheckErr(err)
  58. // vote ladder total_number + 1
  59. _, err = ladder.Incr(ladderId, 1)
  60. t.CheckErr(err)
  61. t.SUCCESS("投票成功")
  62. }
  63. // 投票活动列表
  64. func (t *VoteCtl) List() {
  65. activityId := t.MustGetInt64("activity_id")
  66. // 该活动是否进行中
  67. activity := new(models.Activity)
  68. exist, err := models.Get(activity, activityId)
  69. t.CheckErr(err)
  70. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  71. t.CheckRunning(activity.Status)
  72. votes, err := models.GetVoteListByActivityId(activity.Id)
  73. t.CheckErr(err)
  74. t.JSON(map[string]interface{}{
  75. "total": len(votes),
  76. "votes": votes,
  77. })
  78. }
  79. func (t *VoteCtl) Detail() {
  80. voteActivityId := t.MustGetInt64("vote_activity_id")
  81. uid := t.MustGetUID()
  82. activityId := t.MustGetInt64("activity_id")
  83. activity := new(models.Activity)
  84. exist, err := models.Get(activity, activityId)
  85. t.CheckErr(err)
  86. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  87. ladders := make([]*models.NewVoteActivityLadder, 0)
  88. err = core.GetXormAuto().Where("is_delete=0 and vote_activity_id=?", voteActivityId).
  89. Desc("total_number").Asc("updated_at").Find(&ladders)
  90. t.CheckErr(err)
  91. histories := make([]*models.NewVoteActivityHistory, 0)
  92. err = core.GetXormAuto().Where("is_delete=0 and user_id=? and rehearsal_id=? and vote_activity_id=?",
  93. uid, activity.RehearsalId, voteActivityId).Find(&histories)
  94. t.CheckErr(err)
  95. for i := range ladders {
  96. for j := range histories {
  97. if ladders[i].Id == histories[j].VoteActivityLadderId {
  98. ladders[i].IsVote = true
  99. break
  100. }
  101. }
  102. }
  103. t.JSON(map[string]interface{}{
  104. "total": len(ladders),
  105. "members": ladders,
  106. })
  107. }