package pc import ( "fmt" "hudongzhuanjia/controllers" "hudongzhuanjia/models" activity_service "hudongzhuanjia/services/activity" im_service "hudongzhuanjia/services/im" lottery_service "hudongzhuanjia/services/lottery" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "time" ) //抽奖 type LotteryDrawCtl struct { controllers.AuthorCtl } //开始抽奖 func (t *LotteryDrawCtl) Start() { ladderId := t.MustGetInt64("lottery_draw_ladder_id") activityId := t.MustGetInt64("activity_id") ladder := new(models.LotteryDrawRuleLadder) exist, err := models.Get(ladder, ladderId) t.CheckErr(err) t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在") 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) if ladder.Status != define.StatusNotBegin { t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR) } ladder.Status = define.StatusRunning ladder.UpdatedAt = time.Now() ladder.RollNum = 0 _, err = models.Update(ladder.Id, ladder, "status", "roll_num", "updated_at") t.CheckErr(err) // 通知直播开始抽奖 if activity.RehearsalId == 0 { live := new(models.LiveConfig) exist, err := live.GetByActivityId(activity.Id) t.CheckErr(err) if exist && live.HaveModule(define.MODULE_LOTTERY) { go im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawStart, map[string]interface{}{ "lottery_draw_ladder_id": ladder.Id, "timestamp": time.Now().Unix(), "desc": "开始抽奖", }) } } t.SUCCESS("操作成功") } // 滚动 func (t *LotteryDrawCtl) StartRoll() { ladderId := t.MustGetInt64("lottery_draw_ladder_id") activityId := t.MustGetInt64("activity_id") num := t.MustGetInt64("num") 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) ladder := new(models.LotteryDrawRuleLadder) exist, err = models.Get(ladder, ladderId) t.CheckErr(err) t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在") if ladder.Status != define.StatusRunning { t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR) } ladder.RollNum += 1 _, err = models.Update(ladder.Id, ladder, "roll_num") // 通知直播开始抽奖 if activity.RehearsalId == 0 { live := new(models.LiveConfig) exist, err := live.GetByActivityId(activity.Id) t.CheckErr(err) if exist && live.HaveModule(define.MODULE_LOTTERY) { go im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawRollStart, map[string]interface{}{ "lottery_draw_ladder_id": ladder.Id, "timestamp": time.Now().Unix(), "roll_num": ladder.RollNum, "desc": "开始滚动", "number": num, }) } } t.SUCCESS("操作成功") } func (t *LotteryDrawCtl) StopRoll() { ladderId := t.MustGetInt64("lottery_draw_ladder_id") activityId := t.MustGetInt64("activity_id") num := t.MustGetInt64("num") 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) ladder := new(models.LotteryDrawRuleLadder) exist, err = models.Get(ladder, ladderId) t.CheckErr(err) t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在") // 通知直播开始抽奖 if activity.RehearsalId == 0 { live := new(models.LiveConfig) exist, err := live.GetByActivityId(activity.Id) t.CheckErr(err) if exist && live.HaveModule(define.MODULE_LOTTERY) { go im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawRollStop, map[string]interface{}{ "lottery_draw_ladder_id": ladder.Id, "timestamp": time.Now().Unix(), "roll_num": ladder.RollNum, "desc": "停止滚动", "number": num, }) } } t.SUCCESS("操作成功") } //停止抽奖 func (t *LotteryDrawCtl) Stop() { ladderId := t.MustGetInt64("lottery_draw_ladder_id") 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) ladder := new(models.LotteryDrawRuleLadder) exist, err = models.Get(ladder, ladderId) t.CheckErr(err) t.Assert(exist, code.MSG_LOTTERY_RULE_NOT_EXIST, "抽奖规则不存在") ladder.Status = define.StatusEnding ladder.UpdatedAt = time.Now() ladder.RollNum = 0 _, err = models.Update(ladder.Id, ladder, "status", "updated_at", "roll_num") t.CheckErr(err) // 通知直播开始抽奖 if activity.RehearsalId == 0 { live := new(models.LiveConfig) exist, err := live.GetByActivityId(activity.Id) t.CheckErr(err) if exist && live.HaveModule(define.MODULE_LOTTERY) { go im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawStop, map[string]interface{}{ "lottery_draw_ladder_id": ladder.Id, "timestamp": time.Now().Unix(), "desc": "结束抽奖", }) } } t.SUCCESS("操作成功") } //获取所有抽奖活动列表 func (t *LotteryDrawCtl) List() { activityId := t.MustGetInt64("activity_id") customerId := t.MustGetUID() customer := new(models.Customer) exist, err := models.Get(customer, customerId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") activity := new(models.Activity) exist, err = models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") result, err := lottery_service.GetLotteryAndLadder(activityId, activity.RehearsalId) t.JSON(map[string]interface{}{ "total": len(result), "list": result, }) } //抽奖奖品 func (t *LotteryDrawCtl) Prize() { ruleId := t.MustGetInt64("lottery_draw_rule_id") list, err := models.GetLotteryDrawLadderByRuleId(ruleId) t.CheckErr(err) for index := range list { list[index].Des = "在该活动的所有用户中随机抽奖品数量的用户" } t.JSON(map[string]interface{}{ "total": len(list), "lise": list, }) } // 抽奖用户 func (t *LotteryDrawCtl) Users() { activityId := t.MustGetInt64("activity_id") ruleId := t.MustGetInt64("lottery_draw_rule_id") customerId := t.MustGetUID() customer := new(models.Customer) exist, err := models.Get(customer, customerId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") activity := new(models.Activity) exist, err = models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") area := new(models.AreaStore) exist, err = area.GetByCustomerId(customerId, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") moduleService, exist, err := activity_service.GetModuleService(define.MODULE_LOTTERY, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在") recordIds := make([]int64, 0) if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT { // 去重标志 recordIds, err = models.GetUserIdsByLotteryDrawRuleId(ruleId, activity.RehearsalId, area.Id) t.CheckErr(err) } result, err := lottery_service.GetLotteryUsersResult(area.Id, activity.Id, activity.RehearsalId, recordIds) t.CheckErr(err) t.JSON(map[string]interface{}{ "total": len(result), "list": result, }) } //抽奖动作 func (t *LotteryDrawCtl) Lottery() { activityId := t.MustGetInt64("activity_id") ruleId := t.MustGetInt64("lottery_draw_rule_id") ladderId := t.MustGetInt64("lottery_draw_ladder_id") number := t.MustGetInt("number") // 奖品数量 customerId := t.MustGetUID() customer := new(models.Customer) exist, err := models.Get(customer, customerId) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") 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) // 多地区设置 area := new(models.AreaStore) exist, err = area.GetByCustomerId(customerId, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在") rule := new(models.LotteryDrawRule) exist, err = models.Get(rule, ruleId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_NOT_EXIST, "抽奖规则不存在") // 查询奖品 ladder := new(models.LotteryDrawRuleLadder) exist, err = models.Get(ladder, ladderId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_NOT_EXIST, "抽奖等级不存在") t.CheckRunning(ladder.Status) count, err := new(models.LotteryDrawRecord).CountRecord(ruleId, ladder.Id, activity.RehearsalId, area.Id) t.CheckErr(err) prizeNum := ladder.PrizeNumber - int(count) if prizeNum <= 0 || prizeNum < number { // 需要抽奖的数量比数据库存在数量多 t.ERROR("奖品数量不足", code.MSG_LOTTERY_PRIZE_NOT_ENOUGH) } module, exist, err := activity_service.GetModuleService(define.MODULE_LOTTERY, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在") // 取设置 userIds, err := lottery_service.GetLotteryUserIds(module.BesideRepeat, activityId, ruleId, ladder.Id, activity.RehearsalId, area.Id) if len(userIds) < number { t.ERROR("抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH) } lottery_service.RandLotteryUserIds(userIds) // 打乱需要中奖人员 winnerIds := userIds[:number] winners, err := lottery_service.GetLotteryUsers(winnerIds) t.CheckErr(err) t.CheckErr(err) for i := range winners { // 补全信息 winners[i].PrizeName = ladder.PrizeName winners[i].LadderId = ladder.Id winners[i].PrizeImg = ladder.PrizeImg // 普通抽奖 userPrize := new(models.UserPrize) userPrize.ActivityId = activityId userPrize.RehearsalId = activity.RehearsalId userPrize.ActivityName = activity.Name userPrize.UserId = winners[i].UserId userPrize.PrizeImg = ladder.PrizeImg userPrize.PrizeName = ladder.PrizeName userPrize.PrizeType = 1 userPrize.IsDelete = false userPrize.CreatedAt = time.Now() userPrize.UpdatedAt = time.Now() _, err = models.Add(userPrize) t.CheckErr(err) record := new(models.LotteryDrawRecord) record.UserPrizeId = userPrize.Id record.ActivityId = activityId record.RehearsalId = activity.RehearsalId record.LotteryDrawActivityId = rule.LotteryDrawActivityId record.LotteryDrawRuleId = rule.Id record.UserId = winners[i].UserId record.UserName = winners[i].Username record.UserPhone = winners[i].UserPhone record.LotteryDrawRuleLadderId = ladder.Id record.PrizeName = ladder.PrizeName record.AreaId = area.Id record.AreaName = area.Name record.RollNum = ladder.RollNum record.IsDelete = false record.CreatedAt = time.Now() record.UpdatedAt = time.Now() _, err = models.Add(record) t.CheckErr(err) } // 通知直播开始抽奖 if activity.RehearsalId == 0 { live := new(models.LiveConfig) exist, err := live.GetByActivityId(activity.Id) t.CheckErr(err) if exist && live.HaveModule(define.MODULE_LOTTERY) { go im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawResult, map[string]interface{}{ "lottery_draw_ladder_id": ladder.Id, "winners": winners, "roll_num": ladder.RollNum, "timestamp": time.Now().Unix(), "desc": "抽奖结果", }) } } t.JSON(winners) } //获取中奖名单 func (t *LotteryDrawCtl) ListOfWinners() { ruleId := t.MustGetInt64("lottery_draw_rule_id") rehearsalId := t.MustGetInt64("rehearsal_id") result, err := lottery_service.GetWinnersResult(ruleId, rehearsalId) t.CheckErr(err) t.JSON(map[string]interface{}{ "total": len(result), "list": result, }) }