|
- package svc
-
- import (
- md "applet/app/md/institutional_management/public_platoon"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_models.git/utils/logx"
- "fmt"
- "xorm.io/xorm"
- )
-
- func GetTrees(engine *xorm.Engine, firstUser *model.PublicPlatoonUserRelation, leveLimit int) (list *md.TreeNode, err error) {
- var trees func(list []*md.TreeNode) []*md.TreeNode
- trees = func(list []*md.TreeNode) []*md.TreeNode {
- for _, item := range list {
- userPublicPlatoonRelations := make([]*model.PublicPlatoonUserRelation, 0)
- err = engine.Where("father_uid1 = ?", item.Uid).Find(&userPublicPlatoonRelations)
- if err != nil || len(userPublicPlatoonRelations) == 0 {
- continue
- }
-
- userIDs := make([]int64, len(userPublicPlatoonRelations))
- for i, relation := range userPublicPlatoonRelations {
- userIDs[i] = relation.Uid
- }
-
- users := make(map[int64]model.User)
- var userRecords []model.User
- err = engine.In("id", userIDs).Find(&userRecords)
- if err != nil {
- users = make(map[int64]model.User)
- } else {
- for _, user := range userRecords {
- users[user.Id] = user
- }
- }
-
- tempValue := make([]*md.TreeNode, 0)
- level := 1
- for _, relation := range userPublicPlatoonRelations {
- user := users[relation.Uid]
- tempValue = append(tempValue, &md.TreeNode{
- AvatarURL: user.Avatar,
- Phone: user.Phone,
- Nickname: user.Nickname,
- Uid: relation.Uid,
- PID: relation.RecommendUid,
- Position: relation.Position1,
- SystemID: relation.Id,
- Level: relation.LevelTotal,
- })
- level = relation.LevelTotal
- }
- item.Son = tempValue
- if level <= leveLimit {
- trees(item.Son)
- }
- }
- return list
- }
-
- if firstUser == nil {
- return
- }
-
- user := new(model.User)
- has, err := engine.ID(firstUser.Uid).Get(user)
- if err != nil || !has {
- return
- }
-
- rootNode := &md.TreeNode{
- AvatarURL: user.Avatar,
- Phone: user.Phone,
- Nickname: user.Nickname,
- Uid: firstUser.Uid,
- PID: firstUser.RecommendUid,
- Level: firstUser.LevelTotal,
- Position: firstUser.Position1,
- SystemID: firstUser.Id,
- }
- trees([]*md.TreeNode{rootNode})
- return rootNode, nil
- }
-
- func ExchangeUserPosition(engine *xorm.Engine, relation1, relation2 *model.PublicPlatoonUserRelation) (err error) {
- uid1 := relation1.Uid
- uid2 := relation2.Uid
- recommendUid1 := relation1.RecommendUid
- recommendUid2 := relation2.RecommendUid
- relation1.Uid = uid2
- relation2.Uid = uid1
- relation1.RecommendUid = recommendUid2
- relation2.RecommendUid = recommendUid1
-
- forceColums := []string{"recommend_uid", "uid"}
-
- session := engine.NewSession()
- defer session.Close()
-
- if err := session.Begin(); err != nil {
- return zhios_order_relate_logx.Error(err)
- }
-
- _, err = session.Where("id = ?", relation1.Id).Cols(forceColums...).Update(session, &relation1)
- if err != nil {
- return zhios_order_relate_logx.Error(err)
- }
-
- _, err = session.Where("id = ?", relation2.Id).Cols(forceColums...).Update(session, &relation2)
- if err != nil {
- return zhios_order_relate_logx.Error(err)
- }
-
- m := new(model.PublicPlatoonUserRelation)
- for i := 1; i < 9; i++ {
- str := fmt.Sprintf("father_uid%d", i)
- _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), uid1).Cols(str).Update(map[string]interface{}{str: -66})
- _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), uid2).Cols(str).Update(map[string]interface{}{str: -666})
- if err != nil {
- return zhios_order_relate_logx.Error(err)
- }
- }
-
- for i := 1; i < 9; i++ {
- str := fmt.Sprintf("father_uid%d", i)
- _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), -66).Cols(str).Update(map[string]interface{}{str: uid2})
- _, err = session.Table(m).Where(fmt.Sprintf("%s = ?", str), -666).Cols(str).Update(map[string]interface{}{str: uid1})
- if err != nil {
- return zhios_order_relate_logx.Error(err)
- }
- }
-
- if err = session.Commit(); err != nil {
- return zhios_order_relate_logx.Error(err)
- }
-
- return nil
- }
|