identity.go 11 KB

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