Browse Source

fix:bug

master
黄梓健 5 years ago
parent
commit
d35d309a72
  1. 43
      services/red_envelope/red_envelop.go

43
services/red_envelope/red_envelop.go

@ -50,14 +50,19 @@ const (
)
func GenRedPack(amount, num int) []int {
r := rand.New(rand.NewSource(time.Now().Unix()))
sumMoney := 0
redPacks := make([]int, 0)
maxMoney := MaxRedPackAmount
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < num; i++ {
money := r.Int() * 2 * (amount - sumMoney) / (num - i)
if money <= MinRedPackAmount {
maxMoney = (amount - sumMoney) / (num - i)
if maxMoney > MaxRedPackAmount {
maxMoney = MaxRedPackAmount
}
money := r.Int()
if money < MinRedPackAmount {
money = MinRedPackAmount
} else if money >= MaxRedPackAmount {
} else if money > MaxRedPackAmount {
money = MaxRedPackAmount
}
@ -87,16 +92,24 @@ func GenRedEnvelope(aid int64, name string, rid int64, rule *models.ShakeRedEnve
// 检测红包是否存在
randSum := int(rule.RandSum * 100)
sumMoney := 0
if randSum/rule.RedEnvelopeNum > MinRedPackAmount && randSum/rule.RedEnvelopeNum < MaxRedPackAmount {
maxMoney := MaxRedPackAmount
if randSum/rule.RedEnvelopeNum <= MinRedPackAmount && randSum/rule.RedEnvelopeNum >= MaxRedPackAmount {
return errors.New("每个红包的平均金额必须在1.00元到200.00元之间")
}
for i := 0; i < rule.RedEnvelopeNum; i++ {
money := r.Int() * 2 * (randSum - sumMoney) / (rule.RedEnvelopeNum - i)
if money <= MinRedPackAmount { // 随机的金额可能小于1块钱
// 前置判断每次循环是否存在最小值
//最大可领金额 = 剩余金额的平均值x2 = (剩余金额 / 剩余数量) * 2
maxMoney = (randSum - sumMoney) / (rule.RedEnvelopeNum - i)
if maxMoney > MaxRedPackAmount {
maxMoney = MaxRedPackAmount
}
money := r.Intn(maxMoney)
if money < MinRedPackAmount { // 随机的金额可能小于1块钱
money = MinRedPackAmount
} else if money >= MaxRedPackAmount {
} else if money > MaxRedPackAmount {
money = MaxRedPackAmount
}
// 提前预判剩余红包和红包的金额比例 ==> money/num <= 0.01
rate := (randSum - sumMoney - money) / (rule.RedEnvelopeNum - i)
if rate <= MinRedPackAmount {
@ -116,11 +129,8 @@ func GenRedEnvelope(aid int64, name string, rid int64, rule *models.ShakeRedEnve
record.ShakeRedEnvelopeRuleId = rule.Id
record.Amount = utils.Float64CusDecimal(float64(money)/float64(100), 2)
record.IsDraw = -1
record.IsDelete = false
record.CreatedAt = time.Now()
record.UpdatedAt = time.Now()
if _, err := core.GetXormAuto().InsertOne(record); err != nil {
if _, err := models.Add(record); err != nil {
return err
}
sumMoney += money
@ -135,10 +145,7 @@ func GenRedEnvelope(aid int64, name string, rid int64, rule *models.ShakeRedEnve
record.ShakeRedEnvelopeRuleId = rule.Id
record.Amount = rule.Single
record.IsDraw = -1
record.IsDelete = false
record.CreatedAt = time.Now()
record.UpdatedAt = time.Now()
if _, err := core.GetXormAuto().InsertOne(record); err != nil {
if _, err := models.Add(record); err != nil {
return err
}
}
@ -159,9 +166,7 @@ func GenRedEnvelope(aid int64, name string, rid int64, rule *models.ShakeRedEnve
record.ShakeRedEnvelopeRuleId = rule.Id
record.Amount = ladder.Single
record.IsDraw = -1
record.CreatedAt = time.Now()
record.UpdatedAt = time.Now()
if _, err := core.GetXormAuto().InsertOne(record); err != nil {
if _, err := models.Add(record); err != nil {
return err
}
}

Loading…
Cancel
Save