|
- package svc
-
- import (
- md "applet/app/md/institutional_management/public_platoon"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "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
-
- forceColumns := []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.Table(new(model.PublicPlatoonUserRelation)).ID(relation1.Id).Update(map[string]interface{}{"uid": -99})
- if err != nil {
- return zhios_order_relate_logx.Error(err)
- }
- _, err = session.Table(new(model.PublicPlatoonUserRelation)).ID(relation2.Id).Update(map[string]interface{}{"uid": -999})
- if err != nil {
- return zhios_order_relate_logx.Error(err)
- }
-
- _, err = session.Where("id = ?", relation1.Id).Cols(forceColumns...).Update(session, &relation1)
- if err != nil {
- return zhios_order_relate_logx.Error(err)
- }
- _, err = session.Where("id = ?", relation2.Id).Cols(forceColumns...).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
- }
-
- func GetSonUserDailyActivity(engine *xorm.Engine, fatherUID int64, startDate, endDate string) (activityUserCount int64, activityCount int64, sons []md.SonUserDailyActivityAnalysisNode, err error) {
- relationDb := implement.NewUserRelateDb(engine)
- sonRelations, err := relationDb.FindUserRelateByParentUid(fatherUID, 1)
- if err != nil {
- return 0, 0, nil, err
- }
- if sonRelations == nil {
- return 0, 0, nil, nil
- }
- userIDs := make([]int64, 0)
- for _, relation := range *sonRelations {
- userIDs = append(userIDs, relation.Uid)
- }
-
- activityDb := implement.NewEggEnergyUserActivityDb(engine)
- // 先判断这些用户中是否有活跃用户,如果有再逐个统计
- activityCount, err = activityDb.UserDailyActivityAnalysisFindByUidAndTime(startDate, endDate, userIDs)
- if err != nil {
- return 0, 0, nil, zhios_order_relate_logx.Error(err)
- }
- if activityCount == 0 {
- return 0, 0, nil, nil
- }
- sons = make([]md.SonUserDailyActivityAnalysisNode, 0)
- for _, userID := range userIDs {
- count, err := activityDb.UserDailyActivityAnalysisCountByUidAndTime(startDate, endDate, userID)
- if err != nil {
- return 0, 0, nil, err
- }
- if count > 0 {
- sons = append(sons, md.SonUserDailyActivityAnalysisNode{
- Uid: userID,
- Nickname: "",
- Phone: "",
- ActivityDayNums: count,
- })
- activityUserCount++
- }
- }
- return activityUserCount, activityCount, sons, nil
- }
|