|
- package svc
-
- import (
- "applet/app/db"
- "applet/app/utils/logx"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- "fmt"
- "time"
- )
-
- // RoutineInsertUserRelate is 协程 当关联上级用户时,需要查询当前用户的所有关联下级,并新增关联上级与当前用户下级关系
- func RoutineInsertUserRelate(puid, uid int64, addlv int) {
- userRelateDb := implement.NewUserRelateDb(db.Db)
- defer Rev()
- urs, err := userRelateDb.UserRelatesByPuid(uid, 0, 0)
- if err != nil {
- logx.Warn(err)
- }
- // fmt.Println(*urs)
- for _, item := range *urs {
- _, err = userRelateDb.UserRelateInsert(
- &model.UserRelate{
- ParentUid: puid,
- Uid: item.Uid,
- Level: item.Level + addlv,
- InviteTime: time.Now().Format("2006-01-02 15:04:05"),
- })
- 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, uid int64, lv int) {
- userDb := implement.NewUserDb(db.Db)
- userRelateDb := implement.NewUserRelateDb(db.Db)
- 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().Format("2006-01-02 15:04:05")
- _, err = userRelateDb.UserRelateInsert(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
- }
- }
|