package pc import ( "fmt" "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" "hudongzhuanjia/utils" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "strconv" "time" ) type VoteCtl struct { controllers.AuthorCtl } //二维码 func (t *VoteCtl) Qrcode() { activityId := t.MustGetInt64("activity_id") voteActivityId := t.MustGetInt64("vote_activity_id") uid := t.MustGetUID() area := new(models.AreaStore) exist, err := area.GetByCustomerId(uid, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户信息异常") qrcode, err := utils.GenH5Qrcode(define.H5Vote, map[string]interface{}{ "activity_id": activityId, "area_id": area.Id, "customer_id": uid, "vote_activity_id": voteActivityId, }) t.CheckErr(err) //直接输出二维码 t.JSON(map[string]interface{}{ "qrcode": qrcode, }) } // 装备数据 func (t *VoteCtl) ReadyVote() { voteId := t.MustGetInt64("vote_activity_id") vote := new(models.NewVoteActivity) exist, err := models.Get(vote, voteId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在") if vote.VoteStatus != define.StatusNotBegin { t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR) } _, err = vote.UpdateToStatusByAid(vote.ActivityId, define.StatusReady, define.StatusNotBegin) t.CheckErr(err) // 初始化某些数据, 例如投票的初始票数 // 彩排结束恢复某些数据 ladders := make([]*models.NewVoteActivityLadder, 0) err = core.GetXormAuto().Where("is_delete=0 and vote_activity_id=?", vote.Id).Find(&ladders) t.CheckErr(err) for _, ladder := range ladders { ladder.TotalNumber = ladder.VoteNumber _, err = core.GetXormAuto().Where("is_delete=0 and id=?", ladder.Id). Cols("total_number").Update(ladder) t.CheckErr(err) } _, err = vote.UpdateStatusById(vote.Id, define.StatusReady) t.CheckErr(err) t.SUCCESS("success") } //开始投票 func (t *VoteCtl) StartVote() { voteId := t.MustGetInt64("vote_activity_id") vote := new(models.NewVoteActivity) exist, err := models.Get(vote, voteId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在") if vote.VoteStatus != define.StatusReady { t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR) } vote.VoteStatus = define.StatusRunning vote.UpdatedAt = time.Now() if vote.VoteLastTime == "" { vote.VoteLastTime = "0" } duration, _ := strconv.ParseInt(vote.VoteLastTime, 10, 64) vote.VoteEndTime = time.Now().Unix() + duration _, err = core.GetXormAuto().Where("is_delete=0 and id=?", voteId). Cols("vote_status", "vote_end_time", "updated_at").Update(vote) t.CheckErr(err) t.SUCCESS("操作成功") } //停止投票 func (t *VoteCtl) StopVote() { voteId := t.MustGetInt64("vote_activity_id") vote := new(models.NewVoteActivity) exist, err := models.Get(vote, voteId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "投票活动不存在") if vote.VoteStatus != define.StatusRunning { t.ERROR(fmt.Sprintf("该活动%s", vote.VoteStatus), code.MSG_ERR) } vote.VoteStatus = define.StatusEnding vote.UpdatedAt = time.Now() vote.VoteEndTime = 0 _, err = core.GetXormAuto().Where("is_delete=0 and id=?", voteId). Cols("vote_status", "updated_at", "vote_end_time").Update(vote) t.CheckErr(err) t.SUCCESS("操作成功") } //获取参与人数 func (t *VoteCtl) JoinTotal() { voteActivityId := t.MustGetInt64("vote_activity_id") rehearsalId := t.MustGetInt64("rehearsal_id") total, err := core.GetXormAuto().Distinct("user_id").Where("vote_activity_id=? and rehearsal_id=? and is_delete=0", voteActivityId, rehearsalId).Count(new(models.NewVoteActivityHistory)) t.CheckErr(err) t.JSON(total) } //获取所有的投票活动 func (t *VoteCtl) List() { activityId := t.MustGetInt64("activity_id") votes, err := models.GetVoteListByActivityId(activityId) t.CheckErr(err) ids := make([]int64, 0) for _, v := range votes { ids = append(ids, v.Id) } ladders := make([]*models.NewVoteActivityLadder, 0) err = core.GetXormAuto().Where("is_delete=0").In("vote_activity_id", ids).Find(&ladders) t.CheckErr(err) for index := range votes { for i := range ladders { if votes[index].Id == ladders[i].VoteActivityId { votes[index].VoteActivityLadders = append(votes[index].VoteActivityLadders, ladders[i]) } } } t.JSON(map[string]interface{}{ "total": len(votes), "list": votes, }) } //获取投票的前几(头像、姓名、票数) func (t *VoteCtl) History() { voteActivityId := t.MustGetInt64("vote_activity_id") total := t.MustGetInt("total") ladders, err := models.GetNewVoteTopByVoteActivityId(voteActivityId, total) t.CheckErr(err) t.JSON(ladders) }