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" "github.com/ouxuanserver/osmanthuswine/src/core" ) //抽奖 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, "抽奖规则不存在") if ladder.Status != define.StatusNotBegin { t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR) } ladder.Status = define.StatusRunning ladder.UpdatedAt = time.Now() t.CheckErr(models.Save(map[string]interface{}{ "id=": ladderId, }, ladder, "status", "updated_at")) // 通知直播开始抽奖 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") 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) } // 通知直播开始抽奖 im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawRollStart, map[string]interface{}{ "lottery_draw_ladder_id": ladder.Id, "timestamp": time.Now().Unix(), "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") 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) } // 通知直播开始抽奖 im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawRollStop, map[string]interface{}{ "lottery_draw_ladder_id": ladder.Id, "timestamp": time.Now().Unix(), "desc": "停止滚动", "number": num, }) t.SUCCESS("操作成功") } //停止抽奖 func (t *LotteryDrawCtl) Stop() { 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, "抽奖规则不存在") if ladder.Status != define.StatusRunning { t.ERROR(fmt.Sprintf("该活动%s", ladder.Status), code.MSG_ERR) } ladder.Status = define.StatusEnding ladder.UpdatedAt = time.Now() t.CheckErr(models.Save(map[string]interface{}{ "id=": ladderId, }, ladder, "status", "updated_at")) // 通知直播开始抽奖 im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawStop, map[string]interface{}{ "lottery_draw_ladder_id": ladder.Id, "timestamp": time.Now().Unix(), "desc": "结束抽奖", }) t.SUCCESS("操作成功") } type LotteryListResult struct { LotteryDrawActivityId int64 `json:"lottery_draw_activity_id"` LotteryDrawRuleId int64 `json:"lottery_draw_rule_id"` LotteryDrawActivityName string `json:"lottery_draw_name"` LotteryDrawLadders []*LotteryLadderResult `json:"lottery_draw_ladders"` PrizeNumber int64 `json:"prize_number"` } type LotteryLadderResult struct { LotteryDrawRuleId int64 `json:"-"` LotteryDrawLadderId int64 `json:"lottery_draw_ladder_id"` Status string `json:"status"` PrizeName string `json:"prize_name"` PrizeImg string `json:"prize_img"` PrizeNumber int64 `json:"prize_number"` } //获取所有抽奖活动列表 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 := make([]*LotteryListResult, 0) err = core.GetXormAuto().Table(new(models.LotteryDrawActivity)).Alias("a"). Select("a.id as lottery_draw_activity_id, r.id as lottery_draw_rule_id, a.lottery_draw_activity_name"). Join("LEFT", models.AliasTableName(new(models.LotteryDrawRule), "r"), "a.id=r.lottery_draw_activity_id and r.is_delete=0"). Where("a.is_delete=0 and a.activity_id=?", activityId).Find(&result) t.CheckErr(err) // 多地区的一个坑:分配给主会场的area_id不是主账号的area_id area := new(models.AreaStore) if customer.Pid == 0 { exist, err = area.GetMainAreaById(activityId) } else { exist, err = area.GetAreaStoreById(t.MustGetAreaId()) } t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") ruleIds := make([]int64, 0) for _, v := range result { ruleIds = append(ruleIds, v.LotteryDrawRuleId) } ladders := make([]*LotteryLadderResult, 0) err = core.GetXormAuto().Table(new(models.LotteryDrawRuleLadder)). Select("id as lottery_draw_ladder_id, prize_name, prize_img, prize_number, lottery_draw_rule_id, status"). Where("is_delete=0").In("lottery_draw_rule_id", ruleIds).Find(&ladders) t.CheckErr(err) ladderIds := make([]int64, 0) for _, ladder := range ladders { ladderIds = append(ladderIds, ladder.LotteryDrawLadderId) } records := make([]map[string]int64, 0) err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Alias("r"). Select("r.lottery_draw_rule_ladder_id as ladder_id, count(r.id) as num"). Where("is_delete=0 and rehearsal_id=?", activity.RehearsalId). In("r.lottery_draw_rule_ladder_id", ladderIds).GroupBy("ladder_id").Find(&records) t.CheckErr(err) for i := range ladders { for j := range records { if ladders[i].LotteryDrawLadderId == records[j]["ladder_id"] { ladders[i].PrizeNumber = ladders[i].PrizeNumber - records[j]["num"] } } } for i := range result { for j := range ladders { if result[i].LotteryDrawRuleId == ladders[j].LotteryDrawRuleId { result[i].PrizeNumber += ladders[j].PrizeNumber result[i].LotteryDrawLadders = append(result[i].LotteryDrawLadders, ladders[j]) } } } core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Select("count()") t.JSON(map[string]interface{}{ "total": len(result), "list": result, }) } //抽奖奖品 func (t *LotteryDrawCtl) Prize() { ruleId := t.MustGetInt64("lottery_draw_rule_id") list := make([]*models.LotteryDrawRuleLadder, 0) err := core.GetXormAuto().Where("is_delete=0 and lottery_draw_rule_id=?", ruleId).Find(&list) t.CheckErr(err) for index := range list { list[index].Des = "在该活动的所有用户中随机抽奖品数量的用户" } t.JSON(map[string]interface{}{ "total": len(list), "lise": list, }) } type LotteryUsersResult struct { UserId int64 `json:"user_id"` Username string `json:"username"` Avatar string `json:"avatar"` } // 抽奖用户 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) if customer.Pid == 0 { exist, err = area.GetMainAreaById(activityId) } else { exist, err = area.GetAreaStoreById(t.MustGetAreaId()) } t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_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=?", area.Id, 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", ruleId, activity.RehearsalId, area.Id).Find(&recordIds) t.CheckErr(err) session = session.NotIn("h.user_id", recordIds) } err = session.Find(&result) 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) if customer.Pid == 0 { exist, err = area.GetMainAreaById(activity.Id) } else { exist, err = area.GetAreaStoreById(t.MustGetAreaId()) } 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 := core.GetXormAuto().Where("lottery_draw_rule_id=? and lottery_draw_rule_ladder_id=? "+ "and rehearsal_id=? and area_id=? and is_delete=0", ruleId, ladderId, activity.RehearsalId, area.Id). Count(new(models.LotteryDrawRecord)) t.CheckErr(err) prizeNum := ladder.PrizeNumber - int(count) if prizeNum <= 0 || prizeNum < number { // 需要抽奖的数量比数据库存在数量多 t.ERROR("奖品数量不足", code.MSG_LOTTERY_PRIZE_NOT_ENOUGH) } //根据主活动id和地区id获取所有的用户 signUpIds := make([]int64, 0) err = core.GetXormAuto().Table(new(models.SignUp)).Select("id").Where("activity_id=? and is_delete=0", activityId).Find(&signUpIds) t.CheckErr(err) if len(signUpIds) <= 0 { t.ERROR("暂无签到用户", code.MSG_DATA_NOT_EXIST) } moduleService, exist, err := activity_service.GetModuleService(define.MODULE_LOTTERY, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "活动模块不存在") // 取设置 lotteryUsers := make([]*lottery_service.LotteryUser, 0) 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", ruleId, activity.RehearsalId, area.Id).Find(&recordIds) t.CheckErr(err) //查询已经中奖的用户,剔除已经中奖的用户 err = core.GetXormAuto().Table(new(models.SignHistory)).Select("user_id"). Where("is_delete=0 and rehearsal_id=? and area_id=?", activity.RehearsalId, area.Id).In("sign_rule_id", signUpIds). NotIn("user_id", recordIds).Find(&lotteryUsers) t.CheckErr(err) } else { // 同轮次剔除中奖用户 // 去除同规则中将用户 recordIds := make([]int64, 0) err = core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Select("user_id"). Where("lottery_draw_rule_ladder_id=? and rehearsal_id=? and area_id=? and is_delete=0", ladderId, activity.RehearsalId, area.Id).Find(&recordIds) t.CheckErr(err) err = core.GetXormAuto().Table(new(models.SignHistory)).Select("user_id"). Where("is_delete=0 and rehearsal_id=? and area_id=?", activity.RehearsalId, area.Id).In("sign_rule_id", signUpIds). NotIn("user_id", recordIds).Find(&lotteryUsers) t.CheckErr(err) } if len(lotteryUsers) < number { t.ERROR("抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH) } lottery_service.RandLotteryUser(lotteryUsers) // 打乱需要中奖人员 winners := lotteryUsers[:number] userIds := make([]int64, 0) for _, winner := range winners { userIds = append(userIds, winner.UserId) } users := make([]*models.User, 0) err = core.GetXormAuto().Where("is_delete=0").In("id", userIds).Find(&users) t.CheckErr(err) for i := range winners { // 补全信息 for j := range users { if winners[i].UserId == users[j].Id { winners[i].Username = users[j].Nickname winners[i].Avatar = users[j].Avatar winners[i].UserPhone = users[j].Phone winners[i].PrizeName = ladder.PrizeName winners[i].LadderId = ladder.Id winners[i].PrizeImg = ladder.PrizeImg break } } // 普通抽奖 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 = core.GetXormAuto().Insert(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.IsDelete = false record.CreatedAt = time.Now() record.UpdatedAt = time.Now() _, err = core.GetXormAuto().InsertOne(record) t.CheckErr(err) } // 通知直播开始抽奖 im_service.SendGroupCustomMessage("admin", activityId, im_service.NoticeLotteryDrawResult, map[string]interface{}{ "lottery_draw_ladder_id": ladder.Id, "winners": winners, "timestamp": time.Now().Unix(), "desc": "抽奖结果", }) t.JSON(winners) } 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 *LotteryDrawCtl) ListOfWinners() { ruleId := t.MustGetInt64("lottery_draw_rule_id") rehearsalId := t.MustGetInt64("rehearsal_id") result := make([]*WinnersResult, 0) err := core.GetXormAuto().Table("ox_lottery_draw_record").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_id=? and record.rehearsal_id=?", ruleId, rehearsalId). Find(&result) t.CheckErr(err) t.JSON(map[string]interface{}{ "total": len(result), "list": result, }) }