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

192 lines
5.8 KiB

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 = 499
  46. )
  47. func GenRedPack(amount, num int) []int {
  48. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  49. sumMoney := 0
  50. redPacks := make([]int, num)
  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. // 删除掉之前存在对的
  78. _, err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=? "+
  79. " and shake_red_envelope_activity_id=? and activity_id=? and rehearsal_id=?",
  80. rule.Id, rule.ShakeRedEnvelopeActivityId, aid, rid).Cols("is_delete").
  81. Update(&models.ShakeRedEnvelopeRecord{IsDelete: true})
  82. if err != nil {
  83. return err
  84. }
  85. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  86. if rule.Model == define.SHAKERB_RULE_RANDOM { // 随机红包
  87. //redPacks := GenRedPack(int(rule.RandSum*100), int(rule.RedEnvelopeNum))
  88. //for _, redpack := range redPacks {
  89. // record := new(models.ShakeRedEnvelopeRecord)
  90. // record.ActivityId = aid
  91. // record.RehearsalId = rid
  92. // record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  93. // record.ShakeRedEnvelopeRuleId = rule.Id
  94. // record.Amount = float64(redpack) / 100
  95. // record.IsDraw = -1
  96. // record.IsDelete = false
  97. // record.CreatedAt = time.Now()
  98. // record.UpdatedAt = time.Now()
  99. // if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  100. // return err
  101. // }
  102. //}
  103. // 检测红包是否存在
  104. sumMoney := 0.0
  105. for i := 0; i < int(rule.RedEnvelopeNum); i++ {
  106. money := r.Float64() * 2 * (rule.RandSum - sumMoney) / float64(int(rule.RedEnvelopeNum)-i)
  107. amount := utils.Float64CusDecimal(money, 2)
  108. if amount <= MinRedPackAmount { // 随机的金额可能小于1块钱
  109. amount = MinRedPackAmount
  110. } else if amount >= MaxRedPackAmount {
  111. amount = MaxRedPackAmount
  112. }
  113. // 提前预判剩余红包和红包的金额比例 ==> money/num <= 0.01
  114. rate := (rule.RandSum - sumMoney - amount) / float64(int(rule.RedEnvelopeNum)-i)
  115. if rate <= MinRedPackAmount {
  116. amount = MinRedPackAmount
  117. } else if rate >= MaxRedPackAmount {
  118. amount = MaxRedPackAmount
  119. }
  120. if int(rule.RedEnvelopeNum)-i == 1 {
  121. amount = rule.RandSum - sumMoney
  122. }
  123. record := new(models.ShakeRedEnvelopeRecord)
  124. record.ActivityId = aid
  125. record.RehearsalId = rid
  126. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  127. record.ShakeRedEnvelopeRuleId = rule.Id
  128. record.Amount = amount
  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. sumMoney += record.Amount
  137. }
  138. } else if rule.Model == define.SHAKERB_RULE_COMMON { // 普通红包
  139. for i := 0; i < int(rule.RedEnvelopeNum); i++ {
  140. record := new(models.ShakeRedEnvelopeRecord)
  141. record.ActivityId = aid
  142. record.RehearsalId = rid
  143. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  144. record.ShakeRedEnvelopeRuleId = rule.Id
  145. record.Amount = rule.Single
  146. record.IsDraw = -1
  147. record.IsDelete = false
  148. record.CreatedAt = time.Now()
  149. record.UpdatedAt = time.Now()
  150. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  151. return err
  152. }
  153. }
  154. } else if rule.Model == define.SHAKERB_RULE_LADDER { // 阶梯红包
  155. ladders := make([]*models.ShakeRedEnvelopeRuleLadder, 0)
  156. err := core.GetXormAuto().Where("is_delete=0 and shake_red_envelope_rule_id=?", rule.Id).
  157. Desc("created_at").Find(&ladders)
  158. if err != nil {
  159. return err
  160. }
  161. for _, ladder := range ladders {
  162. for i := 0; i < int(ladder.RedEnvelopeNum); i++ {
  163. record := new(models.ShakeRedEnvelopeRecord)
  164. record.ActivityId = aid
  165. record.RehearsalId = rid
  166. record.ShakeRedEnvelopeActivityId = rule.ShakeRedEnvelopeActivityId
  167. record.ShakeRedEnvelopeRuleId = rule.Id
  168. record.Amount = ladder.Single
  169. record.IsDraw = -1
  170. record.CreatedAt = time.Now()
  171. record.UpdatedAt = time.Now()
  172. if _, err := core.GetXormAuto().InsertOne(record); err != nil {
  173. return err
  174. }
  175. }
  176. }
  177. }
  178. return nil
  179. }