|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910 |
- package rule
-
- import (
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
- md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/cache"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx"
- "errors"
- "fmt"
- "math"
- "strconv"
- "strings"
- "time"
- "xorm.io/xorm"
- )
-
- // AddPublicPlatoonUserRelationCommission 新增公排用户关系记录
- func AddPublicPlatoonUserRelationCommission(engine *xorm.Engine, AddPublicPlatoonUserRelationCommissionReqList []*md2.AddPublicPlatoonUserRelationCommissionReq) (map[string]*model.PublicPlatoonUserRelation, error) {
- var resp = map[string]*model.PublicPlatoonUserRelation{}
- //查找 `public_platoon_basic_setting` 基础设置
- publicPlatoonBasicSettingDb := implement.NewPublicPlatoonBasicSettingDb(engine)
- publicPlatoonBasicSetting, err := publicPlatoonBasicSettingDb.PublicPlatoonBasicSettingGetOneByParams(map[string]interface{}{
- "key": "is_open",
- "value": 1,
- })
- if err != nil {
- return nil, err
- }
- if publicPlatoonBasicSetting == nil {
- return nil, errors.New("公排制度未开启")
- }
-
- publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
- for _, param := range AddPublicPlatoonUserRelationCommissionReqList {
- isHasPublicPlatoonUserRelation, err11 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
- "key": "uid",
- "value": param.Uid,
- })
- if err11 != nil {
- return nil, err11
- }
- if isHasPublicPlatoonUserRelation != nil {
- return nil, errors.New("当前用户已加入公排")
- }
-
- //1、判断是否有推荐人
- if param.RecommendUid != "" {
- //2、有推荐人
- //判断是否有uid为-1 (代表等待新用户填充) 的记录
- PublicPlatoonUserRelation, err1 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByPid(param.RecommendUid, map[string]interface{}{
- "key": "uid",
- "value": -1,
- })
- if err1 != nil {
- return nil, err1
- }
- if PublicPlatoonUserRelation != nil {
- now := time.Now()
- oldUid := PublicPlatoonUserRelation.Uid
- PublicPlatoonUserRelation.Uid = egg_system_rules.StrToInt64(param.Uid)
- PublicPlatoonUserRelation.RecommendUid = egg_system_rules.StrToInt64(param.RecommendUid)
- PublicPlatoonUserRelation.CreateAt = now.Format("2006-01-02 15:04:05")
- PublicPlatoonUserRelation.UpdateAt = now.Format("2006-01-02 15:04:05")
- PublicPlatoonUserRelation.JoinAt = now.Format("2006-01-02 15:04:05")
- updateAffected, err2 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationUpdate(engine.NewSession(), PublicPlatoonUserRelation.Id, PublicPlatoonUserRelation)
- if err2 != nil {
- return nil, err2
- }
- if updateAffected == 0 {
- err = errors.New("更新 public_platoon_user_relation 记录失败")
- return nil, err
- } else {
- for n := 1; n <= 9; n++ {
- str := "father_uid" + strconv.Itoa(n)
- sql := fmt.Sprintf("UPDATE `public_platoon_user_relation` SET %s=%s where %s=%s", str, param.Uid, str, egg_system_rules.Int64ToStr(oldUid))
- fmt.Println(">>>>>>>sql>>>>>>>", sql)
- _, err = engine.Exec(sql)
- if err != nil {
- return nil, err
- }
- }
- resp[param.Uid] = PublicPlatoonUserRelation
- }
- continue
- }
- res, err := PublicPlatoon(engine, egg_system_rules.StrToInt64(param.Uid), egg_system_rules.StrToInt64(param.RecommendUid), *publicPlatoonBasicSetting)
- if err != nil {
- return nil, err
- }
- resp[param.Uid] = &res
- }
- }
- return resp, nil
- }
-
- /*
- 公排方法
-
- TODO 相关公式:
- 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: 根据最新自增`id` 逆向推导 position {levelFirstPosition + (position-1)%5}
- */
- func PublicPlatoon(engine *xorm.Engine, uid, recommendUid int64, PublicPlatoonBasicSetting model.PublicPlatoonBasicSetting) (model.PublicPlatoonUserRelation, error) {
- var standardPublicPlatoonRelation md.StandardPublicPlatoonRelation
- var PublicPlatoonUserRelation model.PublicPlatoonUserRelation
- var fatherUid int64
- //1、 查找当前 public_platoon_user_relation 中 recommendUid 的记录
- publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
- m, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
- "key": "uid",
- "value": recommendUid,
- })
- if err != nil {
- return model.PublicPlatoonUserRelation{}, err
- }
-
- //2、 查找当前 public_platoon_user_relation 中 recommendUid 所有的子记录
- PublicPlatoonUserRelations, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationFindByPid(m.Uid, "father_uid1", "position1")
- if err != nil {
- return model.PublicPlatoonUserRelation{}, err
- }
-
- if len(PublicPlatoonUserRelations) == 0 {
- fatherUid = m.Uid
-
- //证明是第一个
- standardPublicPlatoonRelation.FatherUid1 = m.Uid
- standardPublicPlatoonRelation.FatherUid2 = m.FatherUid1
- standardPublicPlatoonRelation.FatherUid3 = m.FatherUid2
- standardPublicPlatoonRelation.FatherUid4 = m.FatherUid3
- standardPublicPlatoonRelation.FatherUid5 = m.FatherUid4
- standardPublicPlatoonRelation.FatherUid6 = m.FatherUid5
- standardPublicPlatoonRelation.FatherUid7 = m.FatherUid6
- standardPublicPlatoonRelation.FatherUid8 = m.FatherUid7
- standardPublicPlatoonRelation.FatherUid9 = m.FatherUid8
-
- standardPublicPlatoonRelation.Pid1 = m.Position
- standardPublicPlatoonRelation.Pid2 = m.Position1
- standardPublicPlatoonRelation.Pid3 = m.Position2
- standardPublicPlatoonRelation.Pid4 = m.Position3
- standardPublicPlatoonRelation.Pid5 = m.Position4
- standardPublicPlatoonRelation.Pid6 = m.Position5
- standardPublicPlatoonRelation.Pid7 = m.Position6
- standardPublicPlatoonRelation.Pid8 = m.Position7
- standardPublicPlatoonRelation.Pid9 = m.Position8
-
- standardPublicPlatoonRelation.Position1 = SearchPositionFirstSonPosition(m.Position, int64(PublicPlatoonBasicSetting.SeveralTimes))
- standardPublicPlatoonRelation.Position2 = ReverseDeductionPositionByFather(m.Position1, int64(m.Level1), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
- standardPublicPlatoonRelation.Position3 = ReverseDeductionPositionByFather(m.Position2, int64(m.Level2), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
- standardPublicPlatoonRelation.Position4 = ReverseDeductionPositionByFather(m.Position3, int64(m.Level3), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
- standardPublicPlatoonRelation.Position5 = ReverseDeductionPositionByFather(m.Position4, int64(m.Level4), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
- standardPublicPlatoonRelation.Position6 = ReverseDeductionPositionByFather(m.Position5, int64(m.Level5), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
- standardPublicPlatoonRelation.Position7 = ReverseDeductionPositionByFather(m.Position6, int64(m.Level6), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
- standardPublicPlatoonRelation.Position8 = ReverseDeductionPositionByFather(m.Position7, int64(m.Level7), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
- standardPublicPlatoonRelation.Position9 = ReverseDeductionPositionByFather(m.Position8, int64(m.Level8), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
-
- if m.Level1 == 0 {
- m.Level1--
- }
- if m.Level2 == 0 {
- m.Level2--
- }
- if m.Level3 == 0 {
- m.Level3--
- }
- if m.Level4 == 0 {
- m.Level4--
- }
- if m.Level5 == 0 {
- m.Level5--
- }
- if m.Level6 == 0 {
- m.Level6--
- }
- if m.Level7 == 0 {
- m.Level7--
- }
- if m.Level8 == 0 {
- m.Level8--
- }
- if m.Level9 == 0 {
- m.Level9--
- }
-
- standardPublicPlatoonRelation.Level1 = m.Level + 1
- standardPublicPlatoonRelation.Level2 = m.Level1 + 1
- standardPublicPlatoonRelation.Level3 = m.Level2 + 1
- standardPublicPlatoonRelation.Level4 = m.Level3 + 1
- standardPublicPlatoonRelation.Level5 = m.Level4 + 1
- standardPublicPlatoonRelation.Level6 = m.Level5 + 1
- standardPublicPlatoonRelation.Level7 = m.Level6 + 1
- standardPublicPlatoonRelation.Level8 = m.Level7 + 1
- standardPublicPlatoonRelation.Level9 = m.Level8 + 1
- standardPublicPlatoonRelation.LevelTotal = m.LevelTotal + 1
- } else {
- //TODO::判断直属下级是否排满
- if len(PublicPlatoonUserRelations) >= PublicPlatoonBasicSetting.SeveralTimes {
- var fatherLevel float64
- makeSearchLevel(&m.Position, float64(PublicPlatoonBasicSetting.SeveralTimes), &fatherLevel)
- var times = 0
- data, father, err := FindSuitablePosition(engine, &m.Position, float64(PublicPlatoonBasicSetting.SeveralTimes), m.Uid, int64(fatherLevel), ×)
- if err != nil {
- return model.PublicPlatoonUserRelation{}, err
- }
- fatherUid = father
- standardPublicPlatoonRelation = data
- } else {
- fatherUid = m.Uid
-
- standardPublicPlatoonRelation.FatherUid1 = PublicPlatoonUserRelations[0].FatherUid1
- standardPublicPlatoonRelation.FatherUid2 = PublicPlatoonUserRelations[0].FatherUid2
- standardPublicPlatoonRelation.FatherUid3 = PublicPlatoonUserRelations[0].FatherUid3
- standardPublicPlatoonRelation.FatherUid4 = PublicPlatoonUserRelations[0].FatherUid4
- standardPublicPlatoonRelation.FatherUid5 = PublicPlatoonUserRelations[0].FatherUid5
- standardPublicPlatoonRelation.FatherUid6 = PublicPlatoonUserRelations[0].FatherUid6
- standardPublicPlatoonRelation.FatherUid7 = PublicPlatoonUserRelations[0].FatherUid7
- standardPublicPlatoonRelation.FatherUid8 = PublicPlatoonUserRelations[0].FatherUid8
- standardPublicPlatoonRelation.FatherUid9 = PublicPlatoonUserRelations[0].FatherUid9
-
- standardPublicPlatoonRelation.Pid1 = PublicPlatoonUserRelations[0].Pid1
- standardPublicPlatoonRelation.Pid2 = PublicPlatoonUserRelations[0].Pid2
- standardPublicPlatoonRelation.Pid3 = PublicPlatoonUserRelations[0].Pid3
- standardPublicPlatoonRelation.Pid4 = PublicPlatoonUserRelations[0].Pid4
- standardPublicPlatoonRelation.Pid5 = PublicPlatoonUserRelations[0].Pid5
- standardPublicPlatoonRelation.Pid6 = PublicPlatoonUserRelations[0].Pid6
- standardPublicPlatoonRelation.Pid7 = PublicPlatoonUserRelations[0].Pid7
- standardPublicPlatoonRelation.Pid8 = PublicPlatoonUserRelations[0].Pid8
- standardPublicPlatoonRelation.Pid9 = PublicPlatoonUserRelations[0].Pid9
-
- standardPublicPlatoonRelation.Position1 = PublicPlatoonUserRelations[0].Position1 + 1
- if PublicPlatoonUserRelations[0].Position2 != 0 {
- standardPublicPlatoonRelation.Position2 = PublicPlatoonUserRelations[0].Position2 + 1
- }
- if PublicPlatoonUserRelations[0].Position3 != 0 {
- standardPublicPlatoonRelation.Position3 = PublicPlatoonUserRelations[0].Position3 + 1
- }
- if PublicPlatoonUserRelations[0].Position4 != 0 {
- standardPublicPlatoonRelation.Position4 = PublicPlatoonUserRelations[0].Position4 + 1
- }
- if PublicPlatoonUserRelations[0].Position5 != 0 {
- standardPublicPlatoonRelation.Position5 = PublicPlatoonUserRelations[0].Position5 + 1
- }
- if PublicPlatoonUserRelations[0].Position6 != 0 {
- standardPublicPlatoonRelation.Position6 = PublicPlatoonUserRelations[0].Position6 + 1
- }
- if PublicPlatoonUserRelations[0].Position7 != 0 {
- standardPublicPlatoonRelation.Position7 = PublicPlatoonUserRelations[0].Position7 + 1
- }
- if PublicPlatoonUserRelations[0].Position8 != 0 {
- standardPublicPlatoonRelation.Position8 = PublicPlatoonUserRelations[0].Position8 + 1
- }
- if PublicPlatoonUserRelations[0].Position9 != 0 {
- standardPublicPlatoonRelation.Position9 = PublicPlatoonUserRelations[0].Position9 + 1
- }
-
- standardPublicPlatoonRelation.Level1 = PublicPlatoonUserRelations[0].Level1
- standardPublicPlatoonRelation.Level2 = PublicPlatoonUserRelations[0].Level2
- standardPublicPlatoonRelation.Level3 = PublicPlatoonUserRelations[0].Level3
- standardPublicPlatoonRelation.Level4 = PublicPlatoonUserRelations[0].Level4
- standardPublicPlatoonRelation.Level5 = PublicPlatoonUserRelations[0].Level5
- standardPublicPlatoonRelation.Level6 = PublicPlatoonUserRelations[0].Level6
- standardPublicPlatoonRelation.Level7 = PublicPlatoonUserRelations[0].Level7
- standardPublicPlatoonRelation.Level8 = PublicPlatoonUserRelations[0].Level8
- standardPublicPlatoonRelation.Level9 = PublicPlatoonUserRelations[0].Level9
- standardPublicPlatoonRelation.LevelTotal = PublicPlatoonUserRelations[0].LevelTotal
-
- }
-
- }
-
- //2、查找对应pid的 public_platoon_user_relation 记录
- var m1 model.PublicPlatoonUserRelation
- if has, err := engine.Where("uid=?", fatherUid).Get(&m1); err != nil || has == false {
- return PublicPlatoonUserRelation, err
- }
- var fatherUidStr string
- if m1.FatherUid == "" {
- //TODO::顶级
- fatherUidStr = egg_system_rules.Int64ToStr(m1.Id)
- } else {
- fatherUids := strings.Split(m1.FatherUid, "-")
- if len(fatherUids) >= PublicPlatoonBasicSetting.SeveralRows {
- fatherUidStr = egg_system_rules.Int64ToStr(m1.Id) + "-" + strings.Join(fatherUids[0:PublicPlatoonBasicSetting.SeveralRows:len(fatherUids)], "-")
- } else {
- fatherUidStr = egg_system_rules.Int64ToStr(m1.Id) + "-" + m1.FatherUid
- }
- }
-
- // 3、插入 public_platoon_user_relation 记录
- now := time.Now()
- PublicPlatoonUserRelation.Uid = uid
- PublicPlatoonUserRelation.FatherUid = fatherUidStr
-
- PublicPlatoonUserRelation.FatherUid1 = standardPublicPlatoonRelation.FatherUid1
- PublicPlatoonUserRelation.FatherUid2 = standardPublicPlatoonRelation.FatherUid2
- PublicPlatoonUserRelation.FatherUid3 = standardPublicPlatoonRelation.FatherUid3
- PublicPlatoonUserRelation.FatherUid4 = standardPublicPlatoonRelation.FatherUid4
- PublicPlatoonUserRelation.FatherUid5 = standardPublicPlatoonRelation.FatherUid5
- PublicPlatoonUserRelation.FatherUid6 = standardPublicPlatoonRelation.FatherUid6
- PublicPlatoonUserRelation.FatherUid7 = standardPublicPlatoonRelation.FatherUid7
- PublicPlatoonUserRelation.FatherUid8 = standardPublicPlatoonRelation.FatherUid8
- PublicPlatoonUserRelation.FatherUid9 = standardPublicPlatoonRelation.FatherUid9
- PublicPlatoonUserRelation.Pid1 = standardPublicPlatoonRelation.Pid1
- PublicPlatoonUserRelation.Pid2 = standardPublicPlatoonRelation.Pid2
- PublicPlatoonUserRelation.Pid3 = standardPublicPlatoonRelation.Pid3
- PublicPlatoonUserRelation.Pid4 = standardPublicPlatoonRelation.Pid4
- PublicPlatoonUserRelation.Pid5 = standardPublicPlatoonRelation.Pid5
- PublicPlatoonUserRelation.Pid6 = standardPublicPlatoonRelation.Pid6
- PublicPlatoonUserRelation.Pid7 = standardPublicPlatoonRelation.Pid7
- PublicPlatoonUserRelation.Pid8 = standardPublicPlatoonRelation.Pid8
- PublicPlatoonUserRelation.Pid9 = standardPublicPlatoonRelation.Pid9
- PublicPlatoonUserRelation.Position = 1
- PublicPlatoonUserRelation.Position1 = standardPublicPlatoonRelation.Position1
- PublicPlatoonUserRelation.Position2 = standardPublicPlatoonRelation.Position2
- PublicPlatoonUserRelation.Position3 = standardPublicPlatoonRelation.Position3
- PublicPlatoonUserRelation.Position4 = standardPublicPlatoonRelation.Position4
- PublicPlatoonUserRelation.Position5 = standardPublicPlatoonRelation.Position5
- PublicPlatoonUserRelation.Position6 = standardPublicPlatoonRelation.Position6
- PublicPlatoonUserRelation.Position7 = standardPublicPlatoonRelation.Position7
- PublicPlatoonUserRelation.Position8 = standardPublicPlatoonRelation.Position8
- PublicPlatoonUserRelation.Position9 = standardPublicPlatoonRelation.Position9
- PublicPlatoonUserRelation.Level = 1
- PublicPlatoonUserRelation.Level1 = standardPublicPlatoonRelation.Level1
- PublicPlatoonUserRelation.Level2 = standardPublicPlatoonRelation.Level2
- PublicPlatoonUserRelation.Level3 = standardPublicPlatoonRelation.Level3
- PublicPlatoonUserRelation.Level4 = standardPublicPlatoonRelation.Level4
- PublicPlatoonUserRelation.Level5 = standardPublicPlatoonRelation.Level5
- PublicPlatoonUserRelation.Level6 = standardPublicPlatoonRelation.Level6
- PublicPlatoonUserRelation.Level7 = standardPublicPlatoonRelation.Level7
- PublicPlatoonUserRelation.Level8 = standardPublicPlatoonRelation.Level8
- PublicPlatoonUserRelation.Level9 = standardPublicPlatoonRelation.Level9
- PublicPlatoonUserRelation.LevelTotal = standardPublicPlatoonRelation.LevelTotal
- PublicPlatoonUserRelation.RecommendUid = recommendUid
- PublicPlatoonUserRelation.JoinAt = now.Format("2006-01-02 15:04:05")
- PublicPlatoonUserRelation.CreateAt = now.Format("2006-01-02 15:04:05")
- PublicPlatoonUserRelation.UpdateAt = now.Format("2006-01-02 15:04:05")
- PublicPlatoonUserRelation.HasSonNum = 0
- _, err = publicPlatoonUserRelationDb.PublicPlatoonUserRelationInsert(&PublicPlatoonUserRelation)
- if err != nil {
- return model.PublicPlatoonUserRelation{}, err
- }
-
- // 4、改变直属父级的 has_son_num 数量
- update, err := engine.Where("uid = ?", fatherUid).SetExpr("has_son_num", "has_son_num + 1").Update(new(model.PublicPlatoonUserRelation))
- if err != nil {
- return PublicPlatoonUserRelation, err
- }
- if update == 0 {
- return PublicPlatoonUserRelation, errors.New("更新has_son_num数据出错")
- }
- return PublicPlatoonUserRelation, nil
- }
-
- // ReverseDeductionPosition 逆向推导 position
- func ReverseDeductionPosition(calcPosition, levelFirstPosition, severalTimes int) (position int) {
- remainder := (calcPosition - 1) % severalTimes
- position = levelFirstPosition + remainder
- return
- }
-
- func ReverseDeductionPositionByFather(fatherPosition, level, severalTimes, remainder int64) (position int64) {
- if fatherPosition == 0 {
- position = 0
- return
- }
- firstPosition := GetLevelForFirstPosition(level, severalTimes)
- endPosition := firstPosition + int64(math.Pow(float64(severalTimes), float64(level-1))) - 1
- position = fatherPosition + (fatherPosition-firstPosition)*severalTimes + (endPosition - fatherPosition) + 1
- return
- }
-
- func FindSuitablePosition(engine *xorm.Engine, position *int64, severalTimes float64, fuid, fatherLevel int64, times *int) (standardPublicPlatoonRelation md.StandardPublicPlatoonRelation, fatherUid int64, err error) {
- *times++
- positionName := "position" + egg_system_rules.IntToStr(*times)
- fatherName := "father_uid" + egg_system_rules.IntToStr(*times)
- firstSonPosition := SearchPositionFirstSonPosition(*position, int64(severalTimes))
- var tempLevel float64
- makeSearchLevel(&firstSonPosition, severalTimes, &tempLevel)
- firstSonLevel := int64(tempLevel)
- endSonPosition := firstSonPosition + int64(math.Pow(severalTimes, float64(firstSonLevel-fatherLevel))) - 1
- publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
- list, _, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationFindCountByPosition(fuid, fatherName, positionName, int64(firstSonPosition), int64(endSonPosition))
- if err != nil {
- return standardPublicPlatoonRelation, fatherUid, err
- }
-
- if list[0].HasSonNum == int(severalTimes) {
- //TODO::则证明该层直属下级无空位
- position = &firstSonPosition
- return FindSuitablePosition(engine, position, severalTimes, fuid, fatherLevel, times)
- } else if list[0].HasSonNum == 0 {
- fatherUid = list[0].Uid
-
- //TODO::新的一层
- standardPublicPlatoonRelation.FatherUid1 = list[0].Uid
- standardPublicPlatoonRelation.FatherUid2 = list[0].FatherUid1
- standardPublicPlatoonRelation.FatherUid3 = list[0].FatherUid2
- standardPublicPlatoonRelation.FatherUid4 = list[0].FatherUid3
- standardPublicPlatoonRelation.FatherUid5 = list[0].FatherUid4
- standardPublicPlatoonRelation.FatherUid6 = list[0].FatherUid5
- standardPublicPlatoonRelation.FatherUid7 = list[0].FatherUid6
- standardPublicPlatoonRelation.FatherUid8 = list[0].FatherUid7
- standardPublicPlatoonRelation.FatherUid9 = list[0].FatherUid8
-
- standardPublicPlatoonRelation.Pid1 = list[0].Position
- standardPublicPlatoonRelation.Pid2 = list[0].Position1
- standardPublicPlatoonRelation.Pid3 = list[0].Position2
- standardPublicPlatoonRelation.Pid4 = list[0].Position3
- standardPublicPlatoonRelation.Pid5 = list[0].Position4
- standardPublicPlatoonRelation.Pid6 = list[0].Position5
- standardPublicPlatoonRelation.Pid7 = list[0].Position6
- standardPublicPlatoonRelation.Pid8 = list[0].Position7
- standardPublicPlatoonRelation.Pid9 = list[0].Position8
-
- standardPublicPlatoonRelation.Position1 = SearchPositionFirstSonPosition(list[0].Position, int64(severalTimes))
- standardPublicPlatoonRelation.Position2 = ReverseDeductionPositionByFather(list[0].Position1, int64(list[0].Level1), int64(severalTimes), 0)
- standardPublicPlatoonRelation.Position3 = ReverseDeductionPositionByFather(list[0].Position2, int64(list[0].Level2), int64(severalTimes), 0)
- standardPublicPlatoonRelation.Position4 = ReverseDeductionPositionByFather(list[0].Position3, int64(list[0].Level3), int64(severalTimes), 0)
- standardPublicPlatoonRelation.Position5 = ReverseDeductionPositionByFather(list[0].Position4, int64(list[0].Level4), int64(severalTimes), 0)
- standardPublicPlatoonRelation.Position6 = ReverseDeductionPositionByFather(list[0].Position5, int64(list[0].Level5), int64(severalTimes), 0)
- standardPublicPlatoonRelation.Position7 = ReverseDeductionPositionByFather(list[0].Position6, int64(list[0].Level6), int64(severalTimes), 0)
- standardPublicPlatoonRelation.Position8 = ReverseDeductionPositionByFather(list[0].Position7, int64(list[0].Level7), int64(severalTimes), 0)
- standardPublicPlatoonRelation.Position9 = ReverseDeductionPositionByFather(list[0].Position8, int64(list[0].Level8), int64(severalTimes), 0)
-
- if list[0].Level1 == 0 {
- list[0].Level1--
- }
- if list[0].Level2 == 0 {
- list[0].Level2--
- }
- if list[0].Level3 == 0 {
- list[0].Level3--
- }
- if list[0].Level4 == 0 {
- list[0].Level4--
- }
- if list[0].Level5 == 0 {
- list[0].Level5--
- }
- if list[0].Level6 == 0 {
- list[0].Level6--
- }
- if list[0].Level7 == 0 {
- list[0].Level7--
- }
- if list[0].Level8 == 0 {
- list[0].Level8--
- }
- if list[0].Level9 == 0 {
- list[0].Level9--
- }
- standardPublicPlatoonRelation.Level1 = list[0].Level + 1
- standardPublicPlatoonRelation.Level2 = list[0].Level1 + 1
- standardPublicPlatoonRelation.Level3 = list[0].Level2 + 1
- standardPublicPlatoonRelation.Level4 = list[0].Level3 + 1
- standardPublicPlatoonRelation.Level5 = list[0].Level4 + 1
- standardPublicPlatoonRelation.Level6 = list[0].Level5 + 1
- standardPublicPlatoonRelation.Level7 = list[0].Level6 + 1
- standardPublicPlatoonRelation.Level8 = list[0].Level7 + 1
- standardPublicPlatoonRelation.Level9 = list[0].Level8 + 1
- standardPublicPlatoonRelation.LevelTotal = list[0].LevelTotal + 1
- } else {
- fatherUid = list[0].Uid
-
- _, _, _, _, getPositionName, _, _, _ := GetPosition(list[0], *times+1)
- //PublicPlatoonUserRelations, err := db.PublicPlatoonUserRelationFindByPid(engine, m.Position, m.Uid, "pid1", "father_uid1", "position1")
-
- PublicPlatoonUserRelations, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationFindByPid(list[0].Uid, "father_uid1", getPositionName)
-
- if err != nil {
- return standardPublicPlatoonRelation, fatherUid, err
- }
-
- if PublicPlatoonUserRelations[0].Position2 == 0 {
- PublicPlatoonUserRelations[0].Position2 = -1
- }
- if PublicPlatoonUserRelations[0].Position3 == 0 {
- standardPublicPlatoonRelation.Position3 = -1
- }
- if PublicPlatoonUserRelations[0].Position4 == 0 {
- standardPublicPlatoonRelation.Position4 = -1
- }
- if PublicPlatoonUserRelations[0].Position5 == 0 {
- standardPublicPlatoonRelation.Position5 = -1
- }
- if PublicPlatoonUserRelations[0].Position6 == 0 {
- standardPublicPlatoonRelation.Position6 = -1
- }
- if PublicPlatoonUserRelations[0].Position7 == 0 {
- standardPublicPlatoonRelation.Position7 = -1
- }
- if PublicPlatoonUserRelations[0].Position8 == 0 {
- standardPublicPlatoonRelation.Position8 = -1
- }
- if PublicPlatoonUserRelations[0].Position9 == 0 {
- standardPublicPlatoonRelation.Position9 = -1
- }
-
- standardPublicPlatoonRelation = md.StandardPublicPlatoonRelation{
- FatherUid1: PublicPlatoonUserRelations[0].FatherUid1,
- FatherUid2: PublicPlatoonUserRelations[0].FatherUid2,
- FatherUid3: PublicPlatoonUserRelations[0].FatherUid3,
- FatherUid4: PublicPlatoonUserRelations[0].FatherUid4,
- FatherUid5: PublicPlatoonUserRelations[0].FatherUid5,
- FatherUid6: PublicPlatoonUserRelations[0].FatherUid6,
- FatherUid7: PublicPlatoonUserRelations[0].FatherUid7,
- FatherUid8: PublicPlatoonUserRelations[0].FatherUid8,
- FatherUid9: PublicPlatoonUserRelations[0].FatherUid9,
- Pid1: PublicPlatoonUserRelations[0].Pid1,
- Pid2: PublicPlatoonUserRelations[0].Pid2,
- Pid3: PublicPlatoonUserRelations[0].Pid3,
- Pid4: PublicPlatoonUserRelations[0].Pid4,
- Pid5: PublicPlatoonUserRelations[0].Pid5,
- Pid6: PublicPlatoonUserRelations[0].Pid6,
- Pid7: PublicPlatoonUserRelations[0].Pid7,
- Pid8: PublicPlatoonUserRelations[0].Pid8,
- Pid9: PublicPlatoonUserRelations[0].Pid9,
- Position1: PublicPlatoonUserRelations[0].Position1 + 1,
- Position2: PublicPlatoonUserRelations[0].Position2 + 1,
- Position3: PublicPlatoonUserRelations[0].Position3 + 1,
- Position4: PublicPlatoonUserRelations[0].Position4 + 1,
- Position5: PublicPlatoonUserRelations[0].Position5 + 1,
- Position6: PublicPlatoonUserRelations[0].Position6 + 1,
- Position7: PublicPlatoonUserRelations[0].Position7 + 1,
- Position8: PublicPlatoonUserRelations[0].Position8 + 1,
- Position9: PublicPlatoonUserRelations[0].Position9 + 1,
- Level1: PublicPlatoonUserRelations[0].Level1,
- Level2: PublicPlatoonUserRelations[0].Level2,
- Level3: PublicPlatoonUserRelations[0].Level3,
- Level4: PublicPlatoonUserRelations[0].Level4,
- Level5: PublicPlatoonUserRelations[0].Level5,
- Level6: PublicPlatoonUserRelations[0].Level6,
- Level7: PublicPlatoonUserRelations[0].Level7,
- Level8: PublicPlatoonUserRelations[0].Level8,
- Level9: PublicPlatoonUserRelations[0].Level9,
- LevelTotal: PublicPlatoonUserRelations[0].LevelTotal,
- }
- return standardPublicPlatoonRelation, fatherUid, nil
- }
- return
- }
-
- func GetPosition(m model.PublicPlatoonUserRelation, times int) (position, pid int64, level int, fatherUid int64, positionName, pidName, levelName, fatherUidName string) {
- if times == 1 {
- position = m.Position1
- positionName = "position1"
- pid = m.Pid1
- pidName = "pid1"
- level = m.Level1
- levelName = "level1"
- fatherUid = m.FatherUid1
- fatherUidName = "father_uid1"
- }
- if times == 2 {
- position = m.Position2
- positionName = "position2"
- pid = m.Pid2
- pidName = "pid2"
- level = m.Level2
- levelName = "level2"
- fatherUid = m.FatherUid2
- fatherUidName = "father_uid2"
- }
- if times == 3 {
- position = m.Position3
- positionName = "position3"
- pid = m.Pid3
- pidName = "pid3"
- level = m.Level3
- levelName = "level3"
- fatherUid = m.FatherUid3
- fatherUidName = "father_uid3"
- }
- if times == 4 {
- position = m.Position4
- positionName = "position4"
- pid = m.Pid4
- pidName = "pid4"
- level = m.Level4
- levelName = "level4"
- fatherUid = m.FatherUid4
- fatherUidName = "father_uid4"
- }
- if times == 5 {
- position = m.Position5
- positionName = "position5"
- pid = m.Pid5
- pidName = "pid5"
- level = m.Level5
- levelName = "level5"
- fatherUid = m.FatherUid5
- fatherUidName = "father_uid5"
- }
- if times == 6 {
- position = m.Position6
- positionName = "position6"
- pid = m.Pid6
- pidName = "pid6"
- level = m.Level6
- levelName = "level6"
- fatherUid = m.FatherUid6
- fatherUidName = "father_uid6"
- }
- if times == 7 {
- position = m.Position7
- positionName = "position7"
- pid = m.Pid7
- pidName = "pid7"
- level = m.Level7
- levelName = "level7"
- fatherUid = m.FatherUid7
- fatherUidName = "father_uid7"
- }
- if times == 8 {
- position = m.Position8
- positionName = "position8"
- pid = m.Pid8
- pidName = "pid8"
- level = m.Level8
- levelName = "level8"
- fatherUid = m.FatherUid8
- fatherUidName = "father_uid8"
- }
- if times == 9 {
- position = m.Position9
- positionName = "position9"
- pid = m.Pid9
- pidName = "pid9"
- level = m.Level9
- levelName = "level9"
- fatherUid = m.FatherUid9
- fatherUidName = "father_uid9"
- }
-
- return
- }
-
- // GetLevelForFirstPosition 返回当前等级的起始值
- func GetLevelForFirstPosition(level, severalTimes int64) (position int64) {
- position = position + 1
- for n := int64(0); n <= (level - 2); n++ {
- position += int64(math.Pow(float64(severalTimes), float64(n)))
- }
- return
- }
-
- // 查找归属父级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
- }
- }
- }
-
- // 查找当前位置下的第一个子集位置
- func SearchPositionFirstSonPosition(position int64, times int64) (sonPosition int64) {
- sonPosition = (position * times) - (times - 2)
- return
- }
-
- // 查找当前位位置相当于父级的等级
- func SearchPositionSonForLevel(fatherUid string, fatherPosition int64) (level int) {
- fatherUids := strings.Split(fatherUid, "-")
- for key, item := range fatherUids {
- if item == egg_system_rules.Int64ToStr(fatherPosition) {
- level = key + 1
- break
- }
- }
- return
- }
-
- // FindWaitForDealUsers 查询待处理的用户
- func FindWaitForDealUsers(engine *xorm.Engine, page, pageSize int) (err error, resp []int64) {
- now := time.Now().Format("2006-01-02")
- publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
- lists, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationFindByParamsByPage(map[string]interface{}{
- "key": "wait_for_settlement_date",
- "value": now,
- }, page, pageSize)
- if err != nil {
- return
- }
- for _, list := range *lists {
- resp = append(resp, list.Uid)
- }
- return
- }
-
- // 递归查找等级
- func makeSearchLevel(position *int64, rows float64, times *float64) (level int) {
- difference := *position - int64(math.Pow(rows, *times))
- *times++
- if difference <= 0 {
- return int(*times)
- } else {
- position = &difference
- return makeSearchLevel(position, rows, times)
- }
- }
-
- // DealCommonWealthPunish 处理公排处罚
- func DealCommonWealthPunish(engine *xorm.Engine, uid int64, reason string) (err error, resp []map[string]string) {
- session := engine.NewSession()
- defer func() {
- session.Close()
- if err := recover(); err != nil {
- _ = zhios_order_relate_logx.Error(err)
- }
- }()
- session.Begin()
-
- //1、查找 `user_public_platoon_setting` 基础设置
- publicPlatoonBasicSettingDb := implement.NewPublicPlatoonBasicSettingDb(engine)
- PublicPlatoonBasicSetting, err := publicPlatoonBasicSettingDb.PublicPlatoonBasicSettingGetOneByParams(map[string]interface{}{
- "key": "is_open",
- "value": 1,
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- //2、查询出 `public_platoon_user_relation` 中相关记录 && 将该记录的uid置为 -1
- publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
- params, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
- "key": "uid",
- "value": uid,
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
- if params == nil {
- fmt.Println("未查询到公排关系记录!!!!!!!", uid)
- return
- }
-
- //TODO::判断是否为推荐用户
- if params.RecommendUid == 0 {
- params.Uid = params.Uid - int64(time.Now().Unix())
- } else {
- params.Uid = params.Uid - int64(time.Now().Unix())
- }
-
- for n := 1; n <= 9; n++ {
- str := "father_uid" + strconv.Itoa(n)
- sql := fmt.Sprintf("UPDATE `public_platoon_user_relation` SET %s=%s where %s=%s", str, egg_system_rules.Int64ToStr(params.Uid), str, egg_system_rules.Int64ToStr(uid))
- fmt.Println(">>>>>>>sql>>>>>>>", sql)
- _, err = session.Exec(sql)
- if err != nil {
- _ = session.Rollback()
- return
- }
- }
-
- updateAffected, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationUpdate(session, params.Id, params)
- if err != nil {
- _ = session.Rollback()
- return
- }
- if updateAffected == 0 {
- err = errors.New("更新 public_platoon_user_relation 记录失败")
- _ = session.Rollback()
- return
- }
-
- //3、新增一条 `public_platoon_user_relation` 记录
- params.Uid = uid
- res, err := PublicPlatoon(engine, params.Uid, params.RecommendUid, *PublicPlatoonBasicSetting)
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- //4、新增一条 `public_platoon_user_system_punish_records` 记录
- now := time.Now()
- publicPlatoonUserSystemPunishRecordsDb := implement.NewPublicPlatoonUserSystemPunishRecordsDb(engine)
- insertAffected, err := publicPlatoonUserSystemPunishRecordsDb.PublicPlatoonUserSystemPunishRecordsInsert(session, &model.PublicPlatoonUserSystemPunishRecords{
- Uid: params.Uid,
- OldPosition: params.Position,
- NewPosition: res.Position,
- Date: now.AddDate(0, 0, 30).Format("2006-01-02"),
- Title: "系统处罚记录",
- Reason: reason,
- Type: 2,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
- if insertAffected == 0 {
- err = errors.New("新增 public_platoon_user_system_punish_records 记录失败")
- _ = session.Rollback()
- return
- }
-
- err = session.Commit()
- return
- }
-
- const DealUserPublicPlatoonPunishLockKey = "deal_user_public_platoon_lock_key"
-
- // DealUserPublicPlatoonPunish 处理公排处罚
- func DealUserPublicPlatoonPunish(engine *xorm.Engine) (err error) {
- now := time.Now()
- fmt.Println(now.Hour())
- if !(now.Hour() > 22 && now.Hour() < 24) {
- //TODO::只在凌晨一点 ~ 凌晨 8 点运行
- return errors.New("非运行时间")
- }
- //TODO::增加“悲观锁”防止串行
- getString, _ := cache.GetString(DealUserPublicPlatoonPunishLockKey)
- if getString != "" {
- fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
- return errors.New("上一次结算未执行完")
- }
- cache.SetEx(DealUserPublicPlatoonPunishLockKey, "running", 3600*8) //8小时
-
- //查找 `public_platoon_basic_setting` 基础设置
- publicPlatoonBasicSettingDb := implement.NewPublicPlatoonBasicSettingDb(engine)
- PublicPlatoonBasicSetting, err := publicPlatoonBasicSettingDb.PublicPlatoonBasicSettingGetOneByParams(map[string]interface{}{
- "key": "is_open",
- "value": 1,
- })
- if err != nil {
- return err
- }
- if PublicPlatoonBasicSetting == nil {
- return errors.New("公排制度未开启")
- }
- publicPlatoonFreePunishWithUserDb := implement.NewPublicPlatoonFreePunishWithUserDb(engine)
- mapPublicPlatoonFreePunishWithUser, err := publicPlatoonFreePunishWithUserDb.FindAllPublicPlatoonFreePunishWithUser()
- if err != nil {
- return err
- }
- if PublicPlatoonBasicSetting.SystemPunishReplace == 1 {
- systemPunishReplaceValue := PublicPlatoonBasicSetting.SystemPunishReplaceValue
- startDate := now.AddDate(0, 0, -systemPunishReplaceValue).Format("2006-01-02") + " 00:00:00"
- var page = 1
- var pageSize = 100
- for {
- var users []model.User
- err = engine.Limit(pageSize, (page-1)*pageSize).Desc("uid").Find(&users)
- if err != nil {
- return err
- }
- if len(users) <= 0 {
- break
- }
- for _, user := range users {
- var list []model.EggSignIn
- err = engine.Where("start_time >= ?", startDate).And("uid = ?", user.Id).Find(&list)
- if len(list) <= 0 && mapPublicPlatoonFreePunishWithUser[user.Id] == nil {
- var PublicPlatoonRecordsPunishWithUser model.PublicPlatoonRecordsPunishWithUser
- has, err1 := engine.Where("uid = ?", user.Id).Get(&PublicPlatoonRecordsPunishWithUser)
- if err1 != nil {
- return err1
- }
- PublicPlatoonRecordsPunishWithUserDate, _ := time.ParseInLocation("2006-01-02", PublicPlatoonRecordsPunishWithUser.Date, time.Local)
- if has && now.AddDate(0, 0, -7).After(PublicPlatoonRecordsPunishWithUserDate) {
- //TODO::不进行重复处罚
- continue
- }
- //进行处罚
- err, _ = DealCommonWealthPunish(engine, user.Id, "公排处罚")
- if err != nil {
- return err
- }
-
- //添加 public_platoon_records_punish_with_user 记录
- publicPlatoonRecordsPunishWithUserDb := implement.NewPublicPlatoonRecordsPunishWithUserDb(engine)
- if has {
- PublicPlatoonRecordsPunishWithUser.Date = now.Format("2006-01-02")
- _, err2 := publicPlatoonRecordsPunishWithUserDb.PublicPlatoonRecordsPunishWithUserUpdate(PublicPlatoonRecordsPunishWithUser.Id, &PublicPlatoonRecordsPunishWithUser, "date")
- if err2 != nil {
- return err2
- }
- } else {
- _, err2 := publicPlatoonRecordsPunishWithUserDb.PublicPlatoonRecordsPunishWithUserInsert(&model.PublicPlatoonRecordsPunishWithUser{
- Uid: user.Id,
- Date: now.Format("2006-01-02"),
- })
- if err2 != nil {
- return err2
- }
- }
- }
- }
- page++
- }
- }
- return
- }
|