entUser.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package entity
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "app.yhyue.com/moapp/jybase/common"
  6. "app.yhyue.com/moapp/jybase/mysql"
  7. "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/usercenter"
  8. )
  9. type EntUserInfo struct {
  10. AppId string
  11. EntUserId int64
  12. EntId int64
  13. Mysql *mysql.Mysql
  14. }
  15. type User struct {
  16. Name string //员工姓名
  17. Mail string //邮箱
  18. Phone string //手机号
  19. Dept_name string //部门名称
  20. EntUserId int64
  21. }
  22. //获取员工信息
  23. func (this *EntUserInfo) GetEntUserInfo() (*User, string) {
  24. data := this.Mysql.SelectBySql(`SELECT a.name,a.phone,a.mail,c.name AS dept_name,a.id as entUserId FROM entniche_user a
  25. INNER JOIN entniche_department_user b ON (a.id=? AND a.id=b.user_id)
  26. INNER JOIN entniche_department c ON (b.dept_id=c.id)
  27. WHERE a.ent_id=?`, this.EntUserId, this.EntId)
  28. if data == nil || len(*data) == 0 {
  29. return nil, "暂无数据"
  30. }
  31. users, _ := common.JsonUnmarshal(data, &[]*User{}).(*[]*User)
  32. return (*users)[0], ""
  33. }
  34. //1企业管理员 2部门管理员 3员工
  35. func (this *EntUserInfo) CheckEntAdmin() (int64, string) {
  36. if count := this.Mysql.CountBySql(`select count(1) from entniche_user where id =? and ent_id =?`, this.EntUserId, this.EntId); count <= 0 {
  37. return -1, "暂无数据"
  38. }
  39. if data := this.Mysql.SelectBySql(`select role_id from entniche_user_role where user_id =?`, this.EntUserId); data != nil && len(*data) > 0 {
  40. rule := common.Int64All((*data)[0]["role_id"])
  41. return rule, ""
  42. }
  43. return 3, ""
  44. }
  45. type EntUserList struct {
  46. EntId int64
  47. DeptId int64
  48. Name string
  49. Mysql *mysql.Mysql
  50. }
  51. func (this *EntUserList) GetEntUserList() ([]*usercenter.EntUserListData, string) {
  52. sql := `SELECT a.id,a.pid,a.name,c.id AS user_id,c.name AS user_name,c.mail AS user_mail, c.phone AS user_phone ,ed.name AS fdept FROM entniche_department a
  53. INNER JOIN entniche_department_user b ON (a.ent_id=? AND a.id=b.dept_id)
  54. INNER JOIN entniche_user c ON (b.user_id=c.id)
  55. LEFT JOIN entniche_department ed ON (a.pid=ed.id)`
  56. if this.Name != "" {
  57. sql += `where c.name like "%` + this.Name + `%"`
  58. }
  59. sql += `ORDER BY a.id,CONVERT(c.name USING gbk) COLLATE gbk_chinese_ci ASC`
  60. data := this.Mysql.SelectBySql(sql, this.EntId)
  61. listMap := map[int64][]*usercenter.EntUser{}
  62. if data != nil {
  63. for _, v := range *data {
  64. id := common.Int64All(v["id"])
  65. listMap[id] = append(listMap[id], &usercenter.EntUser{
  66. Name: common.ObjToString(v["user_name"]),
  67. Phone: common.ObjToString(v["user_phone"]),
  68. Mail: common.ObjToString(v["user_mail"]),
  69. DeptName: common.ObjToString(v["name"]),
  70. EntUserId: common.Int64All(v["user_id"]),
  71. })
  72. }
  73. }
  74. deptData := &[]map[string]interface{}{}
  75. //获取部门层级关系
  76. if this.DeptId > 0 {
  77. deptData = this.Mysql.SelectBySql(`select a.id,a.name,a.pid from entniche_department a
  78. inner join entniche_department_parent b on (a.ent_id=? and b.pid=? and a.id =b.id)`, this.EntId, this.DeptId)
  79. } else {
  80. deptData = this.Mysql.SelectBySql(`select id,name,pid from entniche_department where ent_id=?`, this.EntId)
  81. }
  82. deptList := []*usercenter.EntUserListData{}
  83. ret := []*usercenter.EntUserListData{}
  84. if len(*deptData) > 0 {
  85. for _, v := range *deptData {
  86. deptList = append(deptList, &usercenter.EntUserListData{
  87. DeptId: common.Int64All(v["id"]),
  88. Name: common.ObjToString(v["name"]),
  89. PId: common.Int64All(v["pid"]),
  90. EntUserList: listMap[common.Int64All(v["id"])],
  91. DeptList: []*usercenter.EntUserListData{},
  92. })
  93. }
  94. //创建根
  95. root := []*usercenter.EntUserListData{deptList[0]}
  96. //获取根上部门
  97. pdept := make([]*usercenter.EntUserListData, 0)
  98. for i, _ := range deptList {
  99. var a *usercenter.EntUserListData
  100. a = deptList[i]
  101. pdept = append(pdept, a)
  102. }
  103. //
  104. for _, v := range root {
  105. makeTree(pdept, v)
  106. data, _ := json.Marshal(&v)
  107. fmt.Println(string(data))
  108. ret = append(ret, v)
  109. }
  110. }
  111. if len(ret) == 0 {
  112. return ret, "暂无数据"
  113. }
  114. return ret, ""
  115. }
  116. func has(v1 usercenter.EntUserListData, vs []*usercenter.EntUserListData) bool {
  117. var has bool
  118. has = false
  119. for _, v2 := range vs {
  120. v3 := *v2
  121. if v1.PId == v3.DeptId {
  122. has = true
  123. break
  124. }
  125. }
  126. return has
  127. }
  128. func makeTree(vs []*usercenter.EntUserListData, node *usercenter.EntUserListData) {
  129. childs := findChild(node, vs)
  130. for _, child := range childs {
  131. node.DeptList = append(node.DeptList, child)
  132. if has(*child, vs) {
  133. makeTree(vs, child)
  134. }
  135. }
  136. }
  137. func findChild(v *usercenter.EntUserListData, vs []*usercenter.EntUserListData) (ret []*usercenter.EntUserListData) {
  138. for _, v2 := range vs {
  139. if v.DeptId == v2.PId {
  140. ret = append(ret, v2)
  141. }
  142. }
  143. return
  144. }