|
|
@@ -2,14 +2,17 @@ package rule |
|
|
|
|
|
|
|
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/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" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"math" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
"xorm.io/xorm" |
|
|
|
) |
|
|
|
|
|
|
@@ -103,3 +106,108 @@ func BatchGetPublicPlatoonRelateCommissionByOrder(engine *xorm.Engine, masterId |
|
|
|
} |
|
|
|
return resp, nil |
|
|
|
} |
|
|
|
|
|
|
|
// AddPublicPlatoonRelateCommission 新增公排用户关系记录 |
|
|
|
func AddPublicPlatoonRelateCommission(engine *xorm.Engine, masterId string, AddPublicPlatoonRelateCommissionReqList []*md.AddPublicPlatoonRelateCommissionReq) (map[string]*model.UserPublicPlatoonRelation, error) { |
|
|
|
var resp map[string]*model.UserPublicPlatoonRelation |
|
|
|
//查找 `user_public_platoon_setting` 基础设置 |
|
|
|
userPublicPlatoonSetting, err := db.UserPublicPlatoonSettingGetOneByParams(engine, map[string]interface{}{ |
|
|
|
"key": "is_open", |
|
|
|
"value": 1, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
for _, param := range AddPublicPlatoonRelateCommissionReqList { |
|
|
|
res, err := publicPlatoon(engine, zhios_order_relate_utils.StrToInt(param.Uid), zhios_order_relate_utils.StrToInt(param.RecommendUid), *userPublicPlatoonSetting) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
resp[param.Uid] = &res |
|
|
|
} |
|
|
|
return resp, nil |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
publicPlatoon 公排方法 |
|
|
|
TODO 相关公式: |
|
|
|
1: 每个等级的起始值(1+5^0+5^1+5^2+...+5^x-2), 每个等级的结束值(`5^0+5^1+5^2+...+5^x-1) |
|
|
|
2: 根据position查找pid { (position-1)/5 } |
|
|
|
3: 根据position查找等级level {position-5^0-5^1-5^2-...-5^x 是否 <0 ? = x+1 } |
|
|
|
*/ |
|
|
|
func publicPlatoon(engine *xorm.Engine, uid, recommendUid int, userPublicPlatoonSetting model.UserPublicPlatoonSetting) (model.UserPublicPlatoonRelation, error) { |
|
|
|
var userPublicPlatoonRelation model.UserPublicPlatoonRelation |
|
|
|
//1、 查找当前 user_public_platoon_relation 中 `position` 最大的记录 |
|
|
|
var m model.UserPublicPlatoonRelation |
|
|
|
if has, err := engine.OrderBy("position desc").Get(&m); err != nil || has == false { |
|
|
|
return userPublicPlatoonRelation, err |
|
|
|
} |
|
|
|
|
|
|
|
position := m.Position + 1 |
|
|
|
pid := makeSearchPid(position, userPublicPlatoonSetting.SeveralTimes) |
|
|
|
var level float64 |
|
|
|
makeSearchLevel(&position, float64(userPublicPlatoonSetting.SeveralTimes), &level) |
|
|
|
level = level + 1 //TODO::根据公式需要 + 1 |
|
|
|
|
|
|
|
//2、查找对应pid的 user_public_platoon_relation 记录 |
|
|
|
var m1 model.UserPublicPlatoonRelation |
|
|
|
if has, err := engine.Where("uid=?", pid).Get(&m1); err != nil || has == false { |
|
|
|
return userPublicPlatoonRelation, err |
|
|
|
} |
|
|
|
var fatherUid string |
|
|
|
if m1.FatherUid == "" { |
|
|
|
//TODO::顶级 |
|
|
|
fatherUid = zhios_order_relate_utils.IntToStr(m1.Uid) |
|
|
|
} else { |
|
|
|
fatherUid = zhios_order_relate_utils.IntToStr(m1.Uid) + "-" + m1.FatherUid |
|
|
|
} |
|
|
|
|
|
|
|
//唯一标识符(父级id-uid-等级-位置) |
|
|
|
var uniqueIdentifier = zhios_order_relate_utils.IntToStr(pid) + zhios_order_relate_utils.IntToStr(uid) + zhios_order_relate_utils.Float64ToStr(level) + zhios_order_relate_utils.IntToStr(position) |
|
|
|
|
|
|
|
// 3、插入 user_public_platoon_relation 记录 |
|
|
|
now := time.Now() |
|
|
|
userPublicPlatoonRelation.Uid = uid |
|
|
|
userPublicPlatoonRelation.FatherUid = fatherUid |
|
|
|
userPublicPlatoonRelation.Pid = pid |
|
|
|
userPublicPlatoonRelation.RecommendUid = recommendUid |
|
|
|
userPublicPlatoonRelation.Level = int(level) |
|
|
|
userPublicPlatoonRelation.Position = position |
|
|
|
userPublicPlatoonRelation.UniqueIdentifier = uniqueIdentifier |
|
|
|
userPublicPlatoonRelation.CreateAt = now |
|
|
|
userPublicPlatoonRelation.UpdateAt = now |
|
|
|
_, err := db.UserPublicPlatoonRelationInsert(engine, &userPublicPlatoonRelation) |
|
|
|
if err != nil { |
|
|
|
return model.UserPublicPlatoonRelation{}, err |
|
|
|
} |
|
|
|
return userPublicPlatoonRelation, nil |
|
|
|
} |
|
|
|
|
|
|
|
//递归查找等级 |
|
|
|
func makeSearchLevel(position *int, rows float64, times *float64) (level int) { |
|
|
|
difference := *position - int(math.Pow(rows, *times)) |
|
|
|
if difference <= 0 { |
|
|
|
return int(*times) |
|
|
|
} else { |
|
|
|
position = &difference |
|
|
|
*times++ |
|
|
|
return makeSearchLevel(position, rows, times) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//查找归属父级id |
|
|
|
func makeSearchPid(position int, row int) (pid int) { |
|
|
|
divisionValue := (position - 1) / row |
|
|
|
if divisionValue == 0 { |
|
|
|
pid = 1 |
|
|
|
return |
|
|
|
} else { |
|
|
|
if (divisionValue*row + 1) == position { |
|
|
|
pid = divisionValue |
|
|
|
return |
|
|
|
} else { |
|
|
|
pid = divisionValue + 1 |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
} |