From 214797d1f82f7a66ad41e7e6443c7a34247fd89b Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Tue, 12 Jul 2022 16:42:26 +0800 Subject: [PATCH] add Reverse: for v1.2.3 --- db/model/user_public_platoon_relation.go | 1 + rule/public_platoon_relate_commission.go | 71 +++++++++++++++++++++--- 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/db/model/user_public_platoon_relation.go b/db/model/user_public_platoon_relation.go index a93d666..62fe1a4 100644 --- a/db/model/user_public_platoon_relation.go +++ b/db/model/user_public_platoon_relation.go @@ -18,4 +18,5 @@ type UserPublicPlatoonRelation struct { WaitForSettlementDate string `json:"wait_for_settlement_date" xorm:"not null default '' comment('待结算时间(0000-00)') CHAR(50)"` CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` UpdateAt time.Time `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` + NewestPublicPlatoon int `json:"newest_public_platoon" xorm:"not null default 0 comment('最新公排') TINYINT(1)"` } diff --git a/rule/public_platoon_relate_commission.go b/rule/public_platoon_relate_commission.go index a040605..333a2a5 100644 --- a/rule/public_platoon_relate_commission.go +++ b/rule/public_platoon_relate_commission.go @@ -117,7 +117,7 @@ func BatchGetPublicPlatoonRelateCommissionByOrder(engine *xorm.Engine, masterId } // AddPublicPlatoonRelateCommission 新增公排用户关系记录 -func AddPublicPlatoonRelateCommission(engine *xorm.Engine, masterId string, AddPublicPlatoonRelateCommissionReqList []*md.AddPublicPlatoonRelateCommissionReq) (map[string]*model.UserPublicPlatoonRelation, error) { +func AddPublicPlatoonRelateCommission(engine *xorm.Engine, 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{}{ @@ -165,20 +165,49 @@ func AddPublicPlatoonRelateCommission(engine *xorm.Engine, masterId string, AddP 1: 每个等级的起始值(1+5^0+5^1+5^2+...+5^x-2), 每个等级的结束值(`5^0+5^1+5^2+...+5^x-1) 2: 根据position查找父级position { (position-1)/5 } 3: 根据position查找等级level {position-5^0-5^1-5^2-...-5^x 是否 <0 ? => x+1 } + 4: 根据最新公排位置`newest_public_platoon` 逆向推导 position {levelFirstPosition + (position-1)%5} */ 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 { + //1、 查找当前 user_public_platoon_relation 中 `newest_public_platoon` 的位置 + m, err := db.UserPublicPlatoonRelationGetOneByParams(engine, map[string]interface{}{ + "key": "newest_public_platoon", + "value": 1, + }) + if err != nil { return userPublicPlatoonRelation, err } + if m == nil { + return userPublicPlatoonRelation, errors.New("查询 user_public_platoon_relation 记录失败") + } + + //TODO::判断 `newest_public_platoon + userPublicPlatoonSetting.SeveralTimes(几乘)` OR `newest_public_platoon + 1` + var position, position1, position2 int + position1 = m.Position + 1 + position2 = m.Position + userPublicPlatoonSetting.SeveralTimes + var level, level1, level2 float64 + makeSearchLevel(&position1, float64(userPublicPlatoonSetting.SeveralTimes), &level1) + makeSearchLevel(&position2, float64(userPublicPlatoonSetting.SeveralTimes), &level2) + level = level1 + 1 //TODO::根据公式需要 + 1 + if level > 2 { + if level1 != level2 { + if position1 == getLevelForLastPosition(int(level), userPublicPlatoonSetting.SeveralTimes) { + position = position1 + } else { + position = reverseDeductionPosition(m.Position, getLevelForFirstPosition(int(level), userPublicPlatoonSetting.SeveralTimes), userPublicPlatoonSetting.SeveralTimes) + } + } else { + if position1 == getLevelForFirstPosition(int(level), userPublicPlatoonSetting.SeveralTimes) { + position = position1 + } else { + position = position2 + } + } + } else { + position = position1 + } - 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 @@ -214,13 +243,37 @@ func publicPlatoon(engine *xorm.Engine, uid, recommendUid int, userPublicPlatoon userPublicPlatoonRelation.JoinAt = now userPublicPlatoonRelation.CreateAt = now userPublicPlatoonRelation.UpdateAt = now - _, err := db.UserPublicPlatoonRelationInsert(engine, &userPublicPlatoonRelation) + _, err = db.UserPublicPlatoonRelationInsert(engine, &userPublicPlatoonRelation) if err != nil { return model.UserPublicPlatoonRelation{}, err } return userPublicPlatoonRelation, nil } +// reverseDeductionPosition 逆向推导 position +func reverseDeductionPosition(calcPosition, levelFirstPosition, severalTimes int) (position int) { + remainder := (calcPosition - 1) % severalTimes + position = levelFirstPosition + remainder + return +} + +// getLevelForFirstPosition 返回当前等级的起始值 +func getLevelForFirstPosition(level, severalTimes int) (position int) { + position = position + 1 + for n := 1; n <= (level - 2); n++ { + position += int(math.Pow(float64(severalTimes), float64(n))) + } + return +} + +// getLevelForLastPosition 每个等级的结束值 +func getLevelForLastPosition(level, severalTimes int) (position int) { + for n := 1; n <= (level - 1); n++ { + position += int(math.Pow(float64(severalTimes), float64(n))) + } + return +} + //递归查找等级 func makeSearchLevel(position *int, rows float64, times *float64) (level int) { difference := *position - int(math.Pow(rows, *times))