互动
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.

167 lines
4.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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 = 30 // 分钱
  45. MaxRedPackAmount = 49900 // 4990
  46. )
  47. func GenRedPack(amount, num int) []int {
  48. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  49. sumMoney := 0
  50. redPacks := make([]int, 0)
  51. for i := 0; i < num; i++ {
  52. money := r.Int() * 2 * (amount - sumMoney) / (num - i)
  53. if money <= MinRedPackAmount {
  54. money = MinRedPackAmount
  55. } else if money >= MaxRedPackAmount {
  56. money = MaxRedPackAmount
  57. }
  58. // 剩余红包的限制
  59. rate := (amount - sumMoney - money) / (num - i)
  60. if rate <= MinRedPackAmount {
  61. money = MinRedPackAmount
  62. } else if rate >= MaxRedPackAmount {
  63. money = MaxRedPackAmount
  64. }
  65. if num-i == 1 { // 最后一个
  66. money = amount - sumMoney
  67. }
  68. sumMoney += money
  69. redPacks = append(redPacks, money)
  70. }
  71. return redPacks
  72. }
  73. // 提前生成红包
  74. func GenRedEnvelope(aid, rid int64, rule *models.ShakeRedEnvelopeRule) error {
  75. // 判断红包是否存在
  76. // 根据算法提前生成红包
  77. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  78. if rule.Model == define.SHAKERB_RULE_RANDOM { // 随机红包
  79. // 检测红包是否存在
  80. for i := 0; i < rule.RedEnvelopeNum; i++ {
  81. randSum := int(rule.RandSum * 100)
  82. sumMoney := 0
  83. money := r.Int() * 2 * (randSum - sumMoney) / (rule.RedEnvelopeNum - i)
  84. if money <= MinRedPackAmount { // 随机的金额可能小于1块钱
  85. money = MinRedPackAmount
  86. } else if money >= MaxRedPackAmount {
  87. money = MaxRedPackAmount
  88. }
  89. // 提前预判剩余红包和红包的金额比例 ==> money/num <= 0.01
  90. rate := (randSum - sumMoney - money) / (rule.RedEnvelopeNum - i)
  91. if rate <= MinRedPackAmount {
  92. money = MinRedPackAmount
  93. } else if rate >= MaxRedPackAmount {
  94. money = MaxRedPackAmount
  95. }
  96. if rule.RedEnvelopeNum-i == 1 {
  97. money = randSum - sumMoney
  98. }
  99. record := new(models.ShakeRedEnvelopeRecord)
  100. record.ActivityId = aid
  101. record.RehearsalId = rid
  102. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  103. record.ShakeRedEnvelopeRuleId = rule.Id
  104. record.Amount = utils.Float64CusDecimal(float64(money)/float64(100), 2)
  105. record.IsDraw = -1
  106. record.IsDelete = false
  107. record.CreatedAt = time.Now()
  108. record.UpdatedAt = time.Now()
  109. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  110. return err
  111. }
  112. sumMoney += money
  113. }
  114. } else if rule.Model == define.SHAKERB_RULE_COMMON { // 普通红包
  115. for i := 0; i < rule.RedEnvelopeNum; i++ {
  116. record := new(models.ShakeRedEnvelopeRecord)
  117. record.ActivityId = aid
  118. record.RehearsalId = rid
  119. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  120. record.ShakeRedEnvelopeRuleId = rule.Id
  121. record.Amount = rule.Single
  122. record.IsDraw = -1
  123. record.IsDelete = false
  124. record.CreatedAt = time.Now()
  125. record.UpdatedAt = time.Now()
  126. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  127. return err
  128. }
  129. }
  130. } else if rule.Model == define.SHAKERB_RULE_LADDER { // 阶梯红包
  131. ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
  132. err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=?", rule.Id).
  133. Desc("created_at").Find(&ladders)
  134. if err != nil {
  135. return err
  136. }
  137. for _, ladder := range ladders {
  138. for i := 0; i < int(ladder.RedEnvelopeNum); i++ {
  139. record := new(models.ShakeRedEnvelopeRecord)
  140. record.ActivityId = aid
  141. record.RehearsalId = rid
  142. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  143. record.ShakeRedEnvelopeRuleId = rule.Id
  144. record.Amount = ladder.Single
  145. record.IsDraw = -1
  146. record.CreatedAt = time.Now()
  147. record.UpdatedAt = time.Now()
  148. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  149. return err
  150. }
  151. }
  152. }
  153. }
  154. return nil
  155. }