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

192 lines
6.5 KiB

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().Table(new(models.User)).
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=?",
rehearsalId, areaId, activityId).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 GetLotteryUsersResultByRollNum(ladderId, rehearsalId interface{}, rollNum int) ([]*LotteryUsersResult, error) {
users := make([]*LotteryUsersResult, 0)
session := core.GetXormAuto().Table(new(models.LotteryDrawRecord)).Alias("r").
Join("LEFT", new(models.User).Alias("u"), "u.id=r.user_id").
Where("r.is_delete=0 and r.lottery_draw_rule_ladder_id=? and "+
"r.rehearsal_id=?", ladderId, rehearsalId)
defer session.Close()
if rollNum > 0 {
session = session.Where("roll_num=?", rollNum)
}
err := session.Find(&users)
return users, err
}
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(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_id=? and record.rehearsal_id=?", ruleId, rehearsalId).
Find(&result)
return result, err
}