package client import ( "fmt" "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" "hudongzhuanjia/utils/code" "time" ) type VoteCtl struct { controllers.AuthorCtl } //投票 func (t *VoteCtl) Vote() { uid := t.MustGetUID() ladderId := t.MustGetInt64("vote_activity_ladder_id") //检查是否可以投票 ladder := new(models.NewVoteActivityLadder) exist, err := models.Get(ladder, ladderId) t.CheckErr(err) t.Assert(exist, code.MSG_VOTE_LADDER_NOT_EXIST, "投票人不存在") vote := new(models.NewVoteActivity) exist, err = models.Get(vote, ladder.VoteActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在") t.CheckRunning(vote.VoteStatus) activity := new(models.Activity) exist, err = models.Get(activity, vote.ActivityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") t.CheckRunning(activity.Status) history := new(models.NewVoteActivityHistory) exist, err = history.ExistByLadderId(uid, ladderId, activity.RehearsalId) t.CheckErr(err) t.Assert(!exist, code.MSG_VOTE_HISTORY_EXIST, "您已经投过票了") if vote.Model == "单选" { //检查是否已经投过票了 exist, err = history.ExistByVoteId(uid, vote.Id, activity.RehearsalId) t.CheckErr(err) t.Assert(!exist, code.MSG_VOTE_HISTORY_EXIST, "您已经投过票了") } else { num, err := history.CountUser(uid, activity.RehearsalId, vote.Id) t.CheckErr(err) if num >= vote.ModelNum { t.ERROR(fmt.Sprintf("您投票人数已超过%d人", vote.ModelNum), code.MSG_ERR) } } // done: 增加人数限制 history.UserId = uid history.RehearsalId = activity.RehearsalId history.VoteActivityId = vote.Id history.VoteActivityLadderId = ladderId history.IsDelete = false history.CreatedAt = time.Now() history.UpdatedAt = time.Now() _, err = models.Add(history) t.CheckErr(err) // vote ladder total_number + 1 _, err = ladder.Incr(ladderId, 1) t.CheckErr(err) t.SUCCESS("投票成功") } // 投票活动列表 func (t *VoteCtl) List() { activityId := t.MustGetInt64("activity_id") // 该活动是否进行中 activity := new(models.Activity) exist, err := models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") t.CheckRunning(activity.Status) votes, err := models.GetVoteListByActivityId(activity.Id) t.CheckErr(err) t.JSON(map[string]interface{}{ "total": len(votes), "votes": votes, }) } func (t *VoteCtl) Detail() { voteActivityId := t.MustGetInt64("vote_activity_id") uid := t.MustGetUID() activityId := t.MustGetInt64("activity_id") activity := new(models.Activity) exist, err := models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") ladders := make([]*models.NewVoteActivityLadder, 0) err = core.GetXormAuto().Where("is_delete=0 and vote_activity_id=?", voteActivityId). Desc("total_number").Asc("updated_at").Find(&ladders) t.CheckErr(err) histories := make([]*models.NewVoteActivityHistory, 0) err = core.GetXormAuto().Where("is_delete=0 and user_id=? and rehearsal_id=? and vote_activity_id=?", uid, activity.RehearsalId, voteActivityId).Find(&histories) t.CheckErr(err) for i := range ladders { for j := range histories { if ladders[i].Id == histories[j].VoteActivityLadderId { ladders[i].IsVote = true break } } } t.JSON(map[string]interface{}{ "total": len(ladders), "members": ladders, }) }