distributor.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package model
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/init"
  5. "strings"
  6. )
  7. func AllDistributor(entId, entUserId int) []*User {
  8. var (
  9. users []*User
  10. )
  11. entInfo := EntInfo(entId, entUserId)
  12. if entInfo.Role_admin_department {
  13. users = *GetDisUsers(entId, entInfo.Dept.Id)
  14. } else if entInfo.Role_admin_system {
  15. users = *GetEntUsers(entId)
  16. }
  17. return users
  18. }
  19. func Distributor(region []string, entId, entUserId int) []*User {
  20. var (
  21. ss, users []*User
  22. )
  23. entInfo := EntInfo(entId, entUserId)
  24. if entInfo.Role_admin_department {
  25. users = *GetDisUsers(entId, entInfo.Dept.Id)
  26. } else if entInfo.Role_admin_system {
  27. users = *GetEntUsers(entId)
  28. }
  29. //获取所有分配权益的用户
  30. powerUser := GetEntPowerUsers(entId)
  31. //判断企业是否为商机管理企业
  32. isEnt := GetIsEnt(entId)
  33. regions := []string{}
  34. for _, v := range region {
  35. //剔除空与全国
  36. if v == "" || strings.Contains(v, "全国") {
  37. continue
  38. }
  39. regions = append(regions, v)
  40. }
  41. for _, v := range users {
  42. var _n namePower
  43. _n.entUserId = common.InterfaceToStr(v.Id)
  44. _n.power = 1
  45. n1 := powerUser[_n]
  46. _n.power = 2
  47. n2 := powerUser[_n]
  48. if (isEnt && v.Power == 1) || n1 != 0 || n2 != 0 { //有权益用户 判断是否被设置分配区域
  49. if len(regions) == 0 { //判断区域
  50. ss = append(ss, v)
  51. continue
  52. }
  53. var rule_id string
  54. //有区域限制 过滤筛选
  55. rules := IC.MainMysql.Find("entniche_user_rule", map[string]interface{}{"user_id": v.Id}, "rule_id,dept_id", "", -1, -1)
  56. if rules != nil && len(*rules) > 0 {
  57. if len(*rules) > 1 { //多条规则 查询优先级最高的规则
  58. ruleIds := make(map[int]string)
  59. for _, v2 := range *rules {
  60. ruleIds[common.IntAll(v2["dept_id"])] = common.InterfaceToStr(v2["rule_id"])
  61. }
  62. //获取部门优先级
  63. var ids []int
  64. departSort(&ids, v.Dept_id)
  65. ids = append(ids, v.Dept_id)
  66. //Reverse(&ids)
  67. for _, v1 := range ids {
  68. if v2, ok := ruleIds[v1]; ok && v2 != "" {
  69. rule_id = v2
  70. break
  71. }
  72. }
  73. } else {
  74. rule_id = common.InterfaceToStr((*rules)[0]["rule_id"])
  75. }
  76. if rule_id != "" {
  77. data, ok := IC.Mgo.FindById("entniche_distribute", rule_id, "")
  78. if ok && data != nil && len(*data) > 0 {
  79. o_area, _ := (*data)["o_area"].(map[string]interface{})
  80. if regionCheck(o_area, regions) {
  81. ss = append(ss, v)
  82. }
  83. continue //企业设置分发区域
  84. }
  85. }
  86. }
  87. // 企业未分配规则 商机管理用户查询个人
  88. data, ok := IC.Mgo.Find("entniche_rule", map[string]interface{}{
  89. "i_userid": v.Id,
  90. "i_entid": entId,
  91. "i_type": 1, //企业大会员
  92. }, "", nil, false, -1, 1)
  93. if !ok || data == nil || len(*data) == 0 {
  94. data, ok = IC.Mgo.Find("entniche_rule", map[string]interface{}{
  95. "i_userid": v.Id,
  96. "i_entid": entId,
  97. "i_type": 0, //企业商机管理
  98. }, "", nil, false, -1, 1)
  99. }
  100. if ok && data != nil {
  101. for _, value := range *data {
  102. o_entniche := common.ObjToMap(value["o_entniche"])
  103. o_area, _ := (*o_entniche)["o_area"].(map[string]interface{})
  104. if regionCheck(o_area, regions) {
  105. ss = append(ss, v)
  106. continue //商机管理
  107. }
  108. }
  109. }
  110. }
  111. }
  112. return ss
  113. }
  114. func regionCheck(o_area map[string]interface{}, regions []string) bool {
  115. if o_area == nil || len(o_area) == 0 {
  116. return true
  117. } else {
  118. regionBool := true
  119. for _, v1 := range regions {
  120. if _, ok1 := o_area[v1]; ok1 {
  121. continue
  122. } else {
  123. regionBool = false
  124. break
  125. }
  126. }
  127. return regionBool
  128. }
  129. }
  130. func departSort(ids *[]int, deptId int) {
  131. list := IC.MainMysql.FindOne("entniche_department", map[string]interface{}{"id": deptId}, "id,pid", "")
  132. if list != nil && len(*list) > 0 {
  133. pid := common.IntAll((*list)["pid"])
  134. if pid != 0 {
  135. departSort(ids, pid)
  136. *ids = append(*ids, pid)
  137. }
  138. }
  139. }
  140. // 数组倒序函数
  141. func Reverse(arr *[]int) {
  142. var temp int
  143. length := len(*arr)
  144. for i := 0; i < length/2; i++ {
  145. temp = (*arr)[i]
  146. (*arr)[i] = (*arr)[length-1-i]
  147. (*arr)[length-1-i] = temp
  148. }
  149. }