package identity import ( "fmt" "sort" "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 EntUserRole string // } 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, EntUserRole: i.EntUserRole, } } // 切换身份 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 m["entUserRole"] = i.EntUserRole 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) } } } } } } // 切换至最优付费账户 if bestIdentity := getBestChoosePosition(sess, mgd, list); bestIdentity != nil { return bestIdentity.PositionId, NewIdentityInfo(bestIdentity).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 } // getBestChoosePosition // 首次登录身份选择 【p387多个身份都有付费产品权限,则先按照产品优先级进行身份的选择,即大会员>商机管理>超级订阅>省份订阅包,如若多个身份的产品权限一致,则优先选择“服务结束时间”晚的身份。】 func getBestChoosePosition(sess *httpsession.Session, mgd *Middleground, positionList []*pb.Identity) *pb.Identity { var ( appId = "10000" mgoUserId = ObjToString(sess.Get("mgoUserId")) baseUserId = Int64All(sess.Get("base_user_id")) scorePositionMap = map[string]*pb.Identity{} scoreArr []string ) for _, v := range positionList { var ( endTime int64 flag int64 weight string powerRes = mgd.PowerCheckCenter.Check(appId, mgoUserId, baseUserId, v.AccountId, v.EntId, v.PositionType, v.PositionId) ) if powerRes.Member.Status > 0 { flag, endTime = 9, powerRes.Member.GetEndTime() } else if powerRes.Entniche.Status > 0 { flag, endTime = 8, powerRes.Entniche.GetEndTime() } else if powerRes.Vip.Status > 0 { flag, endTime = 7, powerRes.Vip.GetEndTime() } else if powerRes.Free.PpStatus > 0 { flag, endTime = 6, powerRes.Free.GetPpEndTime() } if flag > 0 { weight = fmt.Sprintf("%d%d", flag, endTime) scoreArr = append(scoreArr, weight) scorePositionMap[weight] = v } } var ( sLen = len(scoreArr) pos = sLen - 1 ) if sLen > 0 { if sLen > 1 { // 当有多个付费身份时;根据权重字符串排序 sort.Strings(scoreArr) } return scorePositionMap[scoreArr[pos]] } return nil }