|
- package plan
-
- import (
- "applet/app/utils"
- "applet/app/utils/logx"
- )
-
- // 按自购佣金进行计算
- func CalcSelf(grade map[int]*LvGrade, totalAmt float64, userList *LvUser, pvd string, sysFee float64, opt *PlanOpt) error {
- if len(grade) == 0 {
- return logx.Warn("level grade is not set")
- }
- // 第一层为自购用户
- userList.Profit = totalAmt * grade[userList.Lv].SelfRate
- var (
- node = userList
- maxLv int = node.Lv // 当前等级
- maxLevelWeight int = node.LevelWeight // 当前权重
- peerNum int = 0 // 存在同级数
- peerRate float64 = 0 // 同级累计的比例
- selfAmt float64 = userList.Profit // 自购剩余数量
- restAmt float64 = totalAmt - userList.Profit // 剩余数量
- )
- for node.ParentUser != nil {
- node.ParentUser.Profit = 0
- if restAmt <= 0 {
- break
- }
- // 如果是同级, 而且有设置等级
- if node.ParentUser.LevelWeight == maxLevelWeight && len(grade[maxLv].PeerRate) > peerNum {
- node.ParentUser.Profit = selfAmt * grade[maxLv].PeerRate[peerNum]
- peerRate = utils.FloatFormat(peerRate+grade[maxLv].PeerRate[peerNum], 6)
- peerNum++
- // 如果当前的钱比剩余数量要多
- if node.ParentUser.Profit > restAmt {
- node.ParentUser.Profit = 0
- goto NEXT
- }
- // 余额全部消耗完
- if node.ParentUser.Profit == restAmt {
- restAmt = 0
- goto NEXT
- }
- restAmt = restAmt - node.ParentUser.Profit
- } else if node.ParentUser.LevelWeight > maxLevelWeight {
- node.ParentUser.Profit = selfAmt * utils.FloatFormat(grade[node.ParentUser.Lv].TeamRate-grade[node.Lv].TeamRate-peerRate, 6)
- if node.ParentUser.Profit > restAmt || node.ParentUser.Profit < 0 {
- node.ParentUser.Profit = 0
- goto NEXT
- }
- if restAmt == node.ParentUser.Profit {
- goto NEXT
- }
- restAmt = utils.FloatFormat(restAmt-node.ParentUser.Profit, 6)
- maxLevelWeight, maxLv, peerNum, peerRate = node.ParentUser.LevelWeight, node.ParentUser.Lv, 0, 0
- }
- NEXT:
- node.Profit = utils.FloatFormat(node.Profit, 2)
- node = node.ParentUser
- }
- logx.Debugf("\n\n总额: %v, 使用: %v, 剩余: %v \n\n", totalAmt, totalAmt-restAmt, restAmt)
- return nil
- }
-
- /*func Test() {
- g := map[int]*LvGrade{
- 0: {
- Lv: 0,
- SelfRate: 0.4,
- TeamRate: 0,
- PeerRate: []float64{0.05, 0.03, 0.02},
- },
- 1: {Lv: 1,
- SelfRate: 0.5,
- TeamRate: 0.6,
- PeerRate: []float64{0.1, 0.05, 0.03},
- },
- 2: {
- Lv: 2,
- SelfRate: 0.6,
- TeamRate: 0.7,
- PeerRate: []float64{},
- },
- }
- Init(g)
- var money float64 = 100
- u := []int{0, 0, 1, 1, 2}
- var uList *LvUser
- var node *LvUser
- for k, v := range u {
- if uList == nil {
- uList = &LvUser{
- Uid: k,
- Lv: v,
- }
- node = uList
- } else {
- if node != nil {
- node.ParentUser = &LvUser{
- Uid: k,
- Lv: v,
- }
- node = node.ParentUser
- }
- }
- }
- fmt.Println(u, node, uList)
-
- CalcSelf(g, money, uList)
- fmt.Println("final:", uList)
- for uList.ParentUser != nil {
- fmt.Printf("%#v\n", uList)
- uList = uList.ParentUser
- if uList.ParentUser == nil {
- fmt.Printf("%#v", uList)
- }
- }
- }
- */
|