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

166 lines
4.9 KiB

package client
import (
"github.com/ouxuanserver/osmanthuswine/src/core"
"hudongzhuanjia/controllers"
"hudongzhuanjia/models"
activity_service "hudongzhuanjia/services/activity"
"hudongzhuanjia/utils/code"
"hudongzhuanjia/utils/define"
)
type LotteryCtl struct {
controllers.AuthorCtl
}
func (t *LotteryCtl) UserLotteries() {
uid := t.MustGetUID()
userPrizes := make([]*models.UserPrize, 0)
err := core.GetXormAuto().Where("is_delete=0 and user_id=? ", uid).
Desc("created_at").Find(&userPrizes)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"list": userPrizes,
"total": len(userPrizes),
})
}
func (t *LotteryCtl) CashLottery() {
uid := t.MustGetUID()
prizeId := t.MustGetInt64("prize_id")
name := t.MustGet("name")
phone := t.MustGet("phone")
wxNo := t.MustGet("wx_no")
address := t.MustGet("address")
prize := new(models.UserPrize)
prize.Id = prizeId
prize.UserId = uid
prize.Status = 1
err := prize.Update("status")
t.CheckErr(err)
record := new(models.LotteryDrawRecord)
exist, err := record.GetByUserPrizeId(prizeId)
t.CheckErr(err)
t.Assert(exist, code.MSG_LOTTERY_DRAW_RECORD_NOT_EXIST, "中奖记录不存在")
record.Name = name
record.Phone = phone
record.WxNo = wxNo
record.Address = address
record.Status = 1
_, err = record.Update(record.Id, "name", "phone", "wx_no", "address", "status")
t.CheckErr(err)
t.SUCCESS("兑奖成功")
}
func (t *LotteryCtl) CheckLottery() {
uid := t.MustGetUID()
ladderId := t.MustGetInt64("lottery_draw_ladder_id")
ladder := new(models.LotteryDrawRuleLadder)
exist, err := models.Get(ladder, ladderId)
t.CheckErr(err)
t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在")
record := new(models.LotteryDrawRecord)
exist, err = record.GetByUserIdAndLadderId(uid, ladderId)
t.CheckErr(err)
//t.Assert(exist, code.MSG_LOTTERY_DRAW_NOT_HIT, "没有中奖")
t.JSON(map[string]interface{}{
"record": record,
"ladder": ladder,
"status": exist,
})
}
//type WinnersResult struct {
// UserId int64 `json:"user_id"`
// UserName string `json:"user_name"`
// UserPhone string `json:"user_phone"`
// Avatar string `json:"avatar"`
// PrizeName string `json:"prize_name"`
//}
//获取中奖名单
func (t *LotteryCtl) Winners() {
activityId := t.MustGetInt64("activity_id")
ladderId := t.MustGetInt64("lottery_draw_ladder_id")
activity := new(models.Activity)
exist, err := models.Get(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
ladder := new(models.LotteryDrawRuleLadder)
exist, err = models.Get(ladder, ladderId)
t.CheckErr(err)
t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在")
result := make([]*LotteryUsersResult, 0)
err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Alias("record").
Join("LEFT", new(models.User).Alias("user"), "user.id=record.user_id and user.is_delete=0").
Where("record.is_delete=0 and record.lottery_draw_rule_ladder_id=? and record.rehearsal_id=?", ladderId,
activity.RehearsalId).
Find(&result)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"total": len(result),
"ladder": ladder,
"list": result,
})
}
type LotteryUsersResult struct {
UserId int64 `json:"user_id"`
Username string `json:"username"`
Avatar string `json:"avatar"`
}
// 抽奖用户
func (t *LotteryCtl) Users() {
activityId := t.MustGetInt64("activity_id")
ladderId := t.MustGetInt64("lottery_draw_ladder_id")
areaId := t.MustGetInt64("area_id")
activity := new(models.Activity)
exist, err := models.Get(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
ladder := new(models.LotteryDrawRuleLadder)
exist, err = models.Get(ladder, ladderId)
t.CheckErr(err)
t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在")
result := make([]*LotteryUsersResult, 0)
session := core.GetXormAuto().Table(new(models.SignHistory)).Alias("h").
Select("h.user_id, u.nickname as username, u.avatar").Distinct("h.user_id").
Join("LEFT", new(models.User).Alias("u"), "h.user_id=u.id and u.is_delete = 0").
Where("h.is_delete=0 and h.area_id=? and h.activity_id=? and h.rehearsal_id=?",
areaId, activityId, activity.RehearsalId)
moduleService, exist, err := activity_service.GetModuleService(define.MODULE_LOTTERY, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在")
if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT {
// 去重标志
recordIds := make([]int64, 0)
err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Select("user_id").
Where("lottery_draw_rule_id=? and rehearsal_id=? and area_id=? and is_delete=0",
ladder.LotteryDrawRuleId, activity.RehearsalId, areaId).Find(&recordIds)
t.CheckErr(err)
session = session.NotIn("h.user_id", recordIds)
}
err = session.Find(&result)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"ladder": ladder,
"total": len(result),
"list": result,
})
}