golang 的 rabbitmq 消费项目
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.
 
 
 

120 lines
3.1 KiB

  1. package plan
  2. import (
  3. "applet/app/utils"
  4. "applet/app/utils/logx"
  5. )
  6. // 按自购佣金进行计算
  7. func CalcSelf(grade map[int]*LvGrade, totalAmt float64, userList *LvUser, pvd string, sysFee float64, opt *PlanOpt) error {
  8. if len(grade) == 0 {
  9. return logx.Warn("level grade is not set")
  10. }
  11. // 第一层为自购用户
  12. userList.Profit = totalAmt * grade[userList.Lv].SelfRate
  13. var (
  14. node = userList
  15. maxLv int = node.Lv // 当前等级
  16. maxLevelWeight int = node.LevelWeight // 当前权重
  17. peerNum int = 0 // 存在同级数
  18. peerRate float64 = 0 // 同级累计的比例
  19. selfAmt float64 = userList.Profit // 自购剩余数量
  20. restAmt float64 = totalAmt - userList.Profit // 剩余数量
  21. )
  22. for node.ParentUser != nil {
  23. node.ParentUser.Profit = 0
  24. if restAmt <= 0 {
  25. break
  26. }
  27. // 如果是同级, 而且有设置等级
  28. if node.ParentUser.LevelWeight == maxLevelWeight && len(grade[maxLv].PeerRate) > peerNum {
  29. node.ParentUser.Profit = selfAmt * grade[maxLv].PeerRate[peerNum]
  30. peerRate = utils.FloatFormat(peerRate+grade[maxLv].PeerRate[peerNum], 6)
  31. peerNum++
  32. // 如果当前的钱比剩余数量要多
  33. if node.ParentUser.Profit > restAmt {
  34. node.ParentUser.Profit = 0
  35. goto NEXT
  36. }
  37. // 余额全部消耗完
  38. if node.ParentUser.Profit == restAmt {
  39. restAmt = 0
  40. goto NEXT
  41. }
  42. restAmt = restAmt - node.ParentUser.Profit
  43. } else if node.ParentUser.LevelWeight > maxLevelWeight {
  44. node.ParentUser.Profit = selfAmt * utils.FloatFormat(grade[node.ParentUser.Lv].TeamRate-grade[node.Lv].TeamRate-peerRate, 6)
  45. if node.ParentUser.Profit > restAmt || node.ParentUser.Profit < 0 {
  46. node.ParentUser.Profit = 0
  47. goto NEXT
  48. }
  49. if restAmt == node.ParentUser.Profit {
  50. goto NEXT
  51. }
  52. restAmt = utils.FloatFormat(restAmt-node.ParentUser.Profit, 6)
  53. maxLevelWeight, maxLv, peerNum, peerRate = node.ParentUser.LevelWeight, node.ParentUser.Lv, 0, 0
  54. }
  55. NEXT:
  56. node.Profit = utils.FloatFormat(node.Profit, 2)
  57. node = node.ParentUser
  58. }
  59. logx.Debugf("\n\n总额: %v, 使用: %v, 剩余: %v \n\n", totalAmt, totalAmt-restAmt, restAmt)
  60. return nil
  61. }
  62. /*func Test() {
  63. g := map[int]*LvGrade{
  64. 0: {
  65. Lv: 0,
  66. SelfRate: 0.4,
  67. TeamRate: 0,
  68. PeerRate: []float64{0.05, 0.03, 0.02},
  69. },
  70. 1: {Lv: 1,
  71. SelfRate: 0.5,
  72. TeamRate: 0.6,
  73. PeerRate: []float64{0.1, 0.05, 0.03},
  74. },
  75. 2: {
  76. Lv: 2,
  77. SelfRate: 0.6,
  78. TeamRate: 0.7,
  79. PeerRate: []float64{},
  80. },
  81. }
  82. Init(g)
  83. var money float64 = 100
  84. u := []int{0, 0, 1, 1, 2}
  85. var uList *LvUser
  86. var node *LvUser
  87. for k, v := range u {
  88. if uList == nil {
  89. uList = &LvUser{
  90. Uid: k,
  91. Lv: v,
  92. }
  93. node = uList
  94. } else {
  95. if node != nil {
  96. node.ParentUser = &LvUser{
  97. Uid: k,
  98. Lv: v,
  99. }
  100. node = node.ParentUser
  101. }
  102. }
  103. }
  104. fmt.Println(u, node, uList)
  105. CalcSelf(g, money, uList)
  106. fmt.Println("final:", uList)
  107. for uList.ParentUser != nil {
  108. fmt.Printf("%#v\n", uList)
  109. uList = uList.ParentUser
  110. if uList.ParentUser == nil {
  111. fmt.Printf("%#v", uList)
  112. }
  113. }
  114. }
  115. */