Browse Source

change gen red pack

master
黄梓健 5 years ago
parent
commit
dc0450cebd
  1. 13
      controllers/pc/shake_red_envelope.go
  2. 2
      models/shake_red_envelope_rule.go
  3. 59
      services/red_envelope/red_envelop.go

13
controllers/pc/shake_red_envelope.go

@ -49,10 +49,6 @@ func (t *ShakeRedEnvelopeCtl) Ready() {
_, err = rule.UpdateToStatusBySids(sids, define.StatusReady, define.StatusNotBegin)
t.CheckErr(err)
// 初始化红包
err = red_envelope_service.GenRedEnvelope(activity.Id, activity.RehearsalId, rule)
t.CheckErr(err)
_, err = rule.UpdateStatus(ruleId, define.StatusReady)
t.CheckErr(err)
t.SUCCESS("success")
@ -74,6 +70,12 @@ func (t *ShakeRedEnvelopeCtl) Start() {
t.CheckErr(err)
t.Assert(exist, code.MSG_SHAKERB_NOT_EXIST, "摇红包不存在")
activity := new(models.Activity)
exist, err = models.GetById(activity, envelope.ActivityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
t.CheckRunning(activity.Status)
if rule.ShakeRedEnvelopeStatus != define.StatusReady {
t.ERROR(fmt.Sprintf("该活动%s", rule.ShakeRedEnvelopeStatus), code.MSG_ERR)
return
@ -82,6 +84,9 @@ func (t *ShakeRedEnvelopeCtl) Start() {
_, err = rule.UpdateStatus(ruleId, define.StatusRunning)
t.CheckErr(err)
err = red_envelope_service.GenRedEnvelope(activity.Id, activity.RehearsalId, rule)
t.CheckErr(err)
go im_service.SendGroupCustomMessage("admin", envelope.ActivityId, im.NoticeShakeRedPackStart,
map[string]interface{}{
"customer_id": uid,

2
models/shake_red_envelope_rule.go

@ -18,7 +18,7 @@ type ShakeRedEnvelopeRule struct {
ShakeRedEnvelopeActivityId int64 `json:"shake_red_envelope_activity_id" xorm:"not null comment('主活动id')" description:"主活动的id"`
AreaId int64 `json:"area_id" xorm:"not null comment('地区id')" description:"地区id"`
Model string `json:"model" description:"[普通红包,随机红包,阶梯红包]"`
RedEnvelopeNum int64 `json:"red_envelope_num" xorm:"not null comment('红包个数')" description:"红包个数"`
RedEnvelopeNum int `json:"red_envelope_num" xorm:"not null comment('红包个数')" description:"红包个数"`
RandSum float64 `json:"rand_sum" xorm:"decimal not null comment('红包总金额')"`
Single float64 `json:"single" description:"单个金额" xorm:"not null comment('单个金额')"`
Probability float64 `json:"probability" description:"概率[0-100)" xorm:"not null comment('概率')"`

59
services/red_envelope/red_envelop.go

@ -54,7 +54,7 @@ func GenRedPack(amount, num int) []int {
sumMoney := 0
redPacks := make([]int, 0)
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 {
money = MinRedPackAmount
} 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()))
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
rate := (rule.RandSum - sumMoney - amount) / float64(int(rule.RedEnvelopeNum)-i)
rate := (randSum - sumMoney - money) / (rule.RedEnvelopeNum - i)
if rate <= MinRedPackAmount {
amount = MinRedPackAmount
money = MinRedPackAmount
} 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.ActivityId = aid
record.RehearsalId = rid
record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
record.ShakeRedEnvelopeRuleId = rule.Id
record.Amount = amount
record.Amount = utils.Float64CusDecimal(float64(money)/float64(100), 2)
record.IsDraw = -1
record.IsDelete = false
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 {
return err
}
sumMoney += record.Amount
sumMoney += money
}
} 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.ActivityId = aid
record.RehearsalId = rid

Loading…
Cancel
Save