|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- package svc
-
- import (
- "applet/app/db"
- md "applet/app/md/institutional_management/public_platoon"
- "applet/app/utils"
- "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"
- "strings"
- "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, page, pageSize int) (total int64, sons []md.SonUserDailyActivityAnalysisNode, err error) {
- relationDb := implement.NewUserRelateDb(engine)
- sonRelations, total, err := relationDb.PageFindUserRelateByParentUid(fatherUID, page, pageSize)
- if err != nil {
- return 0, nil, zhios_order_relate_logx.Error(err)
- }
- if sonRelations == nil {
- return 0, nil, nil
- }
- userIDs := make([]string, 0)
- for _, relation := range *sonRelations {
- userIDs = append(userIDs, utils.Int64ToStr(int64(relation.Uid)))
- }
-
- userDb := implement.NewUserDb(engine)
- users, err := userDb.UserFindByParams(map[string]interface{}{
- "key": "id",
- "value": userIDs,
- })
- if err != nil {
- return 0, nil, zhios_order_relate_logx.Error(err)
- }
-
- sql := "SELECT COUNT(*) AS total, uid FROM `egg_energy_user_activity` WHERE uid IN (%s) AND date >= '%s' AND date <= '%s' GROUP BY uid"
- results, err := db.QueryNativeString(engine, fmt.Sprintf(sql, strings.Join(userIDs, ","), startDate, endDate))
- if err != nil {
- return 0, nil, zhios_order_relate_logx.Error(err)
- }
-
- var resMap = map[string]string{}
- for _, res := range results {
- resMap[res["uid"]] = res["total"]
- }
-
- sons = make([]md.SonUserDailyActivityAnalysisNode, 0)
- for _, user := range users {
- sons = append(sons, md.SonUserDailyActivityAnalysisNode{
- Uid: user.Id,
- Nickname: user.Nickname,
- Phone: user.Phone,
- ActivityDayNums: utils.StrToInt64(resMap[utils.Int64ToStr(user.Id)]),
- })
- }
- return total, sons, nil
- }
|