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.
193 lines
6.6 KiB
193 lines
6.6 KiB
package lottery_service
|
|
|
|
import (
|
|
"hudongzhuanjia/models"
|
|
"hudongzhuanjia/utils/define"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/ouxuanserver/osmanthuswine/src/core"
|
|
)
|
|
|
|
type LotteryUser struct {
|
|
UserId int
|
|
Username string
|
|
UserPhone string
|
|
Avatar string
|
|
PrizeName string
|
|
LadderId int
|
|
PrizeImg string
|
|
}
|
|
|
|
func GetLotteryUsers(ids []int) ([]*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 []int) {
|
|
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, archId interface{}) ([]int, error) {
|
|
var err error
|
|
userIds := make([]int, 0)
|
|
recordIds := make([]int, 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 arch_id=? and is_delete=0",
|
|
ruleId, rehearsalId, areaId, archId).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 arch_id=? and is_delete=0",
|
|
// ladderId, rehearsalId, areaId, archId).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=? and arch_id=?",
|
|
rehearsalId, areaId, activityId, archId).NotIn("user_id", recordIds).Find(&userIds)
|
|
|
|
return userIds, nil
|
|
}
|
|
|
|
type LotteryListResult struct {
|
|
LotteryDrawActivityId int `json:"lottery_draw_activity_id"`
|
|
LotteryDrawRuleId int `json:"lottery_draw_rule_id"`
|
|
LotteryDrawActivityName string `json:"lottery_draw_name"`
|
|
LotteryDrawLadders []*LotteryLadderResult `json:"lottery_draw_ladders"`
|
|
PrizeNumber int `json:"prize_number"`
|
|
}
|
|
|
|
type LotteryLadderResult struct {
|
|
LotteryDrawRuleId int `json:"-"`
|
|
LotteryDrawLadderId int `json:"lottery_draw_ladder_id"`
|
|
Status string `json:"status"`
|
|
PrizeName string `json:"prize_name"`
|
|
PrizeImg string `json:"prize_img"`
|
|
PrizeNumber int `json:"prize_number"`
|
|
}
|
|
|
|
func GetLotteryAndLadder(activityId, rehearsalId, archId 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.Alias(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([]int, 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([]int, 0)
|
|
for _, ladder := range ladders {
|
|
ladderIds = append(ladderIds, ladder.LotteryDrawLadderId)
|
|
}
|
|
|
|
records := make([]map[string]int, 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=? and arch_id=?", rehearsalId, archId).
|
|
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 int `json:"user_id"`
|
|
Username string `json:"username"`
|
|
Avatar string `json:"avatar"`
|
|
}
|
|
|
|
func GetLotteryUsersResultByRollNum(ladderId, rehearsalId, archId 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=? and r.arch_id=?",
|
|
ladderId, rehearsalId, archId)
|
|
defer session.Close()
|
|
if rollNum > 0 {
|
|
session = session.Where("roll_num=?", rollNum)
|
|
}
|
|
err := session.Find(&users)
|
|
return users, err
|
|
}
|
|
|
|
func GetLotteryUsersResult(areaId, activityId, rehearsalId, archId interface{}, ids []int) ([]*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=? and h.arch_id=?",
|
|
areaId, activityId, rehearsalId, archId)
|
|
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 int `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, archId interface{}) ([]*WinnersResult, error) {
|
|
result := make([]*WinnersResult, 0)
|
|
err := core.GetXormAuto().Table(&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_id=? and r.rehearsal_id=? and r.arch_id=?",
|
|
ruleId, rehearsalId, archId).Find(&result)
|
|
return result, err
|
|
}
|