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

172 lines
5.0 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
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, archId int) (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. exist, err := shakeUser.Exist(uid, rule.Id, archId, rid)
  21. if err != nil {
  22. return nil, err
  23. }
  24. if !exist {
  25. shakeUser.UserId = uid
  26. shakeUser.ArchId = archId
  27. shakeUser.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  28. shakeUser.ShakeRedEnvelopeRuleId = rule.Id
  29. shakeUser.RehearsalId = rid
  30. shakeUser.CreatedAt = time.Now()
  31. shakeUser.UpdateAt = time.Now()
  32. shakeUser.IsDelete = 0
  33. _, err = models.Add(shakeUser)
  34. if err != nil {
  35. return nil, err
  36. }
  37. }
  38. return map[string]interface{}{
  39. "rule": rule,
  40. }, nil
  41. }
  42. return nil, errors.New("尚未开启轮次")
  43. // 记录一下参与人数
  44. }
  45. const (
  46. MinRedPackAmount = 100 // 1块钱
  47. MaxRedPackAmount = 20000 // 200块钱
  48. )
  49. func GenRedPack(amount, num int) []int {
  50. r := rand.New(rand.NewSource(time.Now().Unix()))
  51. sumMoney := 0
  52. redPacks := make([]int, 0)
  53. for i := 0; i < num; i++ {
  54. money := r.Int() * 2 * (amount - sumMoney) / (num - i)
  55. if money <= MinRedPackAmount {
  56. money = MinRedPackAmount
  57. } else if money >= MaxRedPackAmount {
  58. money = MaxRedPackAmount
  59. }
  60. // 剩余红包的限制
  61. rate := (amount - sumMoney - money) / (num - i)
  62. if rate <= MinRedPackAmount {
  63. money = MinRedPackAmount
  64. } else if rate >= MaxRedPackAmount {
  65. money = MaxRedPackAmount
  66. }
  67. if num-i == 1 { // 最后一个
  68. money = amount - sumMoney
  69. }
  70. sumMoney += money
  71. redPacks = append(redPacks, money)
  72. }
  73. return redPacks
  74. }
  75. // 提前生成红包
  76. func GenRedEnvelope(aid int64, name string, rid int64, rule *models.ShakeRedEnvelopeRule) error {
  77. // 判断红包是否存在
  78. // 根据算法提前生成红包
  79. r := rand.New(rand.NewSource(time.Now().Unix()))
  80. if rule.Model == define.SHAKERB_RULE_RANDOM { // 随机红包
  81. // 检测红包是否存在
  82. randSum := int(rule.RandSum * 100)
  83. sumMoney := 0
  84. if randSum/rule.RedEnvelopeNum > MinRedPackAmount && randSum/rule.RedEnvelopeNum < MaxRedPackAmount {
  85. return errors.New("每个红包的平均金额必须在1.00元到200.00元之间")
  86. }
  87. for i := 0; i < rule.RedEnvelopeNum; i++ {
  88. money := r.Int() * 2 * (randSum - sumMoney) / (rule.RedEnvelopeNum - i)
  89. if money <= MinRedPackAmount { // 随机的金额可能小于1块钱
  90. money = MinRedPackAmount
  91. } else if money >= MaxRedPackAmount {
  92. money = MaxRedPackAmount
  93. }
  94. // 提前预判剩余红包和红包的金额比例 ==> money/num <= 0.01
  95. rate := (randSum - sumMoney - money) / (rule.RedEnvelopeNum - i)
  96. if rate <= MinRedPackAmount {
  97. money = MinRedPackAmount
  98. } else if rate >= MaxRedPackAmount {
  99. money = MaxRedPackAmount
  100. }
  101. if rule.RedEnvelopeNum-i == 1 {
  102. money = randSum - sumMoney
  103. }
  104. record := new(models.ShakeRedEnvelopeRecord)
  105. record.ActivityId = aid
  106. record.Name = name
  107. record.RehearsalId = rid
  108. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  109. record.ShakeRedEnvelopeRuleId = rule.Id
  110. record.Amount = utils.Float64CusDecimal(float64(money)/float64(100), 2)
  111. record.IsDraw = -1
  112. record.IsDelete = false
  113. record.CreatedAt = time.Now()
  114. record.UpdatedAt = time.Now()
  115. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  116. return err
  117. }
  118. sumMoney += money
  119. }
  120. } else if rule.Model == define.SHAKERB_RULE_COMMON { // 普通红包
  121. for i := 0; i < rule.RedEnvelopeNum; i++ {
  122. record := new(models.ShakeRedEnvelopeRecord)
  123. record.ActivityId = aid
  124. record.Name = name
  125. record.RehearsalId = rid
  126. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  127. record.ShakeRedEnvelopeRuleId = rule.Id
  128. record.Amount = rule.Single
  129. record.IsDraw = -1
  130. record.IsDelete = false
  131. record.CreatedAt = time.Now()
  132. record.UpdatedAt = time.Now()
  133. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  134. return err
  135. }
  136. }
  137. } else if rule.Model == define.SHAKERB_RULE_LADDER { // 阶梯红包
  138. ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
  139. err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=?", rule.Id).
  140. Desc("created_at").Find(&ladders)
  141. if err != nil {
  142. return err
  143. }
  144. for _, ladder := range ladders {
  145. for i := 0; i < int(ladder.RedEnvelopeNum); i++ {
  146. record := new(models.ShakeRedEnvelopeRecord)
  147. record.ActivityId = aid
  148. record.Name = name
  149. record.RehearsalId = rid
  150. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  151. record.ShakeRedEnvelopeRuleId = rule.Id
  152. record.Amount = ladder.Single
  153. record.IsDraw = -1
  154. record.CreatedAt = time.Now()
  155. record.UpdatedAt = time.Now()
  156. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  157. return err
  158. }
  159. }
  160. }
  161. }
  162. return nil
  163. }