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

171 lines
5.1 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
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 = 100 // 1块钱
  45. MaxRedPackAmount = 20000 // 200块钱
  46. )
  47. func GenRedPack(amount, num int) []int {
  48. r := rand.New(rand.NewSource(time.Now().Unix()))
  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().Unix()))
  78. if rule.Model == define.SHAKERB_RULE_RANDOM { // 随机红包
  79. // 检测红包是否存在
  80. randSum := int(rule.RandSum * 100)
  81. sumMoney := 0
  82. if randSum/rule.RedEnvelopeNum > MinRedPackAmount && randSum/rule.RedEnvelopeNum < MaxRedPackAmount {
  83. return errors.New("每个红包的平均金额必须在1.00元到200.00元之间")
  84. }
  85. for i := 0; i < rule.RedEnvelopeNum; i++ {
  86. money := r.Int() * 2 * (randSum - sumMoney) / (rule.RedEnvelopeNum - i)
  87. if money <= MinRedPackAmount { // 随机的金额可能小于1块钱
  88. money = MinRedPackAmount
  89. } else if money >= MaxRedPackAmount {
  90. money = MaxRedPackAmount
  91. }
  92. // 提前预判剩余红包和红包的金额比例 ==> money/num <= 0.01
  93. rate := (randSum - sumMoney - money) / (rule.RedEnvelopeNum - i)
  94. if rate <= MinRedPackAmount {
  95. money = MinRedPackAmount
  96. } else if rate >= MaxRedPackAmount {
  97. money = MaxRedPackAmount
  98. }
  99. if rule.RedEnvelopeNum-i == 1 {
  100. money = randSum - sumMoney
  101. }
  102. record := new(models.ShakeRedEnvelopeRecord)
  103. record.ActivityId = aid
  104. record.Name = name
  105. record.RehearsalId = rid
  106. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  107. record.ShakeRedEnvelopeRuleId = rule.Id
  108. record.Amount = utils.Float64CusDecimal(float64(money)/float64(100), 2)
  109. record.IsDraw = -1
  110. record.IsDelete = false
  111. record.CreatedAt = time.Now()
  112. record.UpdatedAt = time.Now()
  113. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  114. return err
  115. }
  116. sumMoney += money
  117. }
  118. } else if rule.Model == define.SHAKERB_RULE_COMMON { // 普通红包
  119. for i := 0; i < rule.RedEnvelopeNum; i++ {
  120. record := new(models.ShakeRedEnvelopeRecord)
  121. record.ActivityId = aid
  122. record.Name = name
  123. record.RehearsalId = rid
  124. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  125. record.ShakeRedEnvelopeRuleId = rule.Id
  126. record.Amount = rule.Single
  127. record.IsDraw = -1
  128. record.IsDelete = false
  129. record.CreatedAt = time.Now()
  130. record.UpdatedAt = time.Now()
  131. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  132. return err
  133. }
  134. }
  135. } else if rule.Model == define.SHAKERB_RULE_LADDER { // 阶梯红包
  136. ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
  137. err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=?", rule.Id).
  138. Desc("created_at").Find(&ladders)
  139. if err != nil {
  140. return err
  141. }
  142. for _, ladder := range ladders {
  143. for i := 0; i < int(ladder.RedEnvelopeNum); i++ {
  144. record := new(models.ShakeRedEnvelopeRecord)
  145. record.ActivityId = aid
  146. record.Name = name
  147. record.RehearsalId = rid
  148. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  149. record.ShakeRedEnvelopeRuleId = rule.Id
  150. record.Amount = ladder.Single
  151. record.IsDraw = -1
  152. record.CreatedAt = time.Now()
  153. record.UpdatedAt = time.Now()
  154. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  155. return err
  156. }
  157. }
  158. }
  159. }
  160. return nil
  161. }