identity.go 5.4 KB

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