|
- 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.ID(relation1.Id).Cols(forceColumns...).Update(relation1)
- if err != nil {
- return zhios_order_relate_logx.Error(err)
- }
- _, err = session.ID(relation2.Id).Cols(forceColumns...).Update(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
- }
|