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

126 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
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, activity.ArchId)
  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, activity.ArchId)
  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, activity.ArchId)
  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. _, err = ladder.Incr(ladderId, 1)
  59. t.CheckErr(err)
  60. t.SUCCESS("投票成功")
  61. }
  62. // 投票活动列表
  63. func (t *VoteCtl) List() {
  64. activityId := t.MustGetInt64("activity_id")
  65. // 该活动是否进行中
  66. activity := new(models.Activity)
  67. exist, err := models.Get(activity, activityId)
  68. t.CheckErr(err)
  69. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  70. t.CheckRunning(activity.Status)
  71. votes, err := models.GetVoteListByActivityId(activity.Id)
  72. t.CheckErr(err)
  73. t.JSON(map[string]interface{}{
  74. "total": len(votes),
  75. "votes": votes,
  76. })
  77. }
  78. func (t *VoteCtl) Detail() {
  79. voteActivityId := t.MustGetInt64("vote_activity_id")
  80. uid := t.MustGetUID()
  81. activityId := t.MustGetInt64("activity_id")
  82. activity := new(models.Activity)
  83. exist, err := models.Get(activity, activityId)
  84. t.CheckErr(err)
  85. t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
  86. ladders := make([]*models.NewVoteActivityLadder, 0)
  87. err = core.GetXormAuto().Where("is_delete=0 and vote_activity_id=?", voteActivityId).
  88. Desc("total_number").Asc("updated_at").Find(&ladders)
  89. t.CheckErr(err)
  90. histories := make([]*models.NewVoteActivityHistory, 0)
  91. err = core.GetXormAuto().Where("is_delete=0 and user_id=? and rehearsal_id=? and vote_activity_id=? "+
  92. " and arch_id=?", uid, activity.RehearsalId, voteActivityId, activity.ArchId).Find(&histories)
  93. t.CheckErr(err)
  94. for i := range ladders {
  95. for j := range histories {
  96. if ladders[i].Id == histories[j].VoteActivityLadderId {
  97. ladders[i].IsVote = true
  98. break
  99. }
  100. }
  101. }
  102. t.JSON(map[string]interface{}{
  103. "total": len(ladders),
  104. "members": ladders,
  105. })
  106. }