identity.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package service
  2. import (
  3. "fmt"
  4. "strings"
  5. . "app.yhyue.com/moapp/jybase/common"
  6. "bp.jydev.jianyu360.cn/BaseService/userCenter/entity"
  7. . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
  8. )
  9. //获取用户可切换的身份列表
  10. func IdentityList(userId int64) []*Identity {
  11. result := []*Identity{}
  12. list := entity.BaseMysql.SelectBySql(`SELECT DISTINCT a.id as position_id,c.person_id,a.person_name,a.account_id,a.type as position_type,a.ent_id,b.id as ent_account_id,d.phone from base_user d
  13. inner join base_position a on (d.id=? and d.id=a.user_id)
  14. left JOIN base_account b on (b.type=1 and b.person_id=0 and a.ent_id=b.ent_id)
  15. left JOIN base_account c on (a.account_id=c.id)`, userId)
  16. if list != nil {
  17. m := map[int64]*Identity{}
  18. entIds := []string{}
  19. phone := ""
  20. for _, v := range *list {
  21. i := &Identity{
  22. EntId: Int64All(v["ent_id"]),
  23. PersonId: Int64All(v["person_id"]),
  24. UserName: ObjToString(v["person_name"]),
  25. AccountId: Int64All(v["account_id"]),
  26. EntAccountId: Int64All(v["ent_account_id"]),
  27. PositionId: Int64All(v["position_id"]),
  28. PositionType: Int64All(v["position_type"]),
  29. }
  30. if i.PositionType == 0 {
  31. i.Name = "个人版"
  32. m[0] = i
  33. } else if i.EntId > 0 {
  34. m[i.EntId] = i
  35. entIds = append(entIds, fmt.Sprint(i.EntId))
  36. }
  37. if phone == "" {
  38. phone = ObjToString(v["phone"])
  39. }
  40. }
  41. if len(entIds) > 0 {
  42. ents := entity.Mysql.SelectBySql(`SELECT a.id,a.name,a.ent_id,b.name as ent_name from entniche_user a inner join entniche_info b on (a.phone=? and b.id in (`+strings.Join(entIds, ",")+`) and a.ent_id=b.id) order by b.createtime`, phone)
  43. if ents != nil {
  44. for _, v := range *ents {
  45. i := m[Int64All(v["ent_id"])]
  46. i.EntUserId = Int64All(v["id"])
  47. i.EntUserName = ObjToString(v["name"])
  48. i.Name = ObjToString(v["ent_name"])
  49. result = append(result, i)
  50. }
  51. }
  52. }
  53. if m[0] != nil {
  54. result = append(result, m[0])
  55. }
  56. }
  57. return result
  58. }
  59. //获取账号id获取个人身份信息
  60. func IdentityByUserId(userId int64) *Identity {
  61. list := entity.BaseMysql.SelectBySql(`SELECT a.account_id,a.id as position_id,c.person_id,a.person_name,a.account_id,a.type as position_type from base_user d
  62. inner join base_position a on (d.id=? and a.type=0 and d.id=a.user_id)
  63. inner join base_account c on (c.type=0 and a.account_id=c.id) limit 1`, userId)
  64. if list != nil && len(*list) == 1 {
  65. return &Identity{
  66. EntId: Int64All((*list)[0]["ent_id"]),
  67. PersonId: Int64All((*list)[0]["person_id"]),
  68. UserName: ObjToString((*list)[0]["person_name"]),
  69. AccountId: Int64All((*list)[0]["account_id"]),
  70. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  71. PositionId: Int64All((*list)[0]["position_id"]),
  72. PositionType: Int64All((*list)[0]["position_type"]),
  73. }
  74. }
  75. return nil
  76. }
  77. //根据职位id获取身份信息
  78. func IdentityByPositionId(positionId int64) *Identity {
  79. list := entity.BaseMysql.SelectBySql(`SELECT a.id as position_id,c.person_id,a.person_name,a.account_id,a.type as position_type,a.ent_id,b.phone from base_position a
  80. inner join base_user b on (a.user_id=b.id)
  81. inner join base_account c on (a.id=? and a.account_id=c.id) limit 1`, positionId)
  82. if list != nil && len(*list) == 1 {
  83. identity := &Identity{
  84. EntId: Int64All((*list)[0]["ent_id"]),
  85. PersonId: Int64All((*list)[0]["person_id"]),
  86. UserName: ObjToString((*list)[0]["person_name"]),
  87. AccountId: Int64All((*list)[0]["account_id"]),
  88. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  89. PositionId: Int64All((*list)[0]["position_id"]),
  90. PositionType: Int64All((*list)[0]["position_type"]),
  91. }
  92. if identity.PositionType == 0 {
  93. identity.Name = "个人版"
  94. }
  95. phone := ObjToString((*list)[0]["phone"])
  96. if identity.EntId > 0 {
  97. ents := entity.Mysql.SelectBySql(`SELECT a.id,a.name,a.ent_id,b.name as ent_name from entniche_user a inner join entniche_info b on (a.phone=? and b.id=? and a.ent_id=b.id) limit 1`, phone, identity.EntId)
  98. if ents != nil && len(*ents) == 1 {
  99. identity.EntUserId = Int64All((*ents)[0]["id"])
  100. identity.EntUserName = ObjToString((*ents)[0]["name"])
  101. identity.Name = ObjToString((*ents)[0]["ent_name"])
  102. }
  103. }
  104. return identity
  105. }
  106. return nil
  107. }
  108. //根据企业员工id获取身份信息
  109. func IdentityByEntUserId(entUserId int64) *Identity {
  110. ents := entity.Mysql.SelectBySql(`SELECT a.id,a.name,a.phone,a.ent_id,b.name as ent_name from entniche_user a inner join entniche_info b on (a.id=? and a.ent_id=b.id) limit 1`, entUserId)
  111. if ents == nil || len(*ents) == 0 {
  112. return nil
  113. }
  114. if entId := Int64All((*ents)[0]["ent_id"]); entId > 0 {
  115. list := entity.BaseMysql.SelectBySql(`SELECT a.id as position_id,c.person_id,a.person_name,a.account_id,a.type as position_type,a.ent_id from base_user d
  116. inner join base_position a on (d.phone=? and a.ent_id=? and a.type=1 and d.id=a.user_id)
  117. inner join base_account c on (c.ent_id=? and c.type=1 and a.account_id=c.id) limit 1`, ObjToString((*ents)[0]["phone"]), entId, entId)
  118. if list != nil && len(*list) == 1 {
  119. identity := &Identity{
  120. EntId: Int64All((*list)[0]["ent_id"]),
  121. PersonId: Int64All((*list)[0]["person_id"]),
  122. UserName: ObjToString((*list)[0]["person_name"]),
  123. AccountId: Int64All((*list)[0]["account_id"]),
  124. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  125. PositionId: Int64All((*list)[0]["position_id"]),
  126. PositionType: Int64All((*list)[0]["position_type"]),
  127. EntUserId: Int64All((*ents)[0]["id"]),
  128. EntUserName: ObjToString((*ents)[0]["name"]),
  129. Name: ObjToString((*ents)[0]["ent_name"]),
  130. }
  131. return identity
  132. }
  133. }
  134. return nil
  135. }