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.

all.go 11 KiB

6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package plan
  2. import (
  3. "fmt"
  4. "applet/app/utils"
  5. "applet/app/utils/logx"
  6. )
  7. //佣金 积分 区块币计算
  8. func BuyBili(level, ownbuyReturnType, peerNum int, userType string, fee float64, opt *PlanOpt) (float64, float64) {
  9. var commissionBili = opt.UserRate[level].SelfRate
  10. var commission = fee * commissionBili
  11. if userType == "same_lv" {
  12. commissionBili = opt.UserRate[level].PeerRate[peerNum]
  13. }
  14. if userType == "team" {
  15. commissionBili = opt.UserRate[level].TeamRate
  16. }
  17. commission = fee * commissionBili
  18. // 这里只用金额的 不用ReturnType了
  19. /*if opt.UserRate[level].ReturnType != nil { //返佣类型
  20. commissionBili = 0
  21. commission = 0
  22. for _, v := range opt.UserRate[level].ReturnType {
  23. if v == "commission" || v == "0" { //佣金 v==0 兼容新的版本 0代表金额
  24. commissionBili = utils.StrToFloat64(opt.UserRate[level].SelfRateList.Commission)
  25. if userType == "same_lv" { //同级处理
  26. commissionBili = utils.StrToFloat64(opt.UserRate[level].PeerRateList[peerNum].Commission)
  27. }
  28. if userType == "team" { //团队的
  29. commissionBili = utils.StrToFloat64(opt.UserRate[level].TeamRateList.Commission)
  30. }
  31. commission = fee * commissionBili
  32. }
  33. if v == "integral" { //积分
  34. integralBili = utils.StrToFloat64(opt.UserRate[level].SelfRateList.Integral)
  35. if userType == "same_lv" { //同级处理
  36. integralBili = utils.StrToFloat64(opt.UserRate[level].PeerRateList[peerNum].Integral)
  37. }
  38. if userType == "team" { //团队的
  39. integralBili = utils.StrToFloat64(opt.UserRate[level].TeamRateList.Integral)
  40. }
  41. integral = fee * integralBili
  42. }
  43. if v == "block_icons" { //区块币
  44. blockIconsBili = utils.StrToFloat64(opt.UserRate[level].SelfRateList.BlockIcons)
  45. if userType == "same_lv" { //同级处理
  46. blockIconsBili = utils.StrToFloat64(opt.UserRate[level].PeerRateList[peerNum].Commission)
  47. }
  48. if userType == "team" { //团队的
  49. blockIconsBili = utils.StrToFloat64(opt.UserRate[level].TeamRateList.Commission)
  50. }
  51. blockIcons = fee * blockIconsBili
  52. }
  53. }
  54. }*/
  55. if ownbuyReturnType == 1 { //自购不返利
  56. commission = 0
  57. commissionBili = 0
  58. }
  59. commission = utils.FloatFormat(commission, 6)
  60. commissionBili = utils.FloatFormat(commissionBili, 6)
  61. return commission, commissionBili
  62. }
  63. // 按总佣金的比例进行划分计算
  64. func CalcAll(grade map[int]*LvGrade, totalAmt float64, userList *LvUser, pvd string, sysFee float64, opt *PlanOpt) error {
  65. if len(grade) == 0 {
  66. return logx.Warn("level grade is not set")
  67. }
  68. //查出用户自购佣金
  69. commission, commissionLeaveBili := BuyBili(userList.Lv, userList.OwnbuyReturnType, 0, "own", totalAmt, opt)
  70. userList.Profit = commission
  71. userList.SubsidyFee = 0
  72. var (
  73. node = userList
  74. maxLv = node.Lv // 当前等级
  75. maxLevelWeight = node.LevelWeight // 当前权重
  76. peerNum = 0 // 存在同级数
  77. peerRate float64 = 0 // 同级累计比例
  78. restAmt = totalAmt - userList.Profit // 剩余比例
  79. commissionBili = commissionLeaveBili // 累计佣金比例
  80. )
  81. Loop:
  82. for node.ParentUser != nil { //查找上级用户
  83. node.ParentUser.Profit = 0
  84. //佣金补贴奖励
  85. isCommissionSubsidyFee := 0
  86. commissionBreak := false
  87. var commissionSubsidyBili float64
  88. node.ParentUser.SubsidyFee, isCommissionSubsidyFee, commissionSubsidyBili = subsidyFee(grade, totalAmt, node.ParentUser, userList.NewLv, pvd, sysFee, "commission")
  89. // 如果父级比当前级别低, 跳过
  90. // 同级奖, 如果父级别与当前级别一致,并且设置了对应比例
  91. count := len(grade[maxLv].PeerRate)
  92. if grade[maxLv].PeerRateList != nil {
  93. count = len(grade[maxLv].PeerRateList)
  94. }
  95. // 同级奖
  96. if node.ParentUser.LevelWeight == maxLevelWeight && count > peerNum {
  97. //同级奖励比例
  98. commission, commissionLeaveBili := BuyBili(maxLv, userList.OwnbuyReturnType, peerNum, "same_lv", totalAmt, opt)
  99. //佣金
  100. node.ParentUser.Profit, restAmt, commissionBili, peerRate, node.ParentUser.SubsidyFee, commissionBreak = sameMoney(node.Lv, isCommissionSubsidyFee, restAmt, commission, peerRate, commissionBili, commissionLeaveBili, node.ParentUser.SubsidyFee, commissionSubsidyBili, opt, "commission", opt.UserRate[maxLv].ReturnType)
  101. //如果都为true就结束
  102. if commissionBreak {
  103. break Loop
  104. }
  105. peerNum++
  106. } else if node.ParentUser.LevelWeight > maxLevelWeight {
  107. if _, ok := grade[node.Lv]; !ok {
  108. return logx.Warn("level grade node.Lv is not set")
  109. }
  110. if _, ok := grade[node.ParentUser.Lv]; !ok {
  111. return logx.Warn("level grade node.ParentUser.Lv is not set")
  112. }
  113. _, commissionLeaveBili := BuyBili(node.ParentUser.Lv, userList.OwnbuyReturnType, peerNum, "team", totalAmt, opt)
  114. //佣金
  115. node.ParentUser.Profit = commissionLeaveBili
  116. node.ParentUser.Profit, restAmt, commissionBili, node.ParentUser.SubsidyFee, commissionBreak = teamDiffMoney(node.ParentUser.Profit, grade[node.Lv].PayMode, isCommissionSubsidyFee, totalAmt, restAmt, grade[node.ParentUser.Lv].TeamRate, commissionBili, peerRate, node.ParentUser.SubsidyFee, commissionSubsidyBili, "commission", grade[node.ParentUser.Lv].ReturnType)
  117. //如果都为true就结束
  118. if commissionBreak {
  119. break Loop
  120. }
  121. // 等级往上升则置0
  122. maxLevelWeight, maxLv, peerRate, peerNum = node.ParentUser.LevelWeight, node.ParentUser.Lv, 0, 0
  123. }
  124. node.Profit = utils.StrToFloat64(fmt.Sprintf("%.4f", node.Profit))
  125. node = node.ParentUser
  126. }
  127. return nil
  128. }
  129. //公共处理同级计算
  130. func sameMoney(lv, isSubsidyFee int, restAmt, profit, peerRate, totalBili, leaveBili, subsidyFee, subsidyBili float64, opt *PlanOpt, types string, returnType []string) (float64, float64, float64, float64, float64, bool) {
  131. if returnType != nil {
  132. isReturn := 1
  133. for _, v := range returnType {
  134. if v == types || (v == "0" && types == "commission") {
  135. isReturn = 0
  136. }
  137. }
  138. if isReturn == 1 { //该方式没有返利
  139. return profit, restAmt, totalBili, peerRate, subsidyFee, false
  140. }
  141. } else if types != "commission" && types != "0" { //该方式没有返利 兼容旧版
  142. return profit, restAmt, totalBili, peerRate, subsidyFee, false
  143. }
  144. //如果不够扣了,并且是比例返利就跳过
  145. if restAmt < profit {
  146. profit = 0
  147. }
  148. //如果都邓毅0才跳过
  149. if profit == 0 {
  150. return profit, restAmt, totalBili, peerRate, subsidyFee, true
  151. }
  152. //极差返利
  153. if opt.UserRate[lv].PayMode == 0 && isSubsidyFee == 0 {
  154. restAmt -= profit // 剩余可分
  155. restAmt = utils.FloatFormat(restAmt, 6)
  156. peerRate += leaveBili
  157. totalBili += leaveBili
  158. } else if isSubsidyFee == 1 { //如果只返补贴 当成是极差的一部分 所以要扣 不是额外的
  159. profit = 0
  160. if opt.UserRate[lv].PayMode == 0 {
  161. if restAmt < subsidyFee {
  162. subsidyFee = 0
  163. return profit, restAmt, totalBili, subsidyFee, peerRate, true
  164. }
  165. restAmt -= subsidyFee // 剩余可分
  166. restAmt = utils.FloatFormat(restAmt, 6)
  167. totalBili += utils.FloatFormat(subsidyBili, 6)
  168. }
  169. }
  170. return profit, restAmt, totalBili, peerRate, subsidyFee, false
  171. }
  172. //公共处理下团队-上一层
  173. func teamDiffMoney(profit float64, payMode, isSubsidyFee int, totalAmt, restAmt, teamBili, totalBili, peerRate, subsidyFee, subsidyBili float64, types string, returnType []string) (float64, float64, float64, float64, bool) {
  174. if returnType != nil {
  175. isReturn := 1
  176. for _, v := range returnType {
  177. if v == types || (v == "0" && types == "commission") {
  178. isReturn = 0
  179. }
  180. }
  181. if isReturn == 1 { //该方式没有返利
  182. return profit, restAmt, totalBili, subsidyFee, false
  183. }
  184. } else if types != "commission" && types != "0" { //该方式没有返利 兼容旧版
  185. return profit, restAmt, totalBili, subsidyFee, false
  186. }
  187. // 如果是团队内部支出团队比例大于同级累计比例 或站长支出
  188. if payMode == 1 || teamBili > peerRate {
  189. teamBili = utils.FloatFormat(teamBili-totalBili, 6)
  190. }
  191. //极差返利
  192. if isSubsidyFee == 0 {
  193. totalBili += teamBili
  194. //出现负数跳过
  195. if teamBili <= 0 {
  196. profit = 0
  197. return profit, restAmt, totalBili, subsidyFee, true
  198. }
  199. profit = utils.FloatFormat(teamBili*totalAmt, 6)
  200. if restAmt < profit {
  201. profit = 0
  202. return profit, restAmt, totalBili, subsidyFee, true
  203. }
  204. restAmt -= profit // 剩余可分
  205. } else if isSubsidyFee == 1 { //如果只返补贴 当成是极差的一部分 所以要扣 不是额外的
  206. totalBili += utils.FloatFormat(subsidyBili, 6)
  207. profit = 0
  208. if restAmt < subsidyFee {
  209. profit = 0
  210. subsidyFee = 0
  211. return profit, restAmt, totalBili, subsidyFee, true
  212. }
  213. restAmt -= subsidyFee // 剩余可分
  214. }
  215. restAmt = utils.FloatFormat(restAmt, 6)
  216. return profit, restAmt, totalBili, subsidyFee, false
  217. }
  218. //补贴金额计算 仅针对会员费
  219. func subsidyFee(grade map[int]*LvGrade, totalAmt float64, lvuser *LvUser, newLv int, pvd string, sysFee float64, types string) (float64, int, float64) {
  220. lv := lvuser.Lv
  221. var subsidyFee float64 = 0
  222. var bilis float64 = 0
  223. //会员费分佣只有直推的奖励
  224. if pvd == "user_level_up" && lvuser.Diff != 1 {
  225. return subsidyFee, 0, bilis
  226. }
  227. if _, ok := grade[lv]; !ok {
  228. return 0, 0, 0
  229. }
  230. if grade[lv].UserSubsidyType == "" {
  231. grade[lv].UserSubsidyType = "up_lv"
  232. }
  233. isSubsidyFee := 0
  234. //只有额外补贴跟 分销补贴按钮都开启才有的分
  235. if grade[lv].SubsidyEnable == 1 && grade[lv].UserLvUpSubsidyEnable == 1 {
  236. //判断有没有开启 如果不是推荐会员模式
  237. if pvd == "user_level_up" && grade[lv].UserSubsidyType != "up_lv" {
  238. return subsidyFee, isSubsidyFee, bilis
  239. }
  240. //如果不是购买商品模式 跳过
  241. if pvd != "user_level_up" && grade[lv].UserSubsidyType != "buy_goods" {
  242. return subsidyFee, isSubsidyFee, bilis
  243. }
  244. //处理每个条件的返利
  245. if grade[lv].UserLvUpSubsidyList != nil {
  246. fmt.Println(grade[lv].UserLvUpSubsidyList)
  247. for k, v1 := range grade[lv].UserLvUpSubsidyList {
  248. v, ok := v1.(map[string]interface{})
  249. if ok {
  250. //如果不相等并且是会员升级的没的返
  251. if newLv != int(utils.AnyToInt64(v["lv"])) && pvd == "user_level_up" {
  252. continue
  253. }
  254. //如果层级不是当前层级 且不是会员升级
  255. if pvd != "user_level_up" && k+1 != lvuser.Diff {
  256. continue
  257. }
  258. //如果没开启与补贴共存 只能拿这个奖励
  259. if int(utils.AnyToInt64(v["is_use"])) != 1 {
  260. isSubsidyFee = 1
  261. }
  262. mode := grade[lv].SubsidyCommissionMode
  263. bili := utils.AnyToFloat64(v["bili"])
  264. if mode != "" { //换字段了
  265. bili = utils.AnyToFloat64(v["commission"])
  266. }
  267. if mode == "" { //兼容旧的
  268. if grade[lv].UserLvUpSubsidyMode == 1 {
  269. mode = "money"
  270. }
  271. }
  272. if types == "integral" { //积分
  273. mode = grade[lv].SubsidyIntegralMode
  274. bili = utils.AnyToFloat64(v["integral"])
  275. }
  276. if types == "block_icons" { //区块币
  277. mode = grade[lv].SubsidyBlockIconsMode
  278. bili = utils.AnyToFloat64(v["block_icons"])
  279. }
  280. //如果是按固定金额
  281. if mode == "money" {
  282. subsidyFee += bili
  283. bilis = bili / totalAmt
  284. } else { //按比例分
  285. bilis = bili / 100
  286. //如果是按利润
  287. if grade[lv].SubsidyMode == 1 {
  288. subsidyFee += sysFee * (bili / 100)
  289. } else { //如果按佣金
  290. subsidyFee += totalAmt * (bili / 100)
  291. }
  292. }
  293. }
  294. }
  295. }
  296. }
  297. subsidyFee = utils.FloatFormat(subsidyFee, 6)
  298. bilis = utils.FloatFormat(bilis, 6)
  299. return subsidyFee, isSubsidyFee, bilis
  300. }