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

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