From 392ce6c29c1ef234ad89f33b5a3a7b529b148618 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Thu, 7 Jul 2022 16:40:11 +0800 Subject: [PATCH] =?UTF-8?q?ADD=20=E5=85=B1=E5=AF=8C=E5=88=B6=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/model/user_public_platoon_relation.go | 1 + go.sum | 5 -- md/public_platoon_relate_commission.go | 11 ++- rule/public_platoon_relate_commission.go | 108 +++++++++++++++++++++++ 4 files changed, 117 insertions(+), 8 deletions(-) diff --git a/db/model/user_public_platoon_relation.go b/db/model/user_public_platoon_relation.go index e2d01a4..23e4367 100644 --- a/db/model/user_public_platoon_relation.go +++ b/db/model/user_public_platoon_relation.go @@ -8,6 +8,7 @@ type UserPublicPlatoonRelation struct { Id int `json:"id" xorm:"not null pk autoincr INT(11)"` Uid int `json:"uid" xorm:"not null default 0 comment('用户id') unique INT(11)"` FatherUid string `json:"father_uid" xorm:"not null default '' comment('父级id(123456-563464-438384)') index VARCHAR(100)"` + Pid int `json:"pid" xorm:"not null default 0 comment('父级id') INT(11)"` RecommendUid int `json:"recommend_uid" xorm:"not null default 0 comment('推荐人id') INT(11)"` Level int `json:"level" xorm:"not null default 1 comment('等级(整个系统)') INT(11)"` Position int `json:"position" xorm:"not null default 1 comment('当前等级上位置') INT(11)"` diff --git a/go.sum b/go.sum index 5c80a99..4f74e59 100644 --- a/go.sum +++ b/go.sum @@ -71,8 +71,6 @@ github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgO github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= -github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= -github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= @@ -98,9 +96,6 @@ github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8l github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/gomodule/redigo v1.8.8 h1:f6cXq6RRfiyrOJEV7p3JhLDlmawGBVBBP1MggY8Mo4E= -github.com/gomodule/redigo/redis v0.0.1 h1:tQQSZyg4O0N0Dh2hli1pOrRdj+WHl1xf3w/x7olDgu0= -github.com/gomodule/redigo/redis v0.0.1/go.mod h1:QhGMo2EGfdSsmrYDENZq12/Y23fRP6X6nyFZ4TGSUvM= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= diff --git a/md/public_platoon_relate_commission.go b/md/public_platoon_relate_commission.go index 61968d2..d713e7c 100644 --- a/md/public_platoon_relate_commission.go +++ b/md/public_platoon_relate_commission.go @@ -11,11 +11,11 @@ type LvGrade struct { } type PublicPlatoonRelateCommissionReq struct { - Pvd string `json:"pvd"` + Pvd string `json:"pvd"` //平台供应商 (taobao/jd/pdd/mall_goods ...) Uid string `json:"uid"` - UserLevel string `json:"user_level"` + UserLevel string `json:"user_level"` //用户等级 PendingAmount string `json:"pending_amount"` //待处理金额 - Oid string `json:"oid"` + Oid string `json:"oid"` //订单id } type PublicPlatoonRelateCommissionResp struct { @@ -28,3 +28,8 @@ type PublicPlatoonRelateCommissionResp struct { CommonWealthSystem map[string]string `json:"common_wealth_system"` // 共富制度奖励 DirectPush map[string]string `json:"direct_push"` // 直推奖励 } + +type AddPublicPlatoonRelateCommissionReq struct { + Uid string `json:"uid"` + RecommendUid string `json:"recommend_uid"` //推荐人uid +} diff --git a/rule/public_platoon_relate_commission.go b/rule/public_platoon_relate_commission.go index d78e8f3..43796e1 100644 --- a/rule/public_platoon_relate_commission.go +++ b/rule/public_platoon_relate_commission.go @@ -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 + } + } +}