package identity import ( "strconv" . "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/go-xweb/httpsession" . "app.yhyue.com/moapp/jybase/mongodb" . "app.yhyue.com/moapp/jypkg/middleground" "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb" ) type IdentityInfo struct { Name string //名称 PersonId int64 //自然人id UserName string //用户昵称 AccountId int64 //账户id EntAccountId int64 //企业账户id PositionId int64 //职位id PositionType int64 //职位类型 EntId int64 //企业id EntUserId int64 //企业员工id EntUserName string //企业员工姓名 EntRole int64 //管理员角色 EntNicheDis int64 //商机分配角色 EntDeptId int64 //部门id } func NewIdentityInfo(i *pb.Identity) *IdentityInfo { return &IdentityInfo{ Name: i.Name, PersonId: i.PersonId, UserName: i.UserName, AccountId: i.AccountId, EntAccountId: i.EntAccountId, PositionId: i.PositionId, PositionType: i.PositionType, EntId: i.EntId, EntUserId: i.EntUserId, EntUserName: i.EntUserName, EntRole: i.EntRole, EntNicheDis: i.EntNicheDis, EntDeptId: i.EntDeptId, } } // 切换身份 func (i *IdentityInfo) Switch(sess *httpsession.Session, mgo *MongodbSim) bool { ok := false m := map[string]interface{}{ "personId": i.PersonId, "userName": i.UserName, "accountId": i.AccountId, "positionId": i.PositionId, "positionType": i.PositionType, } mgoUserId, _ := sess.Get("mgoUserId").(string) if i.PositionType == 0 { if sess.Del("entId", "entUserId", "entName", "entUserName", "frameworkEntId", "frameworkEntName", "userId", "entAccountId", "entRole", "entNicheDis", "entDeptId") { m["userId"] = mgoUserId ok = true } } else { m["entId"] = i.EntId m["entUserId"] = i.EntUserId m["entName"] = i.Name m["entUserName"] = i.EntUserName m["frameworkEntId"] = i.EntId m["frameworkEntName"] = i.Name m["userId"] = strconv.FormatInt(i.PositionId, 10) m["entAccountId"] = i.EntAccountId m["entRole"] = i.EntRole m["entNicheDis"] = i.EntNicheDis m["entDeptId"] = i.EntDeptId ok = true } sess.SetMultiple(m) mgo.UpdateById("user", mgoUserId, map[string]interface{}{ "$set": map[string]interface{}{ "login_positionid": i.PositionId, }, }) return ok } // 切换到最优身份 func SwitchToBest(userId int64, sess *httpsession.Session, mgd *Middleground, mgo *MongodbSim, isSelectLast bool) (int64, bool) { list := mgd.UserCenter.IdentityList(userId) if list == nil || len(list) == 0 { return -1, false } if isSelectLast { if mgoUserId, _ := sess.Get("mgoUserId").(string); mgoUserId != "" { user, ok := mgo.FindById("user", mgoUserId, `{"login_positionid":1}`) if ok && user != nil && len(*user) > 0 { if login_positionid := Int64All((*user)["login_positionid"]); login_positionid > 0 { for _, v := range list { if v.PositionId == login_positionid { return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo) } } } } } } reqIds := []int64{} for _, v := range list { if v.PositionType == 1 { reqIds = append(reqIds, v.EntUserId) } } if len(reqIds) > 0 { if resp := mgd.EntManageApplication.EmpowerUserIds(reqIds); resp != nil && len(resp.Ids) > 0 { for _, v := range list { if v.PositionType == 1 && v.EntUserId == resp.Ids[0] { return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo) } } } } for _, v := range list { if v.PositionType == 0 { return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo) } } return -1, false }