identity.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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,a.role,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.EntUserRole = ObjToString(v["role"])
  54. i.EntDeptId = Int64All(v["dept_id"])
  55. result = append(result, i)
  56. }
  57. }
  58. }
  59. if m[0] != nil {
  60. result = append(result, m[0])
  61. }
  62. }
  63. return result
  64. }
  65. //根据账号id获取个人身份信息
  66. func IdentityByUserId(msl *Mysql, userId int64) *Identity {
  67. 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
  68. inner join base_service.base_position a on (d.id=? and a.type=0 and d.id=a.user_id)
  69. 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)
  70. if list != nil && len(*list) > 0 {
  71. return &Identity{
  72. PersonId: Int64All((*list)[0]["person_id"]),
  73. UserName: ObjToString((*list)[0]["person_name"]),
  74. AccountId: Int64All((*list)[0]["account_id"]),
  75. PositionId: Int64All((*list)[0]["position_id"]),
  76. PositionType: Int64All((*list)[0]["position_type"]),
  77. UserId: userId,
  78. }
  79. }
  80. return nil
  81. }
  82. //根据职位id获取身份信息
  83. func IdentityByPositionId(msl *Mysql, positionId int64) *Identity {
  84. 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
  85. inner join base_service.base_user b on (a.id=? and a.user_id=b.id)
  86. inner join base_service.base_account c on (a.account_id=c.id)
  87. 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)
  88. if list != nil && len(*list) == 1 {
  89. identity := &Identity{
  90. EntId: Int64All((*list)[0]["ent_id"]),
  91. PersonId: Int64All((*list)[0]["person_id"]),
  92. UserName: ObjToString((*list)[0]["person_name"]),
  93. AccountId: Int64All((*list)[0]["account_id"]),
  94. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  95. PositionId: positionId,
  96. PositionType: Int64All((*list)[0]["position_type"]),
  97. UserId: Int64All((*list)[0]["user_id"]),
  98. }
  99. if identity.PositionType == 0 {
  100. identity.Name = "个人版"
  101. }
  102. phone := ObjToString((*list)[0]["phone"])
  103. if identity.EntId > 0 {
  104. ents := msl.SelectBySql(`SELECT a.id,a.name,a.ent_id,a.niche_dis,a.role,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)
  105. if ents != nil && len(*ents) > 0 {
  106. identity.EntUserId = Int64All((*ents)[0]["id"])
  107. identity.EntUserName = ObjToString((*ents)[0]["name"])
  108. identity.Name = ObjToString((*ents)[0]["ent_name"])
  109. identity.EntRole = Int64All((*ents)[0]["role_id"])
  110. identity.EntNicheDis = Int64All((*ents)[0]["niche_dis"])
  111. identity.EntUserRole = ObjToString((*ents)[0]["role"])
  112. identity.EntDeptId = Int64All((*ents)[0]["dept_id"])
  113. }
  114. }
  115. return identity
  116. }
  117. return nil
  118. }
  119. //根据账户id获取身份信息
  120. func IdentityByAccountId(msl *Mysql, accountId int64) *Identity {
  121. array := msl.SelectBySql(`SELECT type,person_id,ent_id from base_service.base_account where id=?`, accountId)
  122. if array == nil || len(*array) == 0 {
  123. return nil
  124. } else if Int64All((*array)[0]["type"]) == 1 && Int64All((*array)[0]["person_id"]) == 0 {
  125. return &Identity{
  126. EntId: Int64All((*array)[0]["ent_id"]),
  127. EntAccountId: Int64All((*array)[0]["id"]),
  128. }
  129. }
  130. 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
  131. inner join base_service.base_user b on (c.id=? AND c.person_id=b.person_id)
  132. inner join base_service.base_position a on (a.account_id=c.id)
  133. 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)
  134. if list != nil && len(*list) == 1 {
  135. identity := &Identity{
  136. EntId: Int64All((*list)[0]["ent_id"]),
  137. PersonId: Int64All((*list)[0]["person_id"]),
  138. UserName: ObjToString((*list)[0]["person_name"]),
  139. AccountId: accountId,
  140. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  141. PositionId: Int64All((*list)[0]["position_id"]),
  142. PositionType: Int64All((*list)[0]["position_type"]),
  143. UserId: Int64All((*list)[0]["user_id"]),
  144. }
  145. if identity.PositionType == 0 {
  146. identity.Name = "个人版"
  147. }
  148. phone := ObjToString((*list)[0]["phone"])
  149. if identity.EntId > 0 {
  150. ents := msl.SelectBySql(`SELECT a.id,a.name,a.ent_id,a.niche_dis,a.role,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)
  151. if ents != nil && len(*ents) > 0 {
  152. identity.EntUserId = Int64All((*ents)[0]["id"])
  153. identity.EntUserName = ObjToString((*ents)[0]["name"])
  154. identity.Name = ObjToString((*ents)[0]["ent_name"])
  155. identity.EntRole = Int64All((*ents)[0]["role_id"])
  156. identity.EntNicheDis = Int64All((*ents)[0]["niche_dis"])
  157. identity.EntUserRole = ObjToString((*ents)[0]["role"])
  158. identity.EntDeptId = Int64All((*ents)[0]["dept_id"])
  159. }
  160. }
  161. return identity
  162. }
  163. return nil
  164. }
  165. //根据企业员工id获取身份信息
  166. func IdentityByEntUserId(msl *Mysql, mgo *MongodbSim, entUserId int64) *Identity {
  167. ents := msl.SelectBySql(`SELECT a.id,a.name,a.phone,a.ent_id,a.niche_dis,a.role,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)
  168. if ents == nil || len(*ents) == 0 {
  169. return nil
  170. }
  171. if entId, phone := Int64All((*ents)[0]["ent_id"]), ObjToString((*ents)[0]["phone"]); entId > 0 && phone != "" {
  172. users, ok := mgo.Find("user", map[string]interface{}{
  173. "i_appid": 2,
  174. "$or": []map[string]interface{}{
  175. map[string]interface{}{
  176. "s_phone": phone,
  177. },
  178. map[string]interface{}{
  179. "s_m_phone": phone,
  180. },
  181. },
  182. }, `{"s_phone":-1}`, map[string]interface{}{
  183. "base_user_id": 1,
  184. }, false, 0, 1)
  185. if !ok || users == nil {
  186. return nil
  187. }
  188. userId := Int64All((*users)[0]["base_user_id"])
  189. 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
  190. 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)
  191. 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)
  192. if list != nil && len(*list) > 0 {
  193. identity := &Identity{
  194. PersonId: Int64All((*list)[0]["person_id"]),
  195. UserName: ObjToString((*list)[0]["person_name"]),
  196. AccountId: Int64All((*list)[0]["account_id"]),
  197. EntAccountId: Int64All((*list)[0]["ent_account_id"]),
  198. PositionId: Int64All((*list)[0]["position_id"]),
  199. PositionType: Int64All((*list)[0]["position_type"]),
  200. UserId: userId,
  201. EntId: entId,
  202. EntUserId: Int64All((*ents)[0]["id"]),
  203. EntUserName: ObjToString((*ents)[0]["name"]),
  204. Name: ObjToString((*ents)[0]["ent_name"]),
  205. EntRole: Int64All((*ents)[0]["role_id"]),
  206. EntNicheDis: Int64All((*ents)[0]["niche_dis"]),
  207. EntUserRole: ObjToString((*ents)[0]["role"]),
  208. EntDeptId: Int64All((*ents)[0]["dept_id"]),
  209. }
  210. return identity
  211. }
  212. }
  213. return nil
  214. }
  215. //根据企业id获取身份信息
  216. func IdentityByEntId(msl *Mysql, entId int64) *Identity {
  217. list := msl.SelectBySql(`select id from base_service.base_account where ent_id=? and type=1 and person_id=0 limit 1`, entId)
  218. if list == nil || len(*list) == 0 {
  219. return nil
  220. }
  221. return &Identity{
  222. EntId: entId,
  223. EntAccountId: Int64All((*list)[0]["id"]),
  224. }
  225. }