From 634b527bf15cfd2a7667947facc04828dd73d9ac Mon Sep 17 00:00:00 2001 From: tommy <3405129587@qq.com> Date: Wed, 17 Jun 2020 10:16:33 +0800 Subject: [PATCH] fix:bug --- controllers/client/live.go | 4 ++-- models/shake_red_envelope_record.go | 2 -- services/red_envelope/red_envelop.go | 14 +++++++------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/controllers/client/live.go b/controllers/client/live.go index 2ad6249..9aca28a 100644 --- a/controllers/client/live.go +++ b/controllers/client/live.go @@ -160,8 +160,8 @@ func (t *LiveCtl) SendLiveRedPack() { amount := utils.Float64CusDecimal(t.MustGetDouble("amount"), 2) // 金额 areaId := t.MustGetInt64("area_id") - if amount/float64(num) < 0.3 { // 平均每个红包不得小于0.3 - t.ERROR("每个红包不得小于0.3元", code.MSG_ERR) + if amount/float64(num) < 1 && amount/float64(num) > 200 { // 平均每个红包不得小于0.3 + t.ERROR("每个红包的平均金额必须在1.00元到200.00元之间", code.MSG_ERR) return } diff --git a/models/shake_red_envelope_record.go b/models/shake_red_envelope_record.go index 254e93e..700ba85 100644 --- a/models/shake_red_envelope_record.go +++ b/models/shake_red_envelope_record.go @@ -27,8 +27,6 @@ type ShakeRedEnvelopeRecord struct { IsDraw int `json:"is_draw" xorm:"not null default 0 comment('-1未被摇中,0已被摇中1被提取')"` MchBillno string `json:"mch_billno" xorm:"not null default '' comment('微信红包订单号') VARCHAR(128)"` Version int `json:"version" xorm:"not null version default 0 comment('乐观锁') INT(11)"` - - // 无关变量 } func (t *ShakeRedEnvelopeRecord) TableName() string { diff --git a/services/red_envelope/red_envelop.go b/services/red_envelope/red_envelop.go index 7b9fed4..fd9bffd 100644 --- a/services/red_envelope/red_envelop.go +++ b/services/red_envelope/red_envelop.go @@ -49,10 +49,7 @@ const ( MaxRedPackAmount = 20000 // 200块钱 ) -func GenRedPack(amount, num int) ([]int, error) { - if amount/num <= 100 { - return nil, errors.New("每个红包的平均金额必须在1.00元到200.00元之间") - } +func GenRedPack(amount, num int) []int { r := rand.New(rand.NewSource(time.Now().UnixNano())) sumMoney := 0 redPacks := make([]int, 0) @@ -78,7 +75,7 @@ func GenRedPack(amount, num int) ([]int, error) { sumMoney += money redPacks = append(redPacks, money) } - return redPacks, nil + return redPacks } // 提前生成红包 @@ -88,9 +85,12 @@ func GenRedEnvelope(aid int64, name string, rid int64, rule *models.ShakeRedEnve r := rand.New(rand.NewSource(time.Now().UnixNano())) if rule.Model == define.SHAKERB_RULE_RANDOM { // 随机红包 // 检测红包是否存在 + randSum := int(rule.RandSum * 100) + sumMoney := 0 + if randSum/rule.RedEnvelopeNum > MinRedPackAmount && randSum/rule.RedEnvelopeNum < MaxRedPackAmount { + return errors.New("每个红包的平均金额必须在1.00元到200.00元之间") + } 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