identity.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.id=? and a.user_id=b.id)
  85. inner join base_service.base_account c on (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: positionId,
  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 IdentityByAccountId(msl *Mysql, accountId int64) *Identity {
  119. array := msl.SelectBySql(`SELECT type,person_id,ent_id from base_service.base_account where id=?`, accountId)
  120. if array == nil || len(*array) == 0 {
  121. return nil
  122. } else if Int64All((*array)[0]["type"]) == 1 && Int64All((*array)[0]["person_id"]) == 0 {
  123. return &Identity{
  124. EntId: Int64All((*array)[0]["ent_id"]),
  125. EntAccountId: Int64All((*array)[0]["id"]),
  126. }
  127. }
  128. list := msl.SelectBySql(`SELECT a.id as position_id,c.person_id,a.person_name,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_account c
  129. inner join base_service.base_user b on (c.id=? AND c.person_id=b.person_id)
  130. inner join base_service.base_position a on (a.account_id=c.id)
  131. 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`, accountId)
  132. if list != nil && len(*list) == 1 {
  133. identity := &Identity{
  134. EntId: Int64All((*list)[0]["ent_id"]),
  135. PersonId: Int64All((*list)[0]["person_id"]),
  136. UserName: ObjToString((*list)[0]["person_name"]),
  137. AccountId: accountId,
  138. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  139. PositionId: Int64All((*list)[0]["position_id"]),
  140. PositionType: Int64All((*list)[0]["position_type"]),
  141. UserId: Int64All((*list)[0]["user_id"]),
  142. }
  143. if identity.PositionType == 0 {
  144. identity.Name = "个人版"
  145. }
  146. phone := ObjToString((*list)[0]["phone"])
  147. if identity.EntId > 0 {
  148. 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)
  149. if ents != nil && len(*ents) > 0 {
  150. identity.EntUserId = Int64All((*ents)[0]["id"])
  151. identity.EntUserName = ObjToString((*ents)[0]["name"])
  152. identity.Name = ObjToString((*ents)[0]["ent_name"])
  153. identity.EntRole = Int64All((*ents)[0]["role_id"])
  154. identity.EntNicheDis = Int64All((*ents)[0]["niche_dis"])
  155. identity.EntDeptId = Int64All((*ents)[0]["dept_id"])
  156. }
  157. }
  158. return identity
  159. }
  160. return nil
  161. }
  162. //根据企业员工id获取身份信息
  163. func IdentityByEntUserId(msl *Mysql, mgo *MongodbSim, entUserId int64) *Identity {
  164. 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)
  165. if ents == nil || len(*ents) == 0 {
  166. return nil
  167. }
  168. if entId, phone := Int64All((*ents)[0]["ent_id"]), ObjToString((*ents)[0]["phone"]); entId > 0 && phone != "" {
  169. users, ok := mgo.Find("user", map[string]interface{}{
  170. "$or": []map[string]interface{}{
  171. map[string]interface{}{
  172. "s_phone": phone,
  173. },
  174. map[string]interface{}{
  175. "s_m_phone": phone,
  176. },
  177. },
  178. }, `{"s_phone":-1}`, map[string]interface{}{
  179. "base_user_id": 1,
  180. }, false, 0, 1)
  181. if !ok || users == nil {
  182. return nil
  183. }
  184. userId := Int64All((*users)[0]["base_user_id"])
  185. 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
  186. 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)
  187. 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)
  188. if list != nil && len(*list) > 0 {
  189. identity := &Identity{
  190. PersonId: Int64All((*list)[0]["person_id"]),
  191. UserName: ObjToString((*list)[0]["person_name"]),
  192. AccountId: Int64All((*list)[0]["account_id"]),
  193. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  194. PositionId: Int64All((*list)[0]["position_id"]),
  195. PositionType: Int64All((*list)[0]["position_type"]),
  196. UserId: userId,
  197. EntId: entId,
  198. EntUserId: Int64All((*ents)[0]["id"]),
  199. EntUserName: ObjToString((*ents)[0]["name"]),
  200. Name: ObjToString((*ents)[0]["ent_name"]),
  201. EntRole: Int64All((*ents)[0]["role_id"]),
  202. EntNicheDis: Int64All((*ents)[0]["niche_dis"]),
  203. EntDeptId: Int64All((*ents)[0]["dept_id"]),
  204. }
  205. return identity
  206. }
  207. }
  208. return nil
  209. }
  210. //根据企业id获取身份信息
  211. func IdentityByEntId(msl *Mysql, entId int64) *Identity {
  212. list := msl.SelectBySql(`select id from base_service.base_account where ent_id=? and type=1 and person_id=0 limit 1`, entId)
  213. if list == nil || len(*list) == 0 {
  214. return nil
  215. }
  216. return &Identity{
  217. EntId: entId,
  218. EntAccountId: Int64All((*list)[0]["id"]),
  219. }
  220. }