identity.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. inner join base_account c on (a.account_id=c.id)
  15. left join base_account b on (b.type=1 and b.person_id=0 and b.ent_id<>0 and c.ent_id=b.ent_id) order by a.id desc`, 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. UserId: userId,
  30. }
  31. if i.PositionType == 0 {
  32. i.Name = "个人版"
  33. m[0] = i
  34. } else if i.EntId > 0 {
  35. m[i.EntId] = i
  36. entIds = append(entIds, fmt.Sprint(i.EntId))
  37. }
  38. if phone == "" {
  39. phone = ObjToString(v["phone"])
  40. }
  41. }
  42. if len(entIds) > 0 {
  43. 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)
  44. if ents != nil {
  45. for _, v := range *ents {
  46. i := m[Int64All(v["ent_id"])]
  47. i.EntUserId = Int64All(v["id"])
  48. i.EntUserName = ObjToString(v["name"])
  49. i.Name = ObjToString(v["ent_name"])
  50. result = append(result, i)
  51. }
  52. }
  53. }
  54. if m[0] != nil {
  55. result = append(result, m[0])
  56. }
  57. }
  58. return result
  59. }
  60. //根据账号id获取个人身份信息
  61. func IdentityByUserId(userId int64) *Identity {
  62. list := entity.BaseMysql.SelectBySql(`SELECT a.account_id,a.id as position_id,c.person_id,a.person_name,a.type as position_type from base_user d
  63. inner join base_position a on (d.id=? and a.type=0 and d.id=a.user_id)
  64. inner join base_account c on (c.type=0 and a.account_id=c.id) order by a.id desc limit 1`, userId)
  65. if list != nil && len(*list) == 1 {
  66. return &Identity{
  67. PersonId: Int64All((*list)[0]["person_id"]),
  68. UserName: ObjToString((*list)[0]["person_name"]),
  69. AccountId: Int64All((*list)[0]["account_id"]),
  70. PositionId: Int64All((*list)[0]["position_id"]),
  71. PositionType: Int64All((*list)[0]["position_type"]),
  72. UserId: userId,
  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,b.id as user_id,d.id as ent_account_id 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)
  82. left join base_account d on (d.type=1 and d.person_id=0 and d.ent_id<>0 and d.ent_id=a.ent_id) limit 1`, positionId)
  83. if list != nil && len(*list) == 1 {
  84. identity := &Identity{
  85. EntId: Int64All((*list)[0]["ent_id"]),
  86. PersonId: Int64All((*list)[0]["person_id"]),
  87. UserName: ObjToString((*list)[0]["person_name"]),
  88. AccountId: Int64All((*list)[0]["account_id"]),
  89. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  90. PositionId: Int64All((*list)[0]["position_id"]),
  91. PositionType: Int64All((*list)[0]["position_type"]),
  92. UserId: Int64All((*list)[0]["user_id"]),
  93. }
  94. if identity.PositionType == 0 {
  95. identity.Name = "个人版"
  96. }
  97. phone := ObjToString((*list)[0]["phone"])
  98. if identity.EntId > 0 {
  99. 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)
  100. if ents != nil && len(*ents) == 1 {
  101. identity.EntUserId = Int64All((*ents)[0]["id"])
  102. identity.EntUserName = ObjToString((*ents)[0]["name"])
  103. identity.Name = ObjToString((*ents)[0]["ent_name"])
  104. }
  105. }
  106. return identity
  107. }
  108. return nil
  109. }
  110. //根据企业员工id获取身份信息
  111. func IdentityByEntUserId(entUserId int64) *Identity {
  112. 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)
  113. if ents == nil || len(*ents) == 0 {
  114. return nil
  115. }
  116. if entId := Int64All((*ents)[0]["ent_id"]); entId > 0 {
  117. list := entity.BaseMysql.SelectBySql(`SELECT a.id as position_id,c.person_id,a.person_name,a.account_id,a.type as position_type,d.id as user_id,b.id as ent_account_id from base_user d
  118. inner join base_position a on (d.phone=? and a.ent_id=? and a.type=1 and d.id=a.user_id)
  119. inner join base_account c on (c.ent_id=? and c.type=1 and a.account_id=c.id)
  120. inner join base_account b on (b.type=1 and b.person_id=0 and b.ent_id=?) order by a.id desc limit 1`, ObjToString((*ents)[0]["phone"]), entId, entId, entId)
  121. if list != nil && len(*list) == 1 {
  122. identity := &Identity{
  123. PersonId: Int64All((*list)[0]["person_id"]),
  124. UserName: ObjToString((*list)[0]["person_name"]),
  125. AccountId: Int64All((*list)[0]["account_id"]),
  126. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  127. PositionId: Int64All((*list)[0]["position_id"]),
  128. PositionType: Int64All((*list)[0]["position_type"]),
  129. UserId: Int64All((*list)[0]["user_id"]),
  130. EntId: entId,
  131. EntUserId: Int64All((*ents)[0]["id"]),
  132. EntUserName: ObjToString((*ents)[0]["name"]),
  133. Name: ObjToString((*ents)[0]["ent_name"]),
  134. }
  135. return identity
  136. }
  137. }
  138. return nil
  139. }
  140. //根据企业id获取身份信息
  141. func IdentityByEntId(entId int64) *Identity {
  142. list := entity.BaseMysql.SelectBySql(`select id from base_account where ent_id=? and type=1 and person_id=0 limit 1`, entId)
  143. if list == nil || len(*list) == 0 {
  144. return nil
  145. }
  146. return &Identity{
  147. EntAccountId: Int64All((*list)[0]["id"]),
  148. }
  149. }