黄梓健
5 years ago
26 changed files with 381 additions and 447 deletions
-
12controllers/client/lottery.go
-
6controllers/client/reward.go
-
2controllers/client/sign.go
-
64controllers/pc/activity.go
-
8controllers/pc/area_store.go
-
22controllers/pc/auction.go
-
6controllers/pc/barrage.go
-
10controllers/pc/bully_screen.go
-
44controllers/pc/calorie.go
-
204controllers/pc/lottery_draw.go
-
65controllers/pc/order_draw.go
-
26controllers/pc/reward.go
-
5models/CalorieUser.go
-
6models/calorie.go
-
3models/customer_order_option.go
-
14models/lottery_draw_record.go
-
10models/lottery_draw_rule.go
-
9models/lottery_draw_rule_ladder.go
-
7models/order_draw_rule.go
-
6models/reward_history.go
-
13models/sign_history.go
-
23models/user.go
-
22services/calorie/calorie.go
-
177services/lottery/lottery.go
-
37services/lottery/order.go
-
27services/lottery/rand_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 |
||||
|
} |
@ -1 +1,38 @@ |
|||||
package lottery_service |
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 |
||||
|
|
||||
|
} |
@ -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] |
|
||||
} |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue