entity.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package model
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "encoding/json"
  6. IC "jyBXSubscribe/rpc/init"
  7. "strings"
  8. )
  9. type User struct {
  10. Id int
  11. Name string //员工姓名
  12. Mail string //邮箱
  13. Phone string //手机号
  14. Dept_id int //部门id
  15. Dept_name string //部门名称
  16. //Role string //角色
  17. Power int //权限
  18. }
  19. type CurrentUser struct {
  20. Role_admin_department bool //是否是部门管理员
  21. Role_admin_system bool //是否是系统管理员
  22. Dept *Department //部门信息
  23. BondPhone string //手机号
  24. NickName string //昵称
  25. HeadImageUrl string //头像
  26. PersonalAuth int //个人认证
  27. PersonalAuthReason string //个人认证不通过原因
  28. User_power int //是否分配权限
  29. User_name string //用户姓名
  30. }
  31. type Department struct {
  32. Id int
  33. Name string //部门名
  34. Pid int //上级部门id
  35. Pname string //上级部门名称
  36. Nodiff int //全员无差别接收 0:关闭 1:打开
  37. Subdis int //订阅分发 0:关闭 1:打开
  38. Aid int //管理员id
  39. Aname string //管理员姓名
  40. Rname string //角色名
  41. User_count int //该部门下员工的总数
  42. Dept_count int //该部门下子部门总数
  43. Ent_id int //公司id
  44. }
  45. type namePower struct {
  46. entUserId string
  47. power int //1 超级订阅权益 2大会员权益
  48. }
  49. const (
  50. //角色
  51. Role_admin_system = 1 //系统管理员
  52. Role_admin_department = 2 //部门管理员
  53. )
  54. // 当前登录用户的信息
  55. func EntInfo(entId, entUserId int) *CurrentUser {
  56. currentUser := &CurrentUser{
  57. Dept: &Department{},
  58. }
  59. user := IC.MainMysql.SelectBySql(`SELECT a.name as user_name from entniche_user a INNER JOIN entniche_user_role b on (a.id=b.user_id) where a.id=? and b.role_id=? limit 1`, entUserId, Role_admin_system)
  60. if user != nil && len(*user) > 0 {
  61. currentUser.Role_admin_system = true
  62. currentUser.User_name, _ = (*user)[0]["user_name"].(string)
  63. currentUser.User_power = 1
  64. r := IC.MainMysql.SelectBySql(`SELECT id,name,subdis,nodiff from entniche_department where ent_id=? and pid=0 limit 1`, entId)
  65. if r != nil && len(*r) == 1 {
  66. department := JsonUnmarshal((*r)[0], &Department{}).(*Department)
  67. if department != nil {
  68. department.Pid = department.Id
  69. currentUser.Dept = department
  70. }
  71. }
  72. } else {
  73. //角色、权限
  74. r := IC.MainMysql.SelectBySql(`SELECT a.name as user_name,a.power as user_power,b.role_id,d.id as dept_id,d.name as dept_name,d.subdis as dept_subdis,d.nodiff as dept_nodiff,e.id as dept_pid from entniche_user a
  75. LEFT JOIN entniche_user_role b on (b.user_id=?)
  76. INNER JOIN entniche_department_user c on (a.id=? and a.id=c.user_id)
  77. INNER JOIN entniche_department d on (c.dept_id=d.id)
  78. INNER JOIN entniche_department e on (e.ent_id=? and e.pid=0)
  79. order by a.id desc limit 1`, entUserId, entUserId, entId)
  80. if r != nil && len(*r) == 1 {
  81. currentUser.User_name, _ = (*r)[0]["user_name"].(string)
  82. currentUser.User_power = common.IntAll((*r)[0]["user_power"])
  83. if common.IntAll((*r)[0]["role_id"]) == Role_admin_department {
  84. currentUser.Role_admin_department = true
  85. }
  86. currentUser.Dept.Id = common.IntAll((*r)[0]["dept_id"])
  87. currentUser.Dept.Pid = common.IntAll((*r)[0]["dept_pid"])
  88. currentUser.Dept.Name = common.ObjToString((*r)[0]["dept_name"])
  89. currentUser.Dept.Subdis = common.IntAll((*r)[0]["dept_subdis"])
  90. currentUser.Dept.Nodiff = common.IntAll((*r)[0]["dept_nodiff"])
  91. }
  92. }
  93. return currentUser
  94. }
  95. // map转结构体
  96. func JsonUnmarshal(m interface{}, s interface{}) interface{} {
  97. var b []byte
  98. if v, ok := m.(string); ok {
  99. b = []byte(v)
  100. } else if v, ok := m.([]byte); ok {
  101. b = v
  102. } else {
  103. b, _ = json.Marshal(m)
  104. }
  105. json.Unmarshal(b, &s)
  106. return s
  107. }
  108. // 获取部门下可以进行分发的人员(不包含部门名称和部门id)
  109. func GetDisUsers(entId, deptId int) *[]*User {
  110. r := IC.MainMysql.SelectBySql(`select DISTINCT c.id,c.name,c.phone,c.power,b.dept_id from entniche_department_parent a
  111. INNER JOIN entniche_department_user b on (b.dept_id=? or (a.pid=? and a.id=b.dept_id))
  112. INNER JOIN entniche_user c on (c.ent_id=? and b.user_id=c.id)
  113. order by convert(c.name using gbk) COLLATE gbk_chinese_ci asc`, deptId, deptId, entId)
  114. users, _ := JsonUnmarshal(r, &[]*User{}).(*[]*User)
  115. if users == nil {
  116. return &[]*User{}
  117. }
  118. return users
  119. }
  120. // 获取企业下所有的员工(不包含部门名称和部门id)
  121. func GetEntUsers(entId int) *[]*User {
  122. r := IC.MainMysql.SelectBySql(`select a.id,a.name,a.phone,a.power,b.dept_id from entniche_user a LEFT JOIN entniche_department_user b on (a.id=b.user_id ) where ent_id=?
  123. order by convert(name using gbk) COLLATE gbk_chinese_ci asc`, entId)
  124. users, _ := JsonUnmarshal(*r, &[]*User{}).(*[]*User)
  125. if users == nil {
  126. return &[]*User{}
  127. }
  128. return users
  129. }
  130. // 获取企业下分配权益的用户
  131. func GetEntPowerUsers(entId int) map[namePower]int {
  132. nowFormat := date.NowFormat(date.Date_Short_Layout)
  133. r := IC.MainMysql.SelectBySql(`select a.ent_user_id,b.product_type from entniche_power a LEFT JOIN entniche_wait_empower b ON (a.wait_empower_id = b.id) where a.ent_id=? and a.status = 1 and b.start_time <=? and b.end_time>=? `, entId, nowFormat, nowFormat)
  134. if r == nil {
  135. return nil
  136. }
  137. data := make(map[namePower]int)
  138. for _, v := range *r {
  139. if strings.Contains(common.InterfaceToStr(v["product_type"]), "VIP") {
  140. var _n namePower
  141. _n.entUserId = common.InterfaceToStr(v["ent_user_id"])
  142. _n.power = 1
  143. data[_n] = 1
  144. } else if strings.Contains(common.InterfaceToStr(v["product_type"]), "大会员") {
  145. var _n namePower
  146. _n.entUserId = common.InterfaceToStr(v["ent_user_id"])
  147. _n.power = 2
  148. data[_n] = 2
  149. }
  150. }
  151. return data
  152. }
  153. func GetIsEnt(entId int) bool {
  154. return IC.MainMysql.CountBySql(`select count(*) from entniche_info where id=? and status = 1`, entId) > 0
  155. }