diff --git a/rule/public_platoon_relate_commission.go b/rule/public_platoon_relate_commission.go index b776559..a96f9ea 100644 --- a/rule/public_platoon_relate_commission.go +++ b/rule/public_platoon_relate_commission.go @@ -9,6 +9,7 @@ import ( zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" "errors" "math" + "math/rand" "strings" "time" "xorm.io/xorm" @@ -439,3 +440,122 @@ func DealCommonWealthPunish(engine *xorm.Engine, uid int, reason string) (err er err = session.Commit() return } + +// JudgeUserBelowLevel 判断当前用户下几级排满 +func JudgeUserBelowLevel(engine *xorm.Engine, uid int) (level int, err error) { + //1、查找 `user_public_platoon_setting` 基础设置 + userPublicPlatoonSetting, err := db.UserPublicPlatoonSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) + if err != nil { + return + } + + //2、查找当前用户所在的公排记录 + var m model.UserPublicPlatoonRelation + has, err := engine.Where("uid =?", uid).Get(&m) + if err != nil { + return + } + if !has { + err = errors.New("未查询到当前用户的公排记录") + return + } + + //3、查找当前用户下最下级的层数 + var son model.UserPublicPlatoonRelation + hasSon, err := engine.Where("father_uid LIKE ?", "%-"+zhios_order_relate_utils.IntToStr(m.Id)). + Or("father_uid LIKE ?", zhios_order_relate_utils.IntToStr(m.Id)+"-%"). + Or("father_uid LIKE ?", "%-"+zhios_order_relate_utils.IntToStr(m.Id)+"-%"). + Or("father_uid = ?", m.Id). + OrderBy("id Desc").Get(&son) + if err != nil { + return + } + if !hasSon { + return + } + + level = son.Level - 1 + levelEndPosition := getLevelForLastPosition(son.Level, userPublicPlatoonSetting.SeveralTimes) + if son.Position == levelEndPosition { + level++ + } + return +} + +func FindRandUser(engine *xorm.Engine, nums int) (resp []int64, err error) { + //1、查找最小、最大 位置的公排位置 + var minM, maxM model.UserPublicPlatoonRelation + has, err := engine.Desc("id").Get(&minM) + if err != nil { + return + } + if !has { + err = errors.New("未查询到最小公排记录") + return + } + + has, err = engine.Desc("id").Get(&maxM) + if err != nil { + return + } + if !has { + err = errors.New("未查询到最大公排记录") + return + } + var m model.UserPublicPlatoonRelation + count, err := engine.Count(&m) + if err != nil { + return + } + if int(count) < nums { + nums = int(count) + } + + var uniqueMap = map[int64]bool{} + var j = 0 + for { + ids := randSeedInt(int64(minM.Id), int64(maxM.Id), nums-len(resp), uniqueMap) + var list []model.UserPublicPlatoonRelation + if err1 := engine.In("id", ids). + Find(&list); err1 != nil { + return nil, zhios_order_relate_logx.Warn(err1) + } + + for _, v := range list { + resp = append(resp, int64(v.Uid)) + uniqueMap[int64(v.Id)] = true + } + + if len(resp) == nums { + break + } + + if j == 10 { + //TODO::避免出现死循环 + break + } + j++ + } + + return +} + +func randSeedInt(start, end int64, nums int, uniqueMap map[int64]bool) (resp []int64) { + rand.Seed(time.Now().UnixNano()) + for { + result := rand.Int63n(end) + start + if !uniqueMap[result] { + resp = append(resp, result) + uniqueMap[result] = true + break + } + } + + if len(resp) < nums { + randSeedInt(start, end, nums, uniqueMap) + } + return +}