|
@ -54,7 +54,7 @@ func GenRedPack(amount, num int) []int { |
|
|
sumMoney := 0 |
|
|
sumMoney := 0 |
|
|
redPacks := make([]int, 0) |
|
|
redPacks := make([]int, 0) |
|
|
for i := 0; i < num; i++ { |
|
|
for i := 0; i < num; i++ { |
|
|
money := r.Int()*2*(amount-sumMoney)/num - i |
|
|
|
|
|
|
|
|
money := r.Int() * 2 * (amount - sumMoney) / (num - i) |
|
|
if money <= MinRedPackAmount { |
|
|
if money <= MinRedPackAmount { |
|
|
money = MinRedPackAmount |
|
|
money = MinRedPackAmount |
|
|
} else if money >= MaxRedPackAmount { |
|
|
} else if money >= MaxRedPackAmount { |
|
@ -83,61 +83,36 @@ func GenRedEnvelope(aid, rid int64, rule *models.ShakeRedEnvelopeRule) error { |
|
|
// 判断红包是否存在
|
|
|
// 判断红包是否存在
|
|
|
// 根据算法提前生成红包
|
|
|
// 根据算法提前生成红包
|
|
|
|
|
|
|
|
|
// 删除掉之前存在对的
|
|
|
|
|
|
_, err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=? "+ |
|
|
|
|
|
" and shake_red_envelope_activity_id=? and activity_id=? and rehearsal_id=?", |
|
|
|
|
|
rule.Id, rule.ShakeRedEnvelopeActivityId, aid, rid).Cols("is_delete"). |
|
|
|
|
|
Update(&models.ShakeRedEnvelopeRecord{IsDelete: true}) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano())) |
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano())) |
|
|
if rule.Model == define.SHAKERB_RULE_RANDOM { // 随机红包
|
|
|
if rule.Model == define.SHAKERB_RULE_RANDOM { // 随机红包
|
|
|
//redPacks := GenRedPack(int(rule.RandSum*100), int(rule.RedEnvelopeNum))
|
|
|
|
|
|
//for _, redpack := range redPacks {
|
|
|
|
|
|
// record := new(models.ShakeRedEnvelopeRecord)
|
|
|
|
|
|
// record.ActivityId = aid
|
|
|
|
|
|
// record.RehearsalId = rid
|
|
|
|
|
|
// record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
|
|
|
|
|
|
// record.ShakeRedEnvelopeRuleId = rule.Id
|
|
|
|
|
|
// record.Amount = float64(redpack) / 100
|
|
|
|
|
|
// record.IsDraw = -1
|
|
|
|
|
|
// record.IsDelete = false
|
|
|
|
|
|
// record.CreatedAt = time.Now()
|
|
|
|
|
|
// record.UpdatedAt = time.Now()
|
|
|
|
|
|
// if _, err := core.GetXormAuto().InsertOne(record); err != nil {
|
|
|
|
|
|
// return err
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
// 检测红包是否存在
|
|
|
// 检测红包是否存在
|
|
|
sumMoney := 0.0 |
|
|
|
|
|
for i := 0; i < int(rule.RedEnvelopeNum); i++ { |
|
|
|
|
|
money := r.Float64() * 2 * (rule.RandSum - sumMoney) / float64(int(rule.RedEnvelopeNum)-i) |
|
|
|
|
|
amount := utils.Float64CusDecimal(money, 2) |
|
|
|
|
|
if amount <= MinRedPackAmount { // 随机的金额可能小于1块钱
|
|
|
|
|
|
amount = MinRedPackAmount |
|
|
|
|
|
} else if amount >= MaxRedPackAmount { |
|
|
|
|
|
amount = MaxRedPackAmount |
|
|
|
|
|
|
|
|
for i := 0; i < rule.RedEnvelopeNum; i++ { |
|
|
|
|
|
randSum := int(rule.RandSum * 100) |
|
|
|
|
|
sumMoney := 0 |
|
|
|
|
|
money := r.Int() * 2 * (randSum - sumMoney) / (rule.RedEnvelopeNum - i) |
|
|
|
|
|
if money <= MinRedPackAmount { // 随机的金额可能小于1块钱
|
|
|
|
|
|
money = MinRedPackAmount |
|
|
|
|
|
} else if money >= MaxRedPackAmount { |
|
|
|
|
|
money = MaxRedPackAmount |
|
|
} |
|
|
} |
|
|
// 提前预判剩余红包和红包的金额比例 ==> money/num <= 0.01
|
|
|
// 提前预判剩余红包和红包的金额比例 ==> money/num <= 0.01
|
|
|
rate := (rule.RandSum - sumMoney - amount) / float64(int(rule.RedEnvelopeNum)-i) |
|
|
|
|
|
|
|
|
rate := (randSum - sumMoney - money) / (rule.RedEnvelopeNum - i) |
|
|
if rate <= MinRedPackAmount { |
|
|
if rate <= MinRedPackAmount { |
|
|
amount = MinRedPackAmount |
|
|
|
|
|
|
|
|
money = MinRedPackAmount |
|
|
} else if rate >= MaxRedPackAmount { |
|
|
} else if rate >= MaxRedPackAmount { |
|
|
amount = MaxRedPackAmount |
|
|
|
|
|
|
|
|
money = MaxRedPackAmount |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if int(rule.RedEnvelopeNum)-i == 1 { |
|
|
|
|
|
amount = rule.RandSum - sumMoney |
|
|
|
|
|
|
|
|
if rule.RedEnvelopeNum-i == 1 { |
|
|
|
|
|
money = randSum - sumMoney |
|
|
} |
|
|
} |
|
|
record := new(models.ShakeRedEnvelopeRecord) |
|
|
record := new(models.ShakeRedEnvelopeRecord) |
|
|
record.ActivityId = aid |
|
|
record.ActivityId = aid |
|
|
record.RehearsalId = rid |
|
|
record.RehearsalId = rid |
|
|
record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId |
|
|
record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId |
|
|
record.ShakeRedEnvelopeRuleId = rule.Id |
|
|
record.ShakeRedEnvelopeRuleId = rule.Id |
|
|
record.Amount = amount |
|
|
|
|
|
|
|
|
record.Amount = utils.Float64CusDecimal(float64(money)/float64(100), 2) |
|
|
record.IsDraw = -1 |
|
|
record.IsDraw = -1 |
|
|
record.IsDelete = false |
|
|
record.IsDelete = false |
|
|
record.CreatedAt = time.Now() |
|
|
record.CreatedAt = time.Now() |
|
@ -146,10 +121,10 @@ func GenRedEnvelope(aid, rid int64, rule *models.ShakeRedEnvelopeRule) error { |
|
|
if _, err := core.GetXormAuto().InsertOne(record); err != nil { |
|
|
if _, err := core.GetXormAuto().InsertOne(record); err != nil { |
|
|
return err |
|
|
return err |
|
|
} |
|
|
} |
|
|
sumMoney += record.Amount |
|
|
|
|
|
|
|
|
sumMoney += money |
|
|
} |
|
|
} |
|
|
} else if rule.Model == define.SHAKERB_RULE_COMMON { // 普通红包
|
|
|
} else if rule.Model == define.SHAKERB_RULE_COMMON { // 普通红包
|
|
|
for i := 0; i < int(rule.RedEnvelopeNum); i++ { |
|
|
|
|
|
|
|
|
for i := 0; i < rule.RedEnvelopeNum; i++ { |
|
|
record := new(models.ShakeRedEnvelopeRecord) |
|
|
record := new(models.ShakeRedEnvelopeRecord) |
|
|
record.ActivityId = aid |
|
|
record.ActivityId = aid |
|
|
record.RehearsalId = rid |
|
|
record.RehearsalId = rid |
|
|