diff --git a/controllers/client/lottery.go b/controllers/client/lottery.go index bb3061c..9426068 100644 --- a/controllers/client/lottery.go +++ b/controllers/client/lottery.go @@ -4,6 +4,7 @@ import ( "hudongzhuanjia/controllers" "hudongzhuanjia/models" activity_service "hudongzhuanjia/services/activity" + lottery_service "hudongzhuanjia/services/lottery" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" ) @@ -102,12 +103,6 @@ func (t *LotteryCtl) Winners() { }) } -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") @@ -131,12 +126,11 @@ func (t *LotteryCtl) Users() { recordIds := make([]int64, 0) if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT { // 去重标志 - err = models.GetUserIdsByLotteryDrawRuleId(recordIds, ladder.LotteryDrawRuleId, activity.RehearsalId, areaId) + recordIds, err = models.GetUserIdsByLotteryDrawRuleId(ladder.LotteryDrawRuleId, activity.RehearsalId, areaId) t.CheckErr(err) } - result := make([]*LotteryUsersResult, 0) - err = models.GetLotteryUsersResultBySignHistory(result, areaId, activity.Id, activity.RehearsalId, recordIds) + result, err := lottery_service.GetLotteryUsersResult(areaId, activity.Id, activity.RehearsalId, recordIds) t.CheckErr(err) t.JSON(map[string]interface{}{ diff --git a/controllers/client/reward.go b/controllers/client/reward.go index 9fc0682..668928b 100644 --- a/controllers/client/reward.go +++ b/controllers/client/reward.go @@ -72,12 +72,10 @@ func (t *RewardCtl) Reward() { RehearsalId: activity.RehearsalId, Status: -1, ReviewTime: 0, - IsDelete: false, - CreatedAt: time.Now(), - UpdatedAt: time.Now(), + Type: _type, } - if activity.RehearsalId != 0 { // 彩排不需要支付 + if activity.RehearsalId != 0 && _type == 0 { // 线下h5彩排不需要支付 history.Status = 0 } diff --git a/controllers/client/sign.go b/controllers/client/sign.go index 3f0fe94..b486060 100644 --- a/controllers/client/sign.go +++ b/controllers/client/sign.go @@ -171,7 +171,7 @@ func (t *SignCtl) RealSign() { rid := t.MustGetInt64("rehearsal_id") user := new(models.User) - exist, err := user.GetById(uid) + exist, err := models.Get(user, uid) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") diff --git a/controllers/pc/activity.go b/controllers/pc/activity.go index ad2ccae..9bd0666 100644 --- a/controllers/pc/activity.go +++ b/controllers/pc/activity.go @@ -2,7 +2,6 @@ package pc import ( "fmt" - "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" "hudongzhuanjia/utils/code" @@ -16,20 +15,6 @@ type ActivityCtl struct { //controllers.BaseCtl } -// 废弃 -func (t *ActivityCtl) List() { - customerId := t.MustGetUID() - list := make([]*models.Activity, 0) - err := core.GetXormAuto().Where("is_delete=0 and customer_id=? and status<>?", - customerId, define.StatusEnding).Find(&list) - t.CheckErr(err) - - t.JSON(map[string]interface{}{ - "list": list, - "total": len(list), - }) -} - func (t *ActivityCtl) StartActivity() { activityId := t.MustGetInt64("activity_id") mode := t.MustGetInt("mode") // 标识彩排 mode=0正式 mode=1彩排 @@ -51,32 +36,26 @@ func (t *ActivityCtl) StartActivity() { t.ERROR("该活动在彩排中", code.MSG_ERR) } - var rehearsalId int64 = 0 if mode == 1 { - rehearsal := new(models.Rehearsal) - rehearsal.Status = 1 - rehearsal.ActivityId = activityId - rehearsal.CustomerId = uid - rehearsal.LimitNumber = limit - rehearsal.IsDelete = false - rehearsal.UpdatedAt = time.Now() - rehearsal.CreatedAt = time.Now() - _, err := core.GetXormAuto().InsertOne(rehearsal) + rehearsal := models.Rehearsal{ + CustomerId: uid, + ActivityId: activityId, + LimitNumber: limit, + Status: 1, + IsDelete: false, + } + _, err = models.Add(rehearsal) t.CheckErr(err) - rehearsalId = rehearsal.Id + activity.RehearsalId = rehearsal.Id } activity.Status = define.StatusRunning - activity.RehearsalId = rehearsalId activity.UpdatedAt = time.Now() // todo: 校验 - err = models.Save(map[string]interface{}{ - "id=": activityId, - "is_delete=": 0, - }, activity, "status", "rehearsal_id", "updated_at") + _, err = models.Update(activity.Id, "status", "rehearsal_id") t.CheckErr(err) t.JSON(map[string]interface{}{ - "rehearsal_id": rehearsalId, + "rehearsal_id": activity.RehearsalId, }) } @@ -93,44 +72,41 @@ func (t *ActivityCtl) StopActivity() { } if activity.RehearsalId != 0 { // 彩排回归最初结果 - // todo:其他的活动也要回复到最初的状态 // 竞拍 _, err = models.UpdateAuctionStatusByActivityId(activityId) t.CheckErr(err) - // 抽奖 + // 抽奖 -> 抽奖活动->抽奖规则-> 抽奖阶梯 lotteryIds, err := models.GetLDActivityIdsByActivityId(activityId) t.CheckErr(err) - //_, err = models.UpdateLDRuleStatusByLDActiviytIds(lotteryIds) lotteryRuleIds, err := models.GetLDRuleIdsByLDActivityId(lotteryIds) t.CheckErr(err) _, err = models.UpdateLDLadderStatusByLDRuleIds(lotteryRuleIds) t.CheckErr(err) - // 订单抽奖 + // 订单抽奖-> 订单抽奖活动->抽奖规则->抽奖阶梯 orderIds, err := models.GetODActivityIdsByActivityId(activityId) t.CheckErr(err) - //_, err = models.UpdateODRuleStatusByODActivityIds(orderIds) orderRuleIds, err := models.GetODRuleIdsByODActivityIds(orderIds) t.CheckErr(err) _, err = models.UpdateODLadderStatusByODRuleIds(orderRuleIds) t.CheckErr(err) - // 摇红包 + // 摇红包->摇红包活动->摇红包规则 shakeRbIds, err := models.GetSREActivityIdsByActivityId(activityId) t.CheckErr(err) _, err = models.UpdateSRERuleStatusBySREActivityIds(shakeRbIds) t.CheckErr(err) - // 拔河 + // 拔河->拔河活动 _, err = models.UpdateTOWStatusByActivityId(activityId) t.CheckErr(err) - // 投票 + // 投票->投票活动 _, err = models.UpdateVoteStatusByActiviytId(activityId) t.CheckErr(err) - // 卡路里 + // 卡路里->卡路里活动 _, err = models.UpdateCalorieStatusByActivityId(activityId) t.CheckErr(err) @@ -142,10 +118,8 @@ func (t *ActivityCtl) StopActivity() { } else { activity.Status = define.StatusEnding } - activity.UpdatedAt = time.Now() activity.RehearsalId = 0 - models.Save(map[string]interface{}{ - "id=": activity.Id, - }, activity, "status", "rehearsal_id", "updated_at") + _, err = models.Update(activity.Id, activity, "status", "rehearsal_id") + t.CheckErr(err) t.SUCCESS("success") } diff --git a/controllers/pc/area_store.go b/controllers/pc/area_store.go index 936dce9..03e95a6 100644 --- a/controllers/pc/area_store.go +++ b/controllers/pc/area_store.go @@ -10,14 +10,6 @@ type AreaStoreCtl struct { controllers.AuthorCtl } -// all area store doc -// @Summary activity -// @Description get all area store -// @Tags pc -// @Accept json -// @Produce json -// @Success 200 {string} string "success" -// @Router /Pc/ActivityCtl/list [get] func (t *AreaStoreCtl) List() { activityId := t.MustGetInt64("activity_id") diff --git a/controllers/pc/auction.go b/controllers/pc/auction.go index f7c9698..1375d6c 100644 --- a/controllers/pc/auction.go +++ b/controllers/pc/auction.go @@ -2,7 +2,6 @@ package pc import ( "fmt" - "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" ws_send_service "hudongzhuanjia/services/ws_send" @@ -51,8 +50,7 @@ func (t *AuctionCtl) StartAuction() { auction.UpdatedAt = time.Now() duration, _ := strconv.ParseInt(auction.AuctionDuration, 10, 64) auction.AuctionEndTime = time.Now().Unix() + duration - _, err = core.GetXormAuto().Where("is_delete=0 and id=?", auctionId). - Cols("status", "auction_end_time", "updated_at").Update(auction) + _, err = models.Update(auction.Id, auction, "status", "auction_end_time") t.CheckErr(err) t.SUCCESS("操作成功") @@ -60,7 +58,7 @@ func (t *AuctionCtl) StartAuction() { func (t *AuctionCtl) StopAuction() { auctionId := t.MustGetInt64("auction_activity_id") - rehearsalId := t.MustGetInt64("rehearsal_id") + auction := new(models.NewAuctionActivity) exist, err := models.Get(auction, auctionId) t.CheckErr(err) @@ -72,14 +70,17 @@ func (t *AuctionCtl) StopAuction() { auction.Status = define.StatusEnding auction.UpdatedAt = time.Now() auction.AuctionEndTime = 0 // 消除结束时间计时 - _, err = core.GetXormAuto().Where("is_delete=0 and id=?", auctionId). - Cols("status", "auction_end_time", "updated_at").Update(auction) + _, err = models.Update(auction.Id, auction, "status", "auction_end_time") + t.CheckErr(err) + + activity := new(models.Activity) + _, err = models.Get(activity, auction.ActivityId) t.CheckErr(err) // todo: 记录用户最高价 if auction.AuctionModel == define.INCR_AUCTION { history := new(models.AuctionHistory) - _, err := history.GetHighestMoney(rehearsalId, auction.Id) // 区分彩排数据 + _, err := history.GetHighestMoney(activity.RehearsalId, auction.Id) // 区分彩排数据 t.CheckErr(err) user := new(models.User) @@ -88,7 +89,7 @@ func (t *AuctionCtl) StopAuction() { if exist { record := new(models.AuctionResultRecord) record.AuctionActivityId = auctionId - record.RehearsalId = rehearsalId // 彩排 + record.RehearsalId = history.RehearsalId // 彩排 record.AuctionGoodsName = auction.AuctionGoodsName record.ActivityId = auction.ActivityId record.UserId = history.UserId @@ -100,6 +101,8 @@ func (t *AuctionCtl) StopAuction() { record.UpdatedAt = time.Now() record.CreatedAt = time.Now() record.IsDelete = false + _, err = models.Add(record) + t.CheckErr(err) go ws_send_service.SendAuction(fmt.Sprintf("%d", record.ActivityId), "", 0, map[string]interface{}{ @@ -116,8 +119,7 @@ func (t *AuctionCtl) StopAuction() { "model": auction.AuctionModel, }, }) - _, err = core.GetXormAuto().InsertOne(record) - t.CheckErr(err) + } } else { go ws_send_service.SendAuction(fmt.Sprintf("%d", auction.ActivityId), diff --git a/controllers/pc/barrage.go b/controllers/pc/barrage.go index 02318a4..da1a20e 100644 --- a/controllers/pc/barrage.go +++ b/controllers/pc/barrage.go @@ -39,10 +39,8 @@ func (t *BarrageCtl) SaveSetting() { server.DanmuPosition = t.MustGet("position") server.DanmuSpeed = t.MustGetInt("speed") server.ServerDisplay = t.MustGet("display") - models.Save(map[string]interface{}{ - "is_delete=": 0, - "id=": danmuId, - }, server, "danmu_font_size", "danmu_opacity", "danmu_position", "danmu_speed", "server_display") + _, err = models.Update(danmuId, server, "danmu_font_size", + "danmu_opacity", "danmu_position", "danmu_speed", "server_display") t.CheckErr(err) t.SUCCESS("success") } diff --git a/controllers/pc/bully_screen.go b/controllers/pc/bully_screen.go index 324bb20..505d053 100644 --- a/controllers/pc/bully_screen.go +++ b/controllers/pc/bully_screen.go @@ -44,11 +44,17 @@ func (t *BullyScreenCtl) WaitReview() { //审核操作 func (t *BullyScreenCtl) Review() { customerId := t.MustGetUID() - idList := t.MustGet("ids") status := t.MustGetBool("status") - ids := strings.Split(idList, "|") + ids := strings.Split(t.MustGet("ids"), "|") + //activityId := t.MustGetInt64("activity_id") rehearsalId := t.MustGetInt64("rehearsal_id") + //// TODO: new feat + //activity := new(models.Activity) + //exist, err := models.Get(activity, activityId) + //t.CheckErr(err) + //t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") + //将金额退回给用户 result, err := models.GetBullyScreenHistoryByIds(ids) t.CheckErr(err) diff --git a/controllers/pc/calorie.go b/controllers/pc/calorie.go index 59015f9..6d7f90b 100644 --- a/controllers/pc/calorie.go +++ b/controllers/pc/calorie.go @@ -2,9 +2,9 @@ package pc import ( "fmt" - "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" + calorie_service "hudongzhuanjia/services/calorie" "hudongzhuanjia/utils" "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" @@ -87,8 +87,7 @@ func (t *CalorieCtl) Stop() { // 列出轮次, 包括倒计时 func (t *CalorieCtl) List() { activityId := t.MustGetInt64("activity_id") - calories := make([]*models.Calorie, 0) - err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).Find(&calories) + calories, err := models.GetCaloriesByActivityId(activityId) t.CheckErr(err) t.JSON(map[string]interface{}{ "list": calories, @@ -119,16 +118,6 @@ func (t *CalorieCtl) Qrcode() { }) } -type CalorieCountResult struct { - Id int64 `json:"id"` - ActivityId int64 `json:"activity_id"` - CalorieId int64 `json:"calorie_id"` - UserId int64 `json:"user_id"` - Avatar string `json:"avatar"` - Nickname string `json:"nickname"` - Score int64 `json:"score"` -} - // 统计一些数据, 包括参与人数, 包括当前排名 func (t *CalorieCtl) Count() { activityId := t.MustGetInt64("activity_id") @@ -147,38 +136,15 @@ func (t *CalorieCtl) Count() { t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "卡路里不存在") // 统计人数 - count, err := core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=?", - calorieId, activity.RehearsalId).Count(new(models.CalorieUser)) + count, err := new(models.CalorieUser).Count(calorieId, activity.RehearsalId) t.CheckErr(err) // 统计排名 - result := make([]*CalorieCountResult, 0) - err = core.GetXormAuto().Table(new(models.CalorieUser)).Alias("c"). - Select("c.id, c.activity_id, c.calorie_id, c.user_id, u.avatar, u.nickname, c.score, c.join_time"). - Join("LEFT", new(models.User).Alias("u"), "c.user_id=u.id and u.is_delete=0"). - Where("c.is_delete=0 and c.calorie_id=? and c.rehearsal_id=?", calorieId, activity.RehearsalId). - Limit(limit).Desc("c.score").Asc("c.join_time").Find(&result) - t.CheckErr(err) - //duration := calorie.GameDuration - (time.Now().Unix() - calorie.StartTime) + 6 // 增加6s 倒计时 - //if calorie.StartTime == 0 { - // duration = calorie.GameDuration - //} - - // 3s 倒计时 - //var countDown float64 = 0 - //if duration-calorie.GameDuration > 0 { - // countDown = math.Ceil(float64(duration-calorie.GameDuration) / 2.0) - //} - - //if calorie.Status == define.StatusEnding || duration <= 0 { - //duration = 0 - //} + result, err := calorie_service.RankCalorieUser(calorieId, activity.RehearsalId, limit) + t.CheckErr(err) t.JSON(map[string]interface{}{ "list": result, // 排名统计 "count": count, // 人数统计 - //"time": duration - 2*int64(countDown), // 时长 - //"count_down": countDown, //倒计时 - //"status": calorie.Status, // 状态 }) } diff --git a/controllers/pc/lottery_draw.go b/controllers/pc/lottery_draw.go index ab90aa9..61cdcd8 100644 --- a/controllers/pc/lottery_draw.go +++ b/controllers/pc/lottery_draw.go @@ -10,8 +10,6 @@ import ( "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "time" - - "github.com/ouxuanserver/osmanthuswine/src/core" ) //抽奖 @@ -132,23 +130,6 @@ func (t *LotteryDrawCtl) Stop() { 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") @@ -164,63 +145,7 @@ func (t *LotteryDrawCtl) List() { 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()") + result, err := lottery_service.GetLotteryAndLadder(activityId, activity.RehearsalId) t.JSON(map[string]interface{}{ "total": len(result), @@ -232,8 +157,7 @@ func (t *LotteryDrawCtl) List() { 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) + list, err := models.GetLotteryDrawLadderByRuleId(ruleId) t.CheckErr(err) for index := range list { @@ -245,12 +169,6 @@ func (t *LotteryDrawCtl) Prize() { }) } -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") @@ -268,34 +186,20 @@ func (t *LotteryDrawCtl) Users() { 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()) - } + exist, err = area.GetByCustomerId(customerId) 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, "活动模块不存在") + recordIds := make([]int64, 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) + recordIds, err = models.GetUserIdsByLotteryDrawRuleId(ruleId, activity.RehearsalId, area.Id) t.CheckErr(err) - session = session.NotIn("h.user_id", recordIds) } - err = session.Find(&result) + result, err := lottery_service.GetLotteryUsersResult(area.Id, activity.Id, activity.RehearsalId, recordIds) t.CheckErr(err) t.JSON(map[string]interface{}{ @@ -325,11 +229,7 @@ func (t *LotteryDrawCtl) Lottery() { // 多地区设置 area := new(models.AreaStore) - if customer.Pid == 0 { - exist, err = area.GetMainAreaById(activity.Id) - } else { - exist, err = area.GetAreaStoreById(t.MustGetAreaId()) - } + exist, err = area.GetByCustomerId(customerId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在") @@ -345,9 +245,7 @@ func (t *LotteryDrawCtl) Lottery() { 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)) + count, err := new(models.LotteryDrawRecord).CountRecord(ruleId, ladder.Id, activity.RehearsalId, area.Id) t.CheckErr(err) prizeNum := ladder.PrizeNumber - int(count) @@ -355,74 +253,26 @@ func (t *LotteryDrawCtl) Lottery() { 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) + module, 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 { + 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.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) + lottery_service.RandLotteryUserIds(userIds) // 打乱需要中奖人员 + winnerIds := userIds[:number] + winners, err := lottery_service.GetLotteryUsers(winnerIds) + t.CheckErr(err) + 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 - } - } + winners[i].PrizeName = ladder.PrizeName + winners[i].LadderId = ladder.Id + winners[i].PrizeImg = ladder.PrizeImg + // 普通抽奖 userPrize := new(models.UserPrize) userPrize.ActivityId = activityId @@ -472,24 +322,12 @@ func (t *LotteryDrawCtl) Lottery() { 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) + result, err := lottery_service.GetWinnersResult(ruleId, rehearsalId) t.CheckErr(err) t.JSON(map[string]interface{}{ "total": len(result), diff --git a/controllers/pc/order_draw.go b/controllers/pc/order_draw.go index 8466ae2..5f3526a 100644 --- a/controllers/pc/order_draw.go +++ b/controllers/pc/order_draw.go @@ -2,6 +2,7 @@ package pc import ( "fmt" + "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" activity_service "hudongzhuanjia/services/activity" @@ -10,8 +11,6 @@ import ( "hudongzhuanjia/utils/code" "hudongzhuanjia/utils/define" "time" - - "github.com/ouxuanserver/osmanthuswine/src/core" ) // 订单 @@ -273,11 +272,7 @@ func (t *OrderDrawCtl) Draw() { 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()) - } + exist, err = area.GetByCustomerId(customerId) t.CheckErr(err) t.Assert(exist, code.MSG_DATA_NOT_EXIST, "地区不存在") @@ -285,7 +280,6 @@ func (t *OrderDrawCtl) Draw() { exist, err = models.Get(rule, ruleId) t.CheckErr(err) t.Assert(exist, code.MSG_ORDER_RULE_NOT_EXIST, "订单抽奖规则不存在") - //t.CheckRunning(rule.OrderDrawStatus) // 查询奖品 ladder := new(models.OrderDrawRuleLadder) @@ -302,60 +296,23 @@ func (t *OrderDrawCtl) Draw() { t.ERROR("奖品数量不足", code.MSG_LOTTERY_PRIZE_NOT_ENOUGH) } - moduleService, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id) + module, exist, err := activity_service.GetModuleService(define.MODULE_ORDERLY, activity.Id) t.CheckErr(err) t.Assert(exist, code.MSG_MODULE_NOT_EXIST, "模块服务不存在") - - orderUsers := make([]*lottery_service.LotteryUser, 0) - if moduleService.BesideRepeat == define.MODULE_BESIDE_REPEAT { - //查询已经中奖的用户,剔除已经中奖的用户 - recordIds := make([]int64, 0) - err = core.GetXormAuto().Table(new(models.OrderDrawRecord)).Select("user_id"). - Where("order_draw_rule_id=? and rehearsal_id=? and is_delete=0", - ruleId, activity.RehearsalId).Find(&recordIds) - - err = core.GetXormAuto().Table(new(models.CustomerOrder)). - Select("buyer_id as user_id").Distinct("buyer_id"). - Where("activity_id=? and rehearsal_id=? and is_delete=0", - activity.Id, activity.RehearsalId).NotIn("buyer_id", recordIds). - Find(&orderUsers) - t.CheckErr(err) - if len(orderUsers) < 0 { - t.ERROR("剔除已经中奖的用户后,已经没有用户可以抽了", code.MSG_ERR) - } - } else { - // 不去除 - err = core.GetXormAuto().Table(new(models.CustomerOrder)).Select("buyer_id as user_id").Distinct("buyer_id"). - Where("activity_id=? and rehearsal_id=? and is_delete=0", - activity.Id, activity.RehearsalId).Find(&orderUsers) - t.CheckErr(err) - } - if len(orderUsers) < number { + userIds, err := lottery_service.GetOrderLotteryUserIds(module.BesideRepeat, activity.Id, rule.Id, ladder.Id, activity.RehearsalId, area.Id) + if len(userIds) < number { t.ERROR("订单抽奖人数不足", code.MSG_LOTTERY_PEOPLE_NOT_ENOUGH) } - lottery_service.RandLotteryUser(orderUsers) - winners := orderUsers[:number] - userIds := make([]int64, 0) - for _, winner := range winners { - userIds = append(userIds, winner.UserId) - } + lottery_service.RandLotteryUserIds(userIds) + winnerIds := userIds[:number] - users := make([]*models.User, 0) - err = core.GetXormAuto().Where("is_delete=0").In("id", userIds).Find(&users) + winners, err := lottery_service.GetLotteryUsers(winnerIds) 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].LadderId = ladder.Id - winners[i].PrizeImg = ladder.PrizeImg - winners[i].PrizeName = ladder.PrizeName - break - } - } + winners[i].LadderId = ladder.Id + winners[i].PrizeImg = ladder.PrizeImg + winners[i].PrizeName = ladder.PrizeName // 订单抽奖 ==> 存入我的奖品 userPrize := new(models.UserPrize) userPrize.ActivityId = activityId diff --git a/controllers/pc/reward.go b/controllers/pc/reward.go index caa03ca..dcebadb 100644 --- a/controllers/pc/reward.go +++ b/controllers/pc/reward.go @@ -2,7 +2,6 @@ package pc import ( "fmt" - "github.com/ouxuanserver/osmanthuswine/src/core" "hudongzhuanjia/controllers" "hudongzhuanjia/models" bully_reward_service "hudongzhuanjia/services/bully_reward" @@ -101,20 +100,20 @@ func (t *RewardCtl) Review() { _, err = models.Update(v.Id, v, "status") t.CheckErr(err) + if rehearsalId != 0 && v.Type == 0 { // 线下h5彩排不需要金额 + continue + } + go im_service.SendGroupCustomMessage(v.UserId, v.ActivityId, im_service.NoticeReward, map[string]interface{}{ "amount": v.Amount, "content": v.Content, }) - if rehearsalId != 0 { // 彩排不需要金额 - continue - } wallet := new(models.RewardWallet) _, err = wallet.IncrBalance(uid, v.Amount) t.CheckErr(err) - - _, err = core.GetXormAuto().InsertOne(&models.RewardWalletHistory{ + history := models.RewardWalletHistory{ CustomerId: uid, Money: v.Amount, Type: "打赏", @@ -123,9 +122,8 @@ func (t *RewardCtl) Review() { RewardAccount: v.RewardAmount, Status: "交易成功", Mark: "打赏金额", - CreatedAt: time.Now(), - UpdatedAt: time.Now(), - }) + } + _, err = models.Add(history) t.CheckErr(err) } } @@ -136,16 +134,18 @@ func (t *RewardCtl) Review() { //获取打赏得总金额 func (t *RewardCtl) Amount() { activityId := t.MustGetInt64("activity_id") - rehearsalId := t.MustGetInt64("rehearsal_id") + activity := new(models.Activity) + exist, err := models.Get(activity, activityId) + t.CheckErr(err) + t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") //获取打赏服务得id server := new(models.RewardServer) - exist, err := server.GetByActivityId(activityId) + exist, err = server.GetByActivityId(activityId) t.CheckErr(err) t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在") - totalAmount, err := core.GetXormAuto().Where("reward_server_id=? and status=3 and is_delete=0 "+ - " and rehearsal_id=?", server.Id, rehearsalId).Sum(new(models.RewardHistory), "amount") + totalAmount, err := new(models.RewardHistory).SumMoney(server.Id, activity.RehearsalId) t.CheckErr(err) t.DisplayByData(map[string]interface{}{ diff --git a/models/CalorieUser.go b/models/CalorieUser.go index cc84ced..5600284 100644 --- a/models/CalorieUser.go +++ b/models/CalorieUser.go @@ -46,3 +46,8 @@ func GetCalorieUsersByCalorieIdAndScore(calorieId, rehearsalId, score interface{ calorieId, rehearsalId, score).Desc("score").Asc("join_time").Find(&users) return users, err } + +func (t *CalorieUser) Count(calorieId, rehearsalId interface{}) (int64, error) { + return core.GetXormAuto().Where("is_delete=0 and calorie_id=? and rehearsal_id=?", + calorieId, rehearsalId).Count(t) +} diff --git a/models/calorie.go b/models/calorie.go index a8fe911..67023c9 100644 --- a/models/calorie.go +++ b/models/calorie.go @@ -45,3 +45,9 @@ func UpdateCalorieStatusByActivityId(aid int64) (int64, error) { return core.GetXormAuto().Where("is_delete=0 and activity_id=?", aid). Update(&Calorie{Status: define.StatusNotBegin}) } + +func GetCaloriesByActivityId(activityId interface{}) ([]*Calorie, error) { + calories := make([]*Calorie, 0) + err := core.GetXormAuto().Where("is_delete=0 and activity_id=?", activityId).Find(&calories) + return calories, err +} diff --git a/models/customer_order_option.go b/models/customer_order_option.go index 430abb2..4d665b7 100644 --- a/models/customer_order_option.go +++ b/models/customer_order_option.go @@ -41,5 +41,6 @@ func (t *CustomerOrderOption) GetOpen(aid int64) (bool, error) { func (t *CustomerOrderOption) Switch(aid int64, status int) (int64, error) { t.Status = status - return core.GetXormAuto().Where("is_delete=0 and activity_id=?", aid).Cols("status").Update(t) + return core.GetXormAuto().Where("is_delete=0 and activity_id=?", aid). + Cols("status").Update(t) } diff --git a/models/lottery_draw_record.go b/models/lottery_draw_record.go index abce086..dbd7c93 100644 --- a/models/lottery_draw_record.go +++ b/models/lottery_draw_record.go @@ -65,8 +65,16 @@ func GetLotteryUsersResultByLotteryDrawRecord(obj, ladderId, rehearsalId interfa return session.Find(&obj) } -func GetUserIdsByLotteryDrawRuleId(obj, ruleId, rehearsalId, areaId interface{}) error { - return core.GetXormAuto().Table(new(LotteryDrawRecord)).Select("user_id"). +func GetUserIdsByLotteryDrawRuleId(ruleId, rehearsalId, areaId interface{}) ([]int64, error) { + recordIds := make([]int64, 0) + err := core.GetXormAuto().Table(new(LotteryDrawRecord)).Select("user_id"). Where("lottery_draw_rule_id=? and rehearsal_id=? and area_id=? and is_delete=0", - ruleId, rehearsalId, areaId).Find(&obj) + ruleId, rehearsalId, areaId).Find(&recordIds) + return recordIds, err +} + +func (t *LotteryDrawRecord) CountRecord(ruleId, ladderId, rehearsalId, areaId interface{}) (int64, error) { + return 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, rehearsalId, areaId). + Count(t) } diff --git a/models/lottery_draw_rule.go b/models/lottery_draw_rule.go index 87ffec0..c78dceb 100644 --- a/models/lottery_draw_rule.go +++ b/models/lottery_draw_rule.go @@ -2,7 +2,6 @@ package models import ( "github.com/ouxuanserver/osmanthuswine/src/core" - "hudongzhuanjia/utils/define" "time" ) @@ -16,7 +15,6 @@ type LotteryDrawRule struct { AreaId int64 `json:"area_id" description:"地区id"` AreaStatus string `json:"area_status" description:"多地区[开启,关闭]"` UseTimes int64 `json:"use_times" description:"使用次数"` - LotteryDrawStatus string `json:"lottery_draw_status" description:"抽奖活动状态 [未开始/进行中/已结束]"` // 废弃字段 PrizeNum int `json:"prize_num" xorm:"-" description:"奖品总数"` IsDelete bool `json:"is_delete" xorm:"default(0)" description:"是否删除"` CreatedAt time.Time `json:"-" xorm:"created" description:"创建时间"` @@ -34,7 +32,7 @@ func GetLDRuleIdsByLDActivityId(ids []int64) ([]int64, error) { return ruleIds, err } -func UpdateLDRuleStatusByLDActiviytIds(ids []int64) (int64, error) { - return core.GetXormAuto().Where("is_delete=0").In("lottery_draw_activity_id", ids). - Update(&LotteryDrawRule{LotteryDrawStatus: define.StatusNotBegin}) -} +//func UpdateLDRuleStatusByLDActiviytIds(ids []int64) (int64, error) { +// return core.GetXormAuto().Where("is_delete=0").In("lottery_draw_activity_id", ids). +// Update(&LotteryDrawRule{LotteryDrawStatus: define.StatusNotBegin}) +//} diff --git a/models/lottery_draw_rule_ladder.go b/models/lottery_draw_rule_ladder.go index a13fbd9..a18a1bc 100644 --- a/models/lottery_draw_rule_ladder.go +++ b/models/lottery_draw_rule_ladder.go @@ -33,6 +33,13 @@ func (t *LotteryDrawRuleLadder) Alias(name string) string { } func UpdateLDLadderStatusByLDRuleIds(ids []int64) (int64, error) { - return core.GetXormAuto().Where("is_delete=0").In("lottery_draw_rule_id", ids). + return core.GetXormAuto().Where("is_delete=0"). + In("lottery_draw_rule_id", ids). Update(&LotteryDrawRuleLadder{Status: define.StatusNotBegin}) } + +func GetLotteryDrawLadderByRuleId(ruleId interface{}) ([]*LotteryDrawRuleLadder, error) { + list := make([]*LotteryDrawRuleLadder, 0) + err := core.GetXormAuto().Where("is_delete=0 and lottery_draw_rule_id=?", ruleId).Find(&list) + return list, err +} diff --git a/models/order_draw_rule.go b/models/order_draw_rule.go index 41982ed..d6efc4d 100644 --- a/models/order_draw_rule.go +++ b/models/order_draw_rule.go @@ -2,7 +2,6 @@ package models import ( "github.com/ouxuanserver/osmanthuswine/src/core" - "hudongzhuanjia/utils/define" "time" ) @@ -16,7 +15,6 @@ type OrderDrawRule struct { AreaId int64 `json:"area_id" description:"地区id"` UseTimes int `json:"use_times" description:"使用次数"` Name string `json:"name"` - OrderDrawStatus string `json:"order_draw_status"` PrizeNum int `json:"prize_num" xorm:"-"` HaveEditRule bool `json:"have_edit_rule"` IsDelete bool `json:"is_delete" xorm:"default(0)" description:"是否删除"` @@ -38,8 +36,3 @@ func GetODRuleIdsByODActivityIds(ids []int64) ([]int64, error) { Where("is_delete=0").In("order_draw_activity_id", ids).Find(&ruleIds) return ruleIds, err } - -func UpdateODRuleStatusByODActivityIds(ids []int64) (int64, error) { - return core.GetXormAuto().Where("is_delete=0").In("order_draw_activity_id", ids). - Update(&OrderDrawRule{OrderDrawStatus: define.StatusNotBegin}) -} diff --git a/models/reward_history.go b/models/reward_history.go index ae80eb8..7ea15cf 100644 --- a/models/reward_history.go +++ b/models/reward_history.go @@ -25,6 +25,7 @@ type RewardHistory struct { Amount float64 `json:"amount" xorm:"not null default(0.00) comment('金额') DECIMAL(18)"` Status int `json:"status" xorm:"not null default(0) comment('-1未支付,0未审核,1未通过,2已通过,3已推送,4已作废') INT(11)"` ReviewTime int64 `json:"review_time" xorm:"not null default(0) comment('审核时间') INT(11)"` + Type int `json:"type" xorm:"not null default 0 comment('线下h5/直播 = 0/1') TINYINT(1)"` Version int64 `json:"version" xorm:"not null version comment('乐观锁') INT(11)"` // 额外字段 @@ -59,3 +60,8 @@ func GetRewardHistoryByIds(ids interface{}) ([]*RewardHistory, error) { err := core.GetXormAuto().Where("is_delete=0 and status=0").In("id", ids).Find(&result) return result, err } + +func (t *RewardHistory) SumMoney(serverId, rehearsalId interface{}) (float64, error) { + return core.GetXormAuto().Where("reward_server_id=? and status=3 and is_delete=0 "+ + " and rehearsal_id=?", serverId, rehearsalId).Sum(t, "amount") +} diff --git a/models/sign_history.go b/models/sign_history.go index 9ae6959..1278252 100644 --- a/models/sign_history.go +++ b/models/sign_history.go @@ -35,16 +35,3 @@ func (t *SignHistory) Count(signUpId, activityId, rehearsalId interface{}) (int6 return core.GetXormAuto().Where("is_delete=0 and sign_rule_id=? and rehearsal_id=? and activity_id=?", signUpId, rehearsalId, activityId).Count(t) } - -func GetLotteryUsersResultBySignHistory(obj, areaId, activityId, rehearsalId interface{}, ids []int64) error { - session := core.GetXormAuto().Table(new(SignHistory)).Alias("h"). - Select("h.user_id, u.nickname as username, u.avatar").Distinct("h.user_id"). - Join("LEFT", new(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, rehearsalId) - defer session.Close() - if len(ids) > 0 { - session = session.NotIn("h.user_id", ids) - } - return session.Find(&obj) -} diff --git a/models/user.go b/models/user.go index 9f1059f..9745031 100644 --- a/models/user.go +++ b/models/user.go @@ -44,24 +44,13 @@ func (t *User) GetUserByOpenid(openId string) (bool, error) { return core.GetXormAuto().Where("openid=?", openId).Get(t) } -func (t *User) GetById(id interface{}) (bool, error) { - return core.GetXormAuto().Where("is_delete=0 and id=?", id).Get(t) -} - -func (t *User) SaveAndUpdateWithOpenId() error { - exist, err := core.GetXormAuto().Table(UserTN).Where("openid=?", t.Openid).Exist() - if err != nil { - return err - } - if !exist { - _, err = core.GetXormAuto().Insert(t) - return err - } - _, err = core.GetXormAuto().Where("openid=?", t.Openid).Update(t) - return err -} - func (t *User) SaveUserInfo(uid interface{}) (int64, error) { return core.GetXormAuto().Where("id=?", uid).Cols("nickname", "gender", "avatar", "unionid", "city", "province", "country").Update(t) } + +func GetUsersByIds(ids interface{}) ([]*User, error) { + users := make([]*User, 0) + err := core.GetXormAuto().In("id", ids).Find(&users) + return users, err +} diff --git a/services/calorie/calorie.go b/services/calorie/calorie.go index e1e5d50..a2f927f 100644 --- a/services/calorie/calorie.go +++ b/services/calorie/calorie.go @@ -75,3 +75,25 @@ func GetCurrentCalorie(aid, uid, rid int64) (map[string]interface{}, error) { }, nil } } + +type CalorieCountResult struct { + Id int64 `json:"id"` + ActivityId int64 `json:"activity_id"` + CalorieId int64 `json:"calorie_id"` + UserId int64 `json:"user_id"` + Avatar string `json:"avatar"` + Nickname string `json:"nickname"` + Score int64 `json:"score"` +} + +func RankCalorieUser(calorieId, rehearsalId interface{}, limit int) ([]*CalorieCountResult, error) { + // 统计排名 + result := make([]*CalorieCountResult, 0) + err := core.GetXormAuto().Table(new(models.CalorieUser)).Alias("c"). + Select("c.id, c.activity_id, c.calorie_id, c.user_id, u.avatar, u.nickname, c.score, c.join_time"). + Join("LEFT", new(models.User).Alias("u"), "c.user_id=u.id and u.is_delete=0"). + Where("c.is_delete=0 and c.calorie_id=? and c.rehearsal_id=?", calorieId, rehearsalId). + Limit(limit).Desc("c.score").Asc("c.join_time").Find(&result) + + return result, err +} diff --git a/services/lottery/lottery.go b/services/lottery/lottery.go new file mode 100644 index 0000000..9750a2e --- /dev/null +++ b/services/lottery/lottery.go @@ -0,0 +1,177 @@ +package lottery_service + +import ( + "github.com/ouxuanserver/osmanthuswine/src/core" + "hudongzhuanjia/models" + "hudongzhuanjia/utils/define" + "math/rand" + "time" +) + +type LotteryUser struct { + UserId int64 + Username string + UserPhone string + Avatar string + PrizeName string + LadderId int64 + PrizeImg string +} + +func GetLotteryUsers(ids []int64) ([]*LotteryUser, error) { + users := make([]*LotteryUser, 0) + err := core.GetXormAuto().Select("id as user_id, nickname as username, avatar, phone"). + In("id", ids).Find(&users) + return users, err +} + +func RandLotteryUserIds(users []int64) { + r := rand.New(rand.NewSource(time.Now().Unix())) + if len(users) <= 0 { + return + } + for i := len(users) - 1; i > 0; i-- { + num := r.Intn(i + 1) + users[i], users[num] = users[num], users[i] + } +} + +func GetLotteryUserIds(repeat string, activityId, ruleId, ladderId, rehearsalId, areaId interface{}) ([]int64, error) { + var err error + userIds := make([]int64, 0) + recordIds := make([]int64, 0) + if repeat == define.MODULE_BESIDE_REPEAT { + // 去除同规则中将用户 + 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, rehearsalId, areaId).Find(&recordIds) + } else { + // 去除同规则中将用户 + 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, rehearsalId, areaId).Find(&recordIds) + } + if err != nil { + return nil, err + } + err = core.GetXormAuto().Table(new(models.SignHistory)).Select("user_id"). + Where("is_delete=0 and rehearsal_id=? and area_id=? and activity_id=?", + activityId, rehearsalId, areaId).NotIn("user_id", recordIds).Find(&userIds) + + return userIds, nil +} + +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 GetLotteryAndLadder(activityId, rehearsalId interface{}) ([]*LotteryListResult, error) { + 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) + if err != nil { + return nil, err + } + + 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) + if err != nil { + return nil, 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=?", rehearsalId). + In("r.lottery_draw_rule_ladder_id", ladderIds).GroupBy("ladder_id").Find(&records) + if err != nil { + return nil, 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]) + } + } + } + + return result, nil +} + +type LotteryUsersResult struct { + UserId int64 `json:"user_id"` + Username string `json:"username"` + Avatar string `json:"avatar"` +} + +func GetLotteryUsersResult(areaId, activityId, rehearsalId interface{}, ids []int64) ([]*LotteryUsersResult, error) { + 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, rehearsalId) + if len(ids) >= 0 { + session = session.NotIn("h.user_id", ids) + } + err := session.Find(&result) + if err != nil { + return nil, err + } + return result, nil +} + +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 GetWinnersResult(ruleId, rehearsalId interface{}) ([]*WinnersResult, error) { + 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) + return result, err +} diff --git a/services/lottery/order.go b/services/lottery/order.go index 7a22883..227a298 100644 --- a/services/lottery/order.go +++ b/services/lottery/order.go @@ -1 +1,38 @@ package lottery_service + +import ( + "github.com/ouxuanserver/osmanthuswine/src/core" + "hudongzhuanjia/models" + "hudongzhuanjia/utils/define" +) + +func GetOrderLotteryUserIds(repeat string, activityId, ruleId, ladderId, rehearsalId, areaId interface{}) ([]int64, error) { + var err error + var userIds = make([]int64, 0) + var recordIds = make([]int64, 0) + if repeat == define.MODULE_BESIDE_REPEAT { + //查询已经中奖的用户,剔除已经中奖的用户 + err = core.GetXormAuto().Table(new(models.OrderDrawRecord)).Select("user_id"). + Where("order_draw_rule_id=? and rehearsal_id=? and is_delete=0", + ruleId, rehearsalId).Find(&recordIds) + } else { + // 不去除 + err = core.GetXormAuto().Table(new(models.OrderDrawRecord)).Select("user_id"). + Where("order_draw_rule_ladder_id=? and rehearsal_id=?", + ladderId, rehearsalId).Find(&recordIds) + } + if err != nil { + return nil, err + } + + err = core.GetXormAuto().Table(new(models.CustomerOrder)). + Select("buyer_id as user_id").Distinct("buyer_id"). + Where("activity_id=? and rehearsal_id=? and area_id=? and is_delete=0", + activityId, rehearsalId, areaId).NotIn("buyer_id", recordIds). + Find(&userIds) + if err != nil { + return nil, err + } + return userIds, nil + +} diff --git a/services/lottery/rand_lottery.go b/services/lottery/rand_lottery.go deleted file mode 100644 index 52570b8..0000000 --- a/services/lottery/rand_lottery.go +++ /dev/null @@ -1,27 +0,0 @@ -package lottery_service - -import ( - "math/rand" - "time" -) - -type LotteryUser struct { - UserId int64 - Username string - UserPhone string - Avatar string - PrizeName string - LadderId int64 - PrizeImg string -} - -func RandLotteryUser(users []*LotteryUser) { - r := rand.New(rand.Source(rand.NewSource(time.Now().Unix()))) - if len(users) <= 0 { - return - } - for i := len(users) - 1; i > 0; i-- { - num := r.Intn(i + 1) - users[i], users[num] = users[num], users[i] - } -}