identity.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package identity
  2. import (
  3. "fmt"
  4. "sort"
  5. "strconv"
  6. . "app.yhyue.com/moapp/jybase/common"
  7. "app.yhyue.com/moapp/jybase/go-xweb/httpsession"
  8. . "app.yhyue.com/moapp/jybase/mongodb"
  9. . "app.yhyue.com/moapp/jypkg/middleground"
  10. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb"
  11. )
  12. type IdentityInfo struct {
  13. Name string //名称
  14. PersonId int64 //自然人id
  15. UserName string //用户昵称
  16. AccountId int64 //账户id
  17. EntAccountId int64 //企业账户id
  18. PositionId int64 //职位id
  19. PositionType int64 //职位类型
  20. EntId int64 //企业id
  21. EntUserId int64 //企业员工id
  22. EntUserName string //企业员工姓名
  23. EntRole int64 //管理员角色
  24. EntNicheDis int64 //商机分配角色
  25. EntDeptId int64 //部门id
  26. }
  27. func NewIdentityInfo(i *pb.Identity) *IdentityInfo {
  28. return &IdentityInfo{
  29. Name: i.Name,
  30. PersonId: i.PersonId,
  31. UserName: i.UserName,
  32. AccountId: i.AccountId,
  33. EntAccountId: i.EntAccountId,
  34. PositionId: i.PositionId,
  35. PositionType: i.PositionType,
  36. EntId: i.EntId,
  37. EntUserId: i.EntUserId,
  38. EntUserName: i.EntUserName,
  39. EntRole: i.EntRole,
  40. EntNicheDis: i.EntNicheDis,
  41. EntDeptId: i.EntDeptId,
  42. }
  43. }
  44. // 切换身份
  45. func (i *IdentityInfo) Switch(sess *httpsession.Session, mgo *MongodbSim) bool {
  46. ok := false
  47. m := map[string]interface{}{
  48. "personId": i.PersonId,
  49. "userName": i.UserName,
  50. "accountId": i.AccountId,
  51. "positionId": i.PositionId,
  52. "positionType": i.PositionType,
  53. }
  54. mgoUserId, _ := sess.Get("mgoUserId").(string)
  55. if i.PositionType == 0 {
  56. if sess.Del("entId", "entUserId", "entName", "entUserName", "frameworkEntId", "frameworkEntName", "userId", "entAccountId", "entRole", "entNicheDis", "entDeptId") {
  57. m["userId"] = mgoUserId
  58. ok = true
  59. }
  60. } else {
  61. m["entId"] = i.EntId
  62. m["entUserId"] = i.EntUserId
  63. m["entName"] = i.Name
  64. m["entUserName"] = i.EntUserName
  65. m["frameworkEntId"] = i.EntId
  66. m["frameworkEntName"] = i.Name
  67. m["userId"] = strconv.FormatInt(i.PositionId, 10)
  68. m["entAccountId"] = i.EntAccountId
  69. m["entRole"] = i.EntRole
  70. m["entNicheDis"] = i.EntNicheDis
  71. m["entDeptId"] = i.EntDeptId
  72. ok = true
  73. }
  74. sess.SetMultiple(m)
  75. mgo.UpdateById("user", mgoUserId, map[string]interface{}{
  76. "$set": map[string]interface{}{
  77. "login_positionid": i.PositionId,
  78. },
  79. })
  80. return ok
  81. }
  82. // 切换到最优身份
  83. func SwitchToBest(userId int64, sess *httpsession.Session, mgd *Middleground, mgo *MongodbSim, isSelectLast bool) (int64, bool) {
  84. list := mgd.UserCenter.IdentityList(userId)
  85. if list == nil || len(list) == 0 {
  86. return -1, false
  87. }
  88. //选择上次用户身份
  89. if isSelectLast {
  90. if mgoUserId, _ := sess.Get("mgoUserId").(string); mgoUserId != "" {
  91. user, ok := mgo.FindById("user", mgoUserId, `{"login_positionid":1}`)
  92. if ok && user != nil && len(*user) > 0 {
  93. if login_positionid := Int64All((*user)["login_positionid"]); login_positionid > 0 {
  94. for _, v := range list {
  95. if v.PositionId == login_positionid {
  96. return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo)
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. // 切换至最优付费账户
  104. if bestIdentity := getBestChoosePosition(sess, mgd, list); bestIdentity != nil {
  105. return bestIdentity.PositionId, NewIdentityInfo(bestIdentity).Switch(sess, mgo)
  106. }
  107. reqIds := []int64{}
  108. for _, v := range list {
  109. if v.PositionType == 1 {
  110. reqIds = append(reqIds, v.EntUserId)
  111. }
  112. }
  113. if len(reqIds) > 0 {
  114. if resp := mgd.EntManageApplication.EmpowerUserIds(reqIds); resp != nil && len(resp.Ids) > 0 {
  115. for _, v := range list {
  116. if v.PositionType == 1 && v.EntUserId == resp.Ids[0] {
  117. return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo)
  118. }
  119. }
  120. }
  121. }
  122. for _, v := range list {
  123. if v.PositionType == 0 {
  124. return v.PositionId, NewIdentityInfo(v).Switch(sess, mgo)
  125. }
  126. }
  127. return -1, false
  128. }
  129. // getBestChoosePosition
  130. // 首次登录身份选择 【p387多个身份都有付费产品权限,则先按照产品优先级进行身份的选择,即大会员>商机管理>超级订阅>省份订阅包,如若多个身份的产品权限一致,则优先选择“服务结束时间”晚的身份。】
  131. func getBestChoosePosition(sess *httpsession.Session, mgd *Middleground, positionList []*pb.Identity) *pb.Identity {
  132. var (
  133. appId = "10000"
  134. mgoUserId = ObjToString(sess.Get("mgoUserId"))
  135. baseUserId = Int64All(sess.Get("base_user_id"))
  136. scorePositionMap = map[string]*pb.Identity{}
  137. scoreArr []string
  138. )
  139. for _, v := range positionList {
  140. var (
  141. endTime int64
  142. flag int64
  143. weight string
  144. powerRes = mgd.PowerCheckCenter.Check(appId, mgoUserId, baseUserId, v.AccountId, v.EntId, v.PositionType, v.PositionId)
  145. )
  146. if powerRes.Member.Status > 0 {
  147. flag, endTime = 9, powerRes.Member.GetEndTime()
  148. } else if powerRes.Entniche.Status > 0 {
  149. flag, endTime = 8, powerRes.Entniche.GetEndTime()
  150. } else if powerRes.Vip.Status > 0 {
  151. flag, endTime = 7, powerRes.Vip.GetEndTime()
  152. } else if powerRes.Free.PpStatus > 0 {
  153. flag, endTime = 6, powerRes.Free.GetPpEndTime()
  154. }
  155. if flag > 0 {
  156. weight = fmt.Sprintf("%d%d", flag, endTime)
  157. scoreArr = append(scoreArr, weight)
  158. scorePositionMap[weight] = v
  159. }
  160. }
  161. var (
  162. sLen = len(scoreArr)
  163. pos = sLen - 1
  164. )
  165. if sLen > 0 {
  166. if sLen > 1 { // 当有多个付费身份时;根据权重字符串排序
  167. sort.Strings(scoreArr)
  168. }
  169. return scorePositionMap[scoreArr[pos]]
  170. }
  171. return nil
  172. }