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

170 lines
4.9 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
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 = 1 // 分钱
  45. MaxRedPackAmount = 20000 // 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 int64, name string, 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.Name = name
  102. record.RehearsalId = rid
  103. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  104. record.ShakeRedEnvelopeRuleId = rule.Id
  105. record.Amount = utils.Float64CusDecimal(float64(money)/float64(100), 2)
  106. record.IsDraw = -1
  107. record.IsDelete = false
  108. record.CreatedAt = time.Now()
  109. record.UpdatedAt = time.Now()
  110. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  111. return err
  112. }
  113. sumMoney += money
  114. }
  115. } else if rule.Model == define.SHAKERB_RULE_COMMON { // 普通红包
  116. for i := 0; i < rule.RedEnvelopeNum; i++ {
  117. record := new(models.ShakeRedEnvelopeRecord)
  118. record.ActivityId = aid
  119. record.Name = name
  120. record.RehearsalId = rid
  121. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  122. record.ShakeRedEnvelopeRuleId = rule.Id
  123. record.Amount = rule.Single
  124. record.IsDraw = -1
  125. record.IsDelete = false
  126. record.CreatedAt = time.Now()
  127. record.UpdatedAt = time.Now()
  128. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  129. return err
  130. }
  131. }
  132. } else if rule.Model == define.SHAKERB_RULE_LADDER { // 阶梯红包
  133. ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
  134. err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=?", rule.Id).
  135. Desc("created_at").Find(&ladders)
  136. if err != nil {
  137. return err
  138. }
  139. for _, ladder := range ladders {
  140. for i := 0; i < int(ladder.RedEnvelopeNum); i++ {
  141. record := new(models.ShakeRedEnvelopeRecord)
  142. record.ActivityId = aid
  143. record.Name = name
  144. record.RehearsalId = rid
  145. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  146. record.ShakeRedEnvelopeRuleId = rule.Id
  147. record.Amount = ladder.Single
  148. record.IsDraw = -1
  149. record.CreatedAt = time.Now()
  150. record.UpdatedAt = time.Now()
  151. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  152. return err
  153. }
  154. }
  155. }
  156. }
  157. return nil
  158. }