互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.5 KiB

5 years ago
  1. package red_envelope_service
  2. import (
  3. "errors"
  4. "github.com/ouxuanserver/osmanthuswine/src/core"
  5. "hudongzhuanjia/models"
  6. "hudongzhuanjia/utils"
  7. "hudongzhuanjia/utils/define"
  8. "math/rand"
  9. "time"
  10. )
  11. func GetCurrentRB(aid, uid, rid int64) (map[string]interface{}, error) {
  12. // 信息 一轮只能有一个
  13. rule := new(models.ShakeRedEnvelopeRule)
  14. exist, err := rule.GetCurrent(aid)
  15. if err != nil {
  16. return nil, err
  17. }
  18. if exist {
  19. shakeUser := new(models.ShakeRedEnvelopeUser)
  20. shakeUser.UserId = uid
  21. shakeUser.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  22. shakeUser.ShakeRedEnvelopeRuleId = rule.Id
  23. shakeUser.RehearsalId = rid
  24. shakeUser.CreatedAt = time.Now()
  25. shakeUser.UpdateAt = time.Now()
  26. shakeUser.IsDelete = 0
  27. err := models.Save(map[string]interface{}{
  28. "user_id=": uid,
  29. "shake_red_envelope_activity_id=": rule.ShakeRedEnvelopeActivityId,
  30. "shake_red_envelope_rule_id=": rule.Id,
  31. "rehearsal_id=": rid,
  32. }, shakeUser)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return map[string]interface{}{
  37. "rule": rule,
  38. }, nil
  39. }
  40. return nil, errors.New("尚未开启轮次")
  41. // 记录一下参与人数
  42. }
  43. const (
  44. MinRedPackAmount = 1
  45. MaxRedPackAmount = 499
  46. )
  47. // 提前生成红包
  48. func GenRedEnvelope(aid, rid int64, rule *models.ShakeRedEnvelopeRule) error {
  49. // 判断红包是否存在
  50. // 根据算法提前生成红包
  51. // 删除掉之前存在对的
  52. _, err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=? "+
  53. " and shake_red_envelope_activity_id=? and activity_id=? and rehearsal_id=?",
  54. rule.Id, rule.ShakeRedEnvelopeActivityId, aid, rid).Cols("is_delete").
  55. Update(&models.ShakeRedEnvelopeRecord{IsDelete: true})
  56. if err != nil {
  57. return err
  58. }
  59. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  60. if rule.Model == define.SHAKERB_RULE_RANDOM { // 随机红包
  61. // 检测红包是否存在
  62. sumMoney := 0.0
  63. for i := 0; i < int(rule.RedEnvelopeNum); i++ {
  64. money := r.Float64() * 2 * (rule.RandSum - sumMoney) / float64(int(rule.RedEnvelopeNum)-i)
  65. amount := utils.Float64CusDecimal(money, 2)
  66. if amount < MinRedPackAmount { // 随机的金额可能小于1块钱
  67. amount = MinRedPackAmount
  68. } else if amount >= MaxRedPackAmount {
  69. amount = MaxRedPackAmount
  70. }
  71. // 提前预判剩余红包和红包的金额比例 ==> money/num <= 0.01
  72. rate := (rule.RandSum - sumMoney - amount) / float64(int(rule.RedEnvelopeNum)-i)
  73. if rate <= MinRedPackAmount {
  74. amount = MinRedPackAmount
  75. } else if rate >= MaxRedPackAmount {
  76. amount = MaxRedPackAmount
  77. }
  78. if int(rule.RedEnvelopeNum)-i == 1 {
  79. amount = rule.RandSum - sumMoney
  80. }
  81. record := new(models.ShakeRedEnvelopeRecord)
  82. record.ActivityId = aid
  83. record.RehearsalId = rid
  84. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  85. record.ShakeRedEnvelopeRuleId = rule.Id
  86. record.Amount = amount
  87. record.IsDraw = -1
  88. record.IsDelete = false
  89. record.CreatedAt = time.Now()
  90. record.UpdatedAt = time.Now()
  91. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  92. return err
  93. }
  94. sumMoney += record.Amount
  95. }
  96. } else if rule.Model == define.SHAKERB_RULE_COMMON { // 普通红包
  97. for i := 0; i < int(rule.RedEnvelopeNum); i++ {
  98. record := new(models.ShakeRedEnvelopeRecord)
  99. record.ActivityId = aid
  100. record.RehearsalId = rid
  101. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  102. record.ShakeRedEnvelopeRuleId = rule.Id
  103. record.Amount = rule.Single
  104. record.IsDraw = -1
  105. record.IsDelete = false
  106. record.CreatedAt = time.Now()
  107. record.UpdatedAt = time.Now()
  108. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  109. return err
  110. }
  111. }
  112. } else if rule.Model == define.SHAKERB_RULE_LADDER { // 阶梯红包
  113. ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
  114. err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=?", rule.Id).
  115. Desc("created_at").Find(&ladders)
  116. if err != nil {
  117. return err
  118. }
  119. for _, ladder := range ladders {
  120. for i := 0; i < int(ladder.RedEnvelopeNum); i++ {
  121. record := new(models.ShakeRedEnvelopeRecord)
  122. record.ActivityId = aid
  123. record.RehearsalId = rid
  124. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  125. record.ShakeRedEnvelopeRuleId = rule.Id
  126. record.Amount = ladder.Single
  127. record.IsDraw = -1
  128. record.CreatedAt = time.Now()
  129. record.UpdatedAt = time.Now()
  130. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  131. return err
  132. }
  133. }
  134. }
  135. }
  136. return nil
  137. }