identity.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package service
  2. import (
  3. "fmt"
  4. "strings"
  5. . "app.yhyue.com/moapp/jybase/common"
  6. . "app.yhyue.com/moapp/jybase/mongodb"
  7. . "app.yhyue.com/moapp/jybase/mysql"
  8. . "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
  9. )
  10. //获取用户可切换的身份列表
  11. func IdentityList(msl *Mysql, userId int64) []*Identity {
  12. result := []*Identity{}
  13. list := msl.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_service.base_user d
  14. inner join base_service.base_position a on (d.id=? and d.id=a.user_id)
  15. inner join base_service.base_account c on (a.account_id=c.id)
  16. left join base_service.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)
  17. if list != nil {
  18. m := map[int64]*Identity{}
  19. entIds := []string{}
  20. phone := ""
  21. for _, v := range *list {
  22. i := &Identity{
  23. EntId: Int64All(v["ent_id"]),
  24. PersonId: Int64All(v["person_id"]),
  25. UserName: ObjToString(v["person_name"]),
  26. AccountId: Int64All(v["account_id"]),
  27. EntAccountId: Int64All(v["ent_account_id"]),
  28. PositionId: Int64All(v["position_id"]),
  29. PositionType: Int64All(v["position_type"]),
  30. UserId: userId,
  31. }
  32. if i.PositionType == 0 {
  33. i.Name = "个人版"
  34. m[0] = i
  35. } else if i.EntId > 0 {
  36. m[i.EntId] = i
  37. entIds = append(entIds, fmt.Sprint(i.EntId))
  38. }
  39. if phone == "" {
  40. phone = ObjToString(v["phone"])
  41. }
  42. }
  43. if len(entIds) > 0 {
  44. ents := msl.SelectBySql(`SELECT a.id,a.name,a.ent_id,a.niche_dis,b.name as ent_name,c.role_id,d.dept_id from jianyu.entniche_user a inner join jianyu.entniche_info b on (a.phone=? and b.id in (`+strings.Join(entIds, ",")+`) and a.ent_id=b.id) left join jianyu.entniche_user_role c on (a.id=c.user_id) LEFT JOIN jianyu.entniche_department_user d ON (d.user_id=a.id) order by b.createtime`, phone)
  45. if ents != nil {
  46. for _, v := range *ents {
  47. i := m[Int64All(v["ent_id"])]
  48. i.EntUserId = Int64All(v["id"])
  49. i.EntUserName = ObjToString(v["name"])
  50. i.Name = ObjToString(v["ent_name"])
  51. i.EntRole = Int64All(v["role_id"])
  52. i.EntNicheDis = Int64All(v["niche_dis"])
  53. i.EntDeptId = Int64All(v["dept_id"])
  54. result = append(result, i)
  55. }
  56. }
  57. }
  58. if m[0] != nil {
  59. result = append(result, m[0])
  60. }
  61. }
  62. return result
  63. }
  64. //根据账号id获取个人身份信息
  65. func IdentityByUserId(msl *Mysql, userId int64) *Identity {
  66. list := msl.SelectBySql(`SELECT a.account_id,a.id as position_id,c.person_id,a.person_name,a.type as position_type from base_service.base_user d
  67. inner join base_service.base_position a on (d.id=? and a.type=0 and d.id=a.user_id)
  68. inner join base_service.base_account c on (c.type=0 and a.account_id=c.id) order by a.id desc limit 1`, userId)
  69. if list != nil && len(*list) > 0 {
  70. return &Identity{
  71. PersonId: Int64All((*list)[0]["person_id"]),
  72. UserName: ObjToString((*list)[0]["person_name"]),
  73. AccountId: Int64All((*list)[0]["account_id"]),
  74. PositionId: Int64All((*list)[0]["position_id"]),
  75. PositionType: Int64All((*list)[0]["position_type"]),
  76. UserId: userId,
  77. }
  78. }
  79. return nil
  80. }
  81. //根据职位id获取身份信息
  82. func IdentityByPositionId(msl *Mysql, positionId int64) *Identity {
  83. list := msl.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_service.base_position a
  84. inner join base_service.base_user b on (a.user_id=b.id)
  85. inner join base_service.base_account c on (a.id=? and a.account_id=c.id)
  86. left join base_service.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)
  87. if list != nil && len(*list) == 1 {
  88. identity := &Identity{
  89. EntId: Int64All((*list)[0]["ent_id"]),
  90. PersonId: Int64All((*list)[0]["person_id"]),
  91. UserName: ObjToString((*list)[0]["person_name"]),
  92. AccountId: Int64All((*list)[0]["account_id"]),
  93. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  94. PositionId: Int64All((*list)[0]["position_id"]),
  95. PositionType: Int64All((*list)[0]["position_type"]),
  96. UserId: Int64All((*list)[0]["user_id"]),
  97. }
  98. if identity.PositionType == 0 {
  99. identity.Name = "个人版"
  100. }
  101. phone := ObjToString((*list)[0]["phone"])
  102. if identity.EntId > 0 {
  103. ents := msl.SelectBySql(`SELECT a.id,a.name,a.ent_id,a.niche_dis,b.name as ent_name,c.role_id,d.dept_id from jianyu.entniche_user a inner join jianyu.entniche_info b on (a.phone=? and b.id=? and a.ent_id=b.id) left join jianyu.entniche_user_role c on (a.id=c.user_id) LEFT JOIN jianyu.entniche_department_user d ON (d.user_id=a.id) limit 1`, phone, identity.EntId)
  104. if ents != nil && len(*ents) > 0 {
  105. identity.EntUserId = Int64All((*ents)[0]["id"])
  106. identity.EntUserName = ObjToString((*ents)[0]["name"])
  107. identity.Name = ObjToString((*ents)[0]["ent_name"])
  108. identity.EntRole = Int64All((*ents)[0]["role_id"])
  109. identity.EntNicheDis = Int64All((*ents)[0]["niche_dis"])
  110. identity.EntDeptId = Int64All((*ents)[0]["dept_id"])
  111. }
  112. }
  113. return identity
  114. }
  115. return nil
  116. }
  117. //根据企业员工id获取身份信息
  118. func IdentityByEntUserId(msl *Mysql, mgo *MongodbSim, entUserId int64) *Identity {
  119. ents := msl.SelectBySql(`SELECT a.id,a.name,a.phone,a.ent_id,a.niche_dis,b.name as ent_name,c.role_id,d.dept_id from jianyu.entniche_user a inner join jianyu.entniche_info b on (a.id=? and a.ent_id=b.id) left join jianyu.entniche_user_role c on (a.id=c.user_id) LEFT JOIN jianyu.entniche_department_user d ON (d.user_id=a.id) limit 1`, entUserId)
  120. if ents == nil || len(*ents) == 0 {
  121. return nil
  122. }
  123. if entId, phone := Int64All((*ents)[0]["ent_id"]), ObjToString((*ents)[0]["phone"]); entId > 0 && phone != "" {
  124. users, ok := mgo.Find(Mgo_User, map[string]interface{}{
  125. "$or": []map[string]interface{}{
  126. map[string]interface{}{
  127. "s_phone": phone,
  128. },
  129. map[string]interface{}{
  130. "s_m_phone": phone,
  131. },
  132. },
  133. }, `{"s_phone":-1}`, map[string]interface{}{
  134. "base_user_id": 1,
  135. }, false, 0, 1)
  136. if !ok || users == nil {
  137. return nil
  138. }
  139. userId := Int64All((*users)[0]["base_user_id"])
  140. list := msl.SelectBySql(`SELECT a.id as position_id,c.person_id,a.person_name,a.account_id,a.type as position_type,b.id as ent_account_id from base_service.base_position a
  141. inner join base_service.base_account c on (a.user_id=? and a.ent_id=? and a.type=1 and c.ent_id=? and c.type=1 and a.account_id=c.id)
  142. inner join base_service.base_account b on (b.type=1 and b.person_id=0 and b.ent_id=?) order by a.id desc limit 1`, userId, entId, entId, entId)
  143. if list != nil && len(*list) > 0 {
  144. identity := &Identity{
  145. PersonId: Int64All((*list)[0]["person_id"]),
  146. UserName: ObjToString((*list)[0]["person_name"]),
  147. AccountId: Int64All((*list)[0]["account_id"]),
  148. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  149. PositionId: Int64All((*list)[0]["position_id"]),
  150. PositionType: Int64All((*list)[0]["position_type"]),
  151. UserId: userId,
  152. EntId: entId,
  153. EntUserId: Int64All((*ents)[0]["id"]),
  154. EntUserName: ObjToString((*ents)[0]["name"]),
  155. Name: ObjToString((*ents)[0]["ent_name"]),
  156. EntRole: Int64All((*ents)[0]["role_id"]),
  157. EntNicheDis: Int64All((*ents)[0]["niche_dis"]),
  158. EntDeptId: Int64All((*ents)[0]["dept_id"]),
  159. }
  160. return identity
  161. }
  162. }
  163. return nil
  164. }
  165. //根据企业id获取身份信息
  166. func IdentityByEntId(msl *Mysql, entId int64) *Identity {
  167. list := msl.SelectBySql(`select id from base_service.base_account where ent_id=? and type=1 and person_id=0 limit 1`, entId)
  168. if list == nil || len(*list) == 0 {
  169. return nil
  170. }
  171. return &Identity{
  172. EntAccountId: Int64All((*list)[0]["id"]),
  173. }
  174. }