package svc import ( "applet/app/db" "applet/app/db/model" "applet/app/utils/logx" "fmt" "time" ) // RoutineInsertUserRelate is 协程 当关联上级用户时,需要查询当前用户的所有关联下级,并新增关联上级与当前用户下级关系 func RoutineInsertUserRelate(puid, uid, addlv int) { defer Rev() urs, err := db.UserRelatesByPuid(db.Db, uid, 0, 0) if err != nil { logx.Warn(err) } // fmt.Println(*urs) for _, item := range *urs { _, err = db.UserRelateInsert(db.Db, &model.UserRelate{ ParentUid: puid, Uid: item.Uid, Level: item.Level + addlv, InviteTime: time.Now(), }) if err != nil { continue } logx.Info(fmt.Sprintf("关联pid(%v) -> uid(%v) ,lv:%v", puid, item.Uid, item.Level+addlv)) } } // RoutineMultiRelate is 多级关联 func RoutineMultiRelate(pid int, uid int, lv int) { userDb := db.UserDb{} userDb.Set() defer Rev() for { if pid == 0 { break } m, err := userDb.GetUser(pid) if err != nil || m == nil { logx.Warn(err) break } if m.ParentUid == 0 { break } lv++ ur := new(model.UserRelate) ur.ParentUid = m.ParentUid ur.Uid = uid ur.Level = lv ur.InviteTime = time.Now() _, err = db.UserRelateInsert(db.Db, ur) if err != nil { logx.Warn(err) break } // 还要关联当前的用户的所有下级,注意关联等级 RoutineInsertUserRelate(m.ParentUid, uid, lv) // 下级关联上上级 // 继续查询 logx.Info(fmt.Sprintf("关联pid(%v) -> uid(%v),lv:%v", ur.ParentUid, ur.Uid, lv)) logx.Info("继续查询") pid = m.ParentUid } }