|
|
@@ -0,0 +1,267 @@ |
|
|
|
package svc |
|
|
|
|
|
|
|
import ( |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/lib/comm_plan" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md" |
|
|
|
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" |
|
|
|
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" |
|
|
|
"fmt" |
|
|
|
"xorm.io/xorm" |
|
|
|
) |
|
|
|
|
|
|
|
func IntegralRelease(engine *xorm.Engine, uid, level int, returnAllLevel bool, IsShowExtend, pvd string, user *model.User, comf float64, price string, cfg *comm_plan.PlanOpt, isGoods string, commArr map[string]string) (pvdFee, sysFee, subsidyFee float64, lvUser *comm_plan.LvUser, err error) { |
|
|
|
//佣金扣除抽成后 |
|
|
|
fee, pvdFee, sysFee := CommFee(comf, cfg, "commission", isGoods) |
|
|
|
////积分扣除抽成后 |
|
|
|
integralFee, _, _ := CommFee(zhios_order_relate_utils.StrToFloat64(price), cfg, "integral", isGoods) |
|
|
|
//常规级差 |
|
|
|
var userRelationship *[]md.UserRelation |
|
|
|
if returnAllLevel { |
|
|
|
userRelationship, err = UserRelativeNetwork(engine, uid, 2) |
|
|
|
} else if IsShowExtend == "1" { |
|
|
|
//userRelationship, err = UserRelativeNetwork(engine, uid, 1) |
|
|
|
userRelationship = &[]md.UserRelation{ |
|
|
|
{ |
|
|
|
CurUid: user.Uid, |
|
|
|
Uid: user.Uid, |
|
|
|
Diff: 1, |
|
|
|
Level: user.Level, |
|
|
|
}, |
|
|
|
} |
|
|
|
uid = 0 |
|
|
|
level = 0 |
|
|
|
} else { |
|
|
|
userRelationship = nil |
|
|
|
} |
|
|
|
//如果没登录,要找出权重最低的那个 |
|
|
|
if uid == 0 { |
|
|
|
lvList, err := db.UserLevelInIDescByWeightLow(engine) |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err) |
|
|
|
} |
|
|
|
if lvList != nil { |
|
|
|
for _, v := range lvList { |
|
|
|
level = v.Id |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//查出所有等级替换权重 |
|
|
|
levelList, _ := db.UserLevlEgAll(engine) |
|
|
|
levelWeight := 0 |
|
|
|
for _, v1 := range levelList { |
|
|
|
if v1.Id == level { |
|
|
|
levelWeight = v1.LevelWeight |
|
|
|
} |
|
|
|
} |
|
|
|
lvUser = &comm_plan.LvUser{Uid: uid, Lv: level, Diff: 0, LevelWeight: levelWeight} |
|
|
|
var node = lvUser |
|
|
|
if userRelationship != nil { |
|
|
|
//关系链处理 |
|
|
|
for _, v := range *userRelationship { |
|
|
|
if v.Uid == 0 { |
|
|
|
continue |
|
|
|
} |
|
|
|
for _, v1 := range levelList { |
|
|
|
levelId := v.Level |
|
|
|
if v1.Id == levelId { |
|
|
|
levelWeight = v1.LevelWeight |
|
|
|
} |
|
|
|
} |
|
|
|
node.ParentUser = &comm_plan.LvUser{Uid: v.Uid, Lv: v.Level, Diff: v.Diff, OldDiff: v.OldDiff, LevelWeight: levelWeight} |
|
|
|
node = node.ParentUser |
|
|
|
} |
|
|
|
} |
|
|
|
err = CalIntegralRelease(cfg, fee, integralFee, lvUser, commArr) |
|
|
|
if err != nil { |
|
|
|
return 0, 0, 0, nil, err |
|
|
|
} |
|
|
|
return pvdFee, sysFee, subsidyFee, lvUser, nil |
|
|
|
} |
|
|
|
|
|
|
|
// 按总佣金的比例进行划分计算 |
|
|
|
func CalIntegralRelease(opt *comm_plan.PlanOpt, totalAmt, integralTotalAmt float64, userList *comm_plan.LvUser, commArr map[string]string) error { |
|
|
|
grade := opt.UserRate |
|
|
|
if len(grade) == 0 { |
|
|
|
return zhios_order_relate_logx.Warn("level grade is not set") |
|
|
|
} |
|
|
|
//查出用户自购佣金 |
|
|
|
commission, _, amountList, ratioList := CalReturnAmountAndRatio(userList.Lv, userList.OwnbuyReturnType, "own", totalAmt, integralTotalAmt, opt, commArr) |
|
|
|
userList.Profit = commission // 另外出来的佣金 兼容旧的 |
|
|
|
userList.ProfitList = amountList // 各币种分佣 |
|
|
|
ratioListMap := convertList2Map(ratioList) |
|
|
|
for k, v := range userList.ProfitList { |
|
|
|
userList.ProfitList[k].Val = ratioListMap[v.Cid] * v.Val |
|
|
|
} |
|
|
|
// 各种币换算出总的额度 |
|
|
|
totalAmtList := make([]*comm_plan.VirtualCoinCommission, 0) |
|
|
|
for coinId, rate := range opt.VirtualCoinMoneyRatioList { |
|
|
|
var amount float64 |
|
|
|
if coinId == 0 { |
|
|
|
amount = totalAmt |
|
|
|
} else { |
|
|
|
amount = integralTotalAmt * zhios_order_relate_utils.AnyToFloat64(rate) |
|
|
|
} |
|
|
|
totalAmtList = append(totalAmtList, &comm_plan.VirtualCoinCommission{ |
|
|
|
Cid: zhios_order_relate_utils.AnyToString(coinId), |
|
|
|
Val: amount, |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
var ( |
|
|
|
node = userList |
|
|
|
maxLevelWeight = node.LevelWeight // 当前权重 |
|
|
|
) |
|
|
|
for node.ParentUser != nil { //查找上级用户 |
|
|
|
node.ParentUser.Profit = 0 |
|
|
|
// 如果父级比当前级别低, 跳过 |
|
|
|
if node.ParentUser.LevelWeight >= maxLevelWeight && node.ParentUser.Diff <= 2 { |
|
|
|
if _, ok := grade[node.Lv]; !ok { |
|
|
|
return zhios_order_relate_logx.Warn("level grade node.Lv is not set") |
|
|
|
} |
|
|
|
if _, ok := grade[node.ParentUser.Lv]; !ok { |
|
|
|
return zhios_order_relate_logx.Warn("level grade node.ParentUser.Lv is not set") |
|
|
|
} |
|
|
|
userType := "" |
|
|
|
if node.ParentUser.Diff == 1 { |
|
|
|
userType = "first_extend" |
|
|
|
} |
|
|
|
if node.ParentUser.Diff == 2 { |
|
|
|
userType = "second_extend" |
|
|
|
} |
|
|
|
commission, _, amountList, _ := CalReturnAmountAndRatio(node.ParentUser.Lv, userList.OwnbuyReturnType, userType, totalAmt, integralTotalAmt, opt, commArr) |
|
|
|
//佣金 |
|
|
|
node.ParentUser.Profit = commission |
|
|
|
//积分 |
|
|
|
node.ParentUser.ProfitList = amountList |
|
|
|
// 等级往上升则置0 |
|
|
|
maxLevelWeight = node.ParentUser.LevelWeight |
|
|
|
} |
|
|
|
node.Profit = zhios_order_relate_utils.StrToFloat64(fmt.Sprintf("%.4f", node.Profit)) |
|
|
|
node = node.ParentUser |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
//佣金 积分 区块币计算 |
|
|
|
func CalReturnAmountAndRatio(level, ownbuyReturnType int, userType string, fee, integralFee float64, opt *comm_plan.PlanOpt, commArr map[string]string) (commission, commissionRatio float64, amountList, ratioList []*comm_plan.VirtualCoinCommission) { |
|
|
|
|
|
|
|
lvGrade := opt.UserRate[level] |
|
|
|
if lvGrade.SelfRateList != nil { |
|
|
|
commissionRatioStr := getVirtualCoinRatio(userType, level, opt.UserRate, "0") |
|
|
|
commissionRatio = zhios_order_relate_utils.StrToFloat64(commissionRatioStr) |
|
|
|
} |
|
|
|
//(商品价格*佣金比例10%-手续费%)*(可分配系数31%-税3%-运营费用1%)*15% |
|
|
|
if userType == "first_extend" || userType == "second_extend" { |
|
|
|
fee = getfee(fee, commArr) |
|
|
|
} |
|
|
|
commission = fee * commissionRatio |
|
|
|
|
|
|
|
var maxReturnTypeCount = 0 |
|
|
|
var returnType = make([]string, 0) |
|
|
|
for _, v := range opt.UserRate { |
|
|
|
if v.ReturnType != nil { |
|
|
|
if len(v.ReturnType) > maxReturnTypeCount { |
|
|
|
maxReturnTypeCount = len(v.ReturnType) |
|
|
|
returnType = v.ReturnType |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 新版支持多种虚拟币 支持的种类id保存在ReturnType id=0代表现金佣金 其他为虚拟币 |
|
|
|
if returnType != nil { //返佣类型 |
|
|
|
for _, coinId := range returnType { |
|
|
|
newFee := integralFee |
|
|
|
if coinId == "0" { |
|
|
|
newFee = fee |
|
|
|
} |
|
|
|
if userType == "first_extend" || userType == "second_extend" { |
|
|
|
newFee = getfee(newFee, commArr) |
|
|
|
} |
|
|
|
ratio := getVirtualCoinRatio(userType, level, opt.UserRate, coinId) |
|
|
|
amount := getCoinAmount(ratio, zhios_order_relate_utils.StrToInt(coinId), newFee, opt.VirtualCoinMoneyRatioList) |
|
|
|
if coinId != "0" && opt.Mode == "lv_winery" && userType == "own" { //酒庄模式不在这里计算 |
|
|
|
amount = 0 |
|
|
|
} |
|
|
|
amountList = append(amountList, &comm_plan.VirtualCoinCommission{ |
|
|
|
Cid: coinId, |
|
|
|
Val: amount, |
|
|
|
}) |
|
|
|
ratioList = append(ratioList, &comm_plan.VirtualCoinCommission{ |
|
|
|
Cid: coinId, |
|
|
|
Val: zhios_order_relate_utils.AnyToFloat64(ratio), |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ownbuyReturnType == 1 { //自购不返利 |
|
|
|
commission = 0 |
|
|
|
for i := range amountList { |
|
|
|
amountList[i].Val = 0 |
|
|
|
} |
|
|
|
} |
|
|
|
commission = zhios_order_relate_utils.FloatFormat(commission, 2) |
|
|
|
|
|
|
|
for i, coin := range amountList { |
|
|
|
amountList[i].Val = zhios_order_relate_utils.FloatFormat(coin.Val, 6) |
|
|
|
} |
|
|
|
|
|
|
|
return commission, commissionRatio, amountList, ratioList |
|
|
|
} |
|
|
|
func getfee(fee float64, commArr map[string]string) float64 { |
|
|
|
newfee := fee |
|
|
|
if zhios_order_relate_utils.StrToFloat64(commArr["alipayServiceChargeRate"]) > 0 { |
|
|
|
newfee = fee - fee*zhios_order_relate_utils.StrToFloat64(commArr["alipayServiceChargeRate"]) |
|
|
|
} |
|
|
|
if zhios_order_relate_utils.StrToFloat64(commArr["taxRevenueRate"]) > 0 { |
|
|
|
newfee = newfee - newfee*zhios_order_relate_utils.StrToFloat64(commArr["taxRevenueRate"]) |
|
|
|
} |
|
|
|
if zhios_order_relate_utils.StrToFloat64(commArr["operatingExpenseRate"]) > 0 { |
|
|
|
newfee = newfee - newfee*zhios_order_relate_utils.StrToFloat64(commArr["operatingExpenseRate"]) |
|
|
|
} |
|
|
|
if zhios_order_relate_utils.StrToFloat64(commArr["distributableCoefficientRate"]) > 0 { |
|
|
|
newfee = newfee - newfee*zhios_order_relate_utils.StrToFloat64(commArr["distributableCoefficientRate"]) |
|
|
|
} |
|
|
|
return newfee |
|
|
|
} |
|
|
|
|
|
|
|
// 获取佣金、虚拟币比例 0=佣金 |
|
|
|
func getVirtualCoinRatio(typ string, level int, grade map[int]*comm_plan.LvGrade, coinId string) (ratio string) { |
|
|
|
if grade[level].ReturnType == nil { |
|
|
|
return "0" |
|
|
|
} |
|
|
|
if zhios_order_relate_utils.InArr(coinId, grade[level].ReturnType) == false { |
|
|
|
return "0" |
|
|
|
} |
|
|
|
ok := false |
|
|
|
switch typ { |
|
|
|
case "first_extend": |
|
|
|
ratio, ok = grade[level].FirstExtend[coinId] |
|
|
|
case "second_extend": |
|
|
|
ratio, ok = grade[level].SecondExtend[coinId] |
|
|
|
default: |
|
|
|
ratio, ok = grade[level].SelfRateList[coinId] |
|
|
|
} |
|
|
|
if !ok { |
|
|
|
ratio = "0" |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// 计算佣金、虚拟币额度 |
|
|
|
func getCoinAmount(ratio string, coinId int, fee float64, rateList map[int]string) (amount float64) { |
|
|
|
moneyRate, ok := rateList[coinId] |
|
|
|
if !ok { |
|
|
|
amount = 0.00 |
|
|
|
return |
|
|
|
} |
|
|
|
if coinId == 0 { // 金额 |
|
|
|
amount = fee * zhios_order_relate_utils.StrToFloat64(ratio) |
|
|
|
} else { // 虚拟币 需要将金额按设置的比例兑换成虚拟币 这里不乘比例了 会影响后面的极差 |
|
|
|
//amount = fee * zhios_order_relate_utils.AnyToFloat64(moneyRate) * zhios_order_relate_utils.StrToFloat64(ratio) |
|
|
|
amount = fee * zhios_order_relate_utils.AnyToFloat64(moneyRate) |
|
|
|
} |
|
|
|
return |
|
|
|
} |