system.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package services
  2. import (
  3. "fmt"
  4. "hongze/hz_crm_api/models/company"
  5. "hongze/hz_crm_api/models/roadshow"
  6. "hongze/hz_crm_api/models/system"
  7. "hongze/hz_crm_api/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. func GetRoleTypeCode(roleType string) string {
  13. var roleTypeCode string
  14. switch roleType {
  15. case utils.ROLE_TYPE_ADMIN:
  16. roleTypeCode = utils.ROLE_TYPE_CODE_ADMIN
  17. case utils.ROLE_TYPE_FICC_ADMIN:
  18. roleTypeCode = utils.ROLE_TYPE_CODE_FICC_ADMIN
  19. case utils.ROLE_TYPE_FICC_SELLER:
  20. roleTypeCode = utils.ROLE_TYPE_CODE_FICC_SELLER
  21. case utils.ROLE_TYPE_RAI_ADMIN:
  22. roleTypeCode = utils.ROLE_TYPE_CODE_RAI_ADMIN
  23. case utils.ROLE_TYPE_FICC_GROUP, utils.ROLE_TYPE_FICC_MANAGER:
  24. roleTypeCode = utils.ROLE_TYPE_CODE_FICC_GROUP
  25. case utils.ROLE_TYPE_RAI_GROUP:
  26. roleTypeCode = utils.ROLE_TYPE_CODE_RAI_GROUP
  27. case utils.ROLE_TYPE_FICC_DEPARTMENT:
  28. roleTypeCode = utils.ROLE_TYPE_CODE_FICC_DEPARTMENT
  29. case utils.ROLE_TYPE_RAI_DEPARTMENT:
  30. roleTypeCode = utils.ROLE_TYPE_CODE_RAI_DEPARTMENT
  31. case utils.ROLE_TYPE_FICC_RESEARCHR:
  32. roleTypeCode = utils.ROLE_TYPE_CODE_FICC_RESEARCHR
  33. case utils.ROLE_TYPE_RAI_RESEARCHR:
  34. roleTypeCode = utils.ROLE_TYPE_CODE_RAI_RESEARCHR
  35. case utils.ROLE_TYPE_RAI_SELLER:
  36. roleTypeCode = utils.ROLE_TYPE_CODE_RAI_SELLER
  37. default:
  38. roleTypeCode = utils.ROLE_TYPE_ADMIN
  39. }
  40. return roleTypeCode
  41. }
  42. type AdminGroup struct {
  43. GroupId int `description:"分组id"`
  44. GroupName string `description:"分组名称"`
  45. ResearcherList []*roadshow.Researcher
  46. }
  47. // GetFiccSystemGroup 获取ficc的大分组
  48. func GetFiccSystemGroup() (adminGroupList []AdminGroup, groupMap map[int]int, err error) {
  49. list, err := system.GetSysGroupListByDepartmentId(2)
  50. if err != nil {
  51. return
  52. }
  53. groupMap = make(map[int]int)
  54. adminGroupList = make([]AdminGroup, 0)
  55. for _, v := range list {
  56. if v.ParentId == 0 {
  57. researcherList := make([]*roadshow.Researcher, 0)
  58. adminGroup := AdminGroup{
  59. GroupId: v.GroupId,
  60. GroupName: v.GroupName,
  61. ResearcherList: researcherList,
  62. }
  63. adminGroupList = append(adminGroupList, adminGroup)
  64. groupMap[v.GroupId] = v.GroupId
  65. } else {
  66. groupMap[v.GroupId] = v.ParentId
  67. }
  68. }
  69. return
  70. }
  71. // GetFiccSystemGroupIdsByGroupId 获取groupIp相关的分组ID
  72. func GetFiccSystemGroupIdsByGroupId(groupId int) (groupIds []int, err error) {
  73. list, err := system.GetSysGroupListByDepartmentId(2)
  74. if err != nil {
  75. return
  76. }
  77. groupMap := make(map[int]int) //整理三级分组对应的二级分组
  78. for _, v := range list {
  79. if v.ParentId == 0 {
  80. groupMap[v.GroupId] = v.GroupId
  81. } else {
  82. groupMap[v.GroupId] = v.ParentId
  83. }
  84. }
  85. parentId := 0
  86. for k, v := range groupMap {
  87. if k == groupId {
  88. parentId = v //找到当前相关的二级分组
  89. break
  90. }
  91. }
  92. if parentId > 0 { //根据二级分组,查三级和二级的组
  93. for k, v := range groupMap {
  94. if v == parentId {
  95. groupIds = append(groupIds, k)
  96. }
  97. }
  98. }
  99. return
  100. }
  101. // GetFiccSeller 获取ficc的销售(需要吧小分组的group_id给处理成大分组group_id)
  102. func GetFiccSeller(firstDate time.Time, groupMap map[int]int) (researcherList []*roadshow.Researcher, err error) {
  103. roleTypeCode := "('ficc_seller','ficc_group','ficc_team')"
  104. researcherList, err = roadshow.GetSellerList(roleTypeCode, "")
  105. if err != nil {
  106. return
  107. }
  108. for index, v := range researcherList {
  109. tmpGroupId, ok := groupMap[v.GroupId]
  110. if !ok {
  111. tmpGroupId = v.GroupId
  112. }
  113. researcherList[index].GroupId = tmpGroupId
  114. }
  115. return
  116. }
  117. // GetRaiSystemGroup 获取权益的大分组
  118. func GetRaiSystemGroup() (adminGroupList []AdminGroup, groupMap map[int]int, err error) {
  119. list, err := system.GetSysGroupListByDepartmentId(5)
  120. if err != nil {
  121. return
  122. }
  123. groupMap = make(map[int]int)
  124. adminGroupList = make([]AdminGroup, 0)
  125. for _, v := range list {
  126. if v.ParentId == 0 {
  127. researcherList := make([]*roadshow.Researcher, 0)
  128. adminGroup := AdminGroup{
  129. GroupId: v.GroupId,
  130. GroupName: v.GroupName,
  131. ResearcherList: researcherList,
  132. }
  133. adminGroupList = append(adminGroupList, adminGroup)
  134. groupMap[v.GroupId] = v.GroupId
  135. } else {
  136. groupMap[v.GroupId] = v.ParentId
  137. }
  138. }
  139. return
  140. }
  141. // GetRaiSeller 获取权益的销售(需要吧小分组的group_id给处理成大分组group_id)
  142. func GetRaiSeller(groupMap map[int]int) (researcherList []*roadshow.Researcher, err error) {
  143. roleTypeCode := "('rai_seller','rai_group')"
  144. researcherList, err = roadshow.GetRaiSellerList(roleTypeCode, "")
  145. if err != nil {
  146. return
  147. }
  148. for index, v := range researcherList {
  149. tmpGroupId, ok := groupMap[v.GroupId]
  150. if !ok {
  151. tmpGroupId = v.GroupId
  152. }
  153. researcherList[index].GroupId = tmpGroupId
  154. }
  155. return
  156. }
  157. // CheckAdminIsSameBigGroup 判断是否两个系统用户是否同一个大组内
  158. func CheckAdminIsSameBigGroup(adminInfo1, adminInfo2 *system.Admin) (isSame bool, err error) {
  159. // 如果销售和创建人是同一个小组
  160. if adminInfo1.GroupId == adminInfo2.GroupId {
  161. isSame = true
  162. return
  163. }
  164. var admin1BigGroupId, admin2BigGroupId int
  165. //获取第一个系统用户的大组id
  166. {
  167. //获取该账号的大组id
  168. pid, tmpErr := company.GetParentIdFromGroup(adminInfo1.GroupId)
  169. if tmpErr != nil {
  170. err = tmpErr
  171. return
  172. }
  173. if pid != nil && *pid > 0 {
  174. //该账号放在了三级分组下
  175. admin1BigGroupId = *pid
  176. } else {
  177. //该账号放在了二级分组下
  178. admin1BigGroupId = adminInfo1.GroupId
  179. }
  180. }
  181. //获取第二个系统用户的大组id
  182. {
  183. //获取该账号的大组id
  184. pid, tmpErr := company.GetParentIdFromGroup(adminInfo2.GroupId)
  185. if tmpErr != nil {
  186. err = tmpErr
  187. return
  188. }
  189. if pid != nil && *pid > 0 {
  190. //该账号放在了三级分组下
  191. admin2BigGroupId = *pid
  192. } else {
  193. //该账号放在了二级分组下
  194. admin2BigGroupId = adminInfo2.GroupId
  195. }
  196. }
  197. if admin1BigGroupId == admin2BigGroupId {
  198. isSame = true
  199. return
  200. }
  201. return
  202. }
  203. // GetBigGroupAllGroupInfo 根据分组id 获取该分组下所有关联的分组(包含上级分组和自己的分组)
  204. func GetBigGroupAllGroupInfo(groupId int) (groupMap map[int]*system.SysGroup, err error) {
  205. groupInfo, err := system.GetSysGroupById(groupId)
  206. if err != nil {
  207. return
  208. }
  209. groupMap = make(map[int]*system.SysGroup)
  210. if groupInfo.ParentId == 0 { //当前就是处于大分组下的第一级分组
  211. groupMap[groupInfo.GroupId] = groupInfo
  212. childSysGroupList, tmpErr := system.GetChildSysGroupByGroupId(groupInfo.GroupId)
  213. if tmpErr != nil {
  214. err = tmpErr
  215. return
  216. }
  217. for _, v := range childSysGroupList {
  218. groupMap[v.GroupId] = v
  219. }
  220. } else { //当前处于二级分组
  221. childSysGroupList, tmpErr := system.GetChildSysGroupByGroupId(groupInfo.ParentId)
  222. if tmpErr != nil {
  223. err = tmpErr
  224. return
  225. }
  226. for _, v := range childSysGroupList {
  227. groupMap[v.GroupId] = v
  228. }
  229. //父级分组信息
  230. parentGroupInfo, tmpErr := system.GetSysGroupById(groupInfo.ParentId)
  231. if tmpErr != nil {
  232. err = tmpErr
  233. return
  234. }
  235. groupMap[groupInfo.GroupId] = parentGroupInfo
  236. }
  237. return
  238. }
  239. // GetBigGroupByFiccGroupId 根据主管分组id获取用户的大组
  240. func GetBigGroupByFiccGroupId(groupId int) (groupMap map[int]*system.SysGroup, err error) {
  241. bigGroupId := 0
  242. //获取该账号的大组id
  243. pid, err := company.GetParentIdFromGroup(groupId)
  244. if err != nil {
  245. return
  246. }
  247. if pid != nil && *pid > 0 {
  248. //该账号放在了三级分组下
  249. bigGroupId = *pid
  250. } else {
  251. //该账号放在了二级分组下
  252. bigGroupId = groupId
  253. }
  254. groupMap, err = GetBigGroupAllGroupInfo(bigGroupId)
  255. return
  256. }
  257. // CheckRaiAdmin 判断是否为权益内部人员
  258. func CheckRaiAdmin(adminId int) (isRai bool, err error) {
  259. raiAdmins, e := system.GetRaiAdmin()
  260. if e != nil {
  261. err = fmt.Errorf("获取权益内部人员失败, GetRaiAdmin Err: %s", e.Error())
  262. return
  263. }
  264. for _, r := range raiAdmins {
  265. if adminId == r.AdminId {
  266. isRai = true
  267. break
  268. }
  269. }
  270. return
  271. }
  272. func GetFiccChildGroupIdsByGroupIds(groupId int) (sid string, sellerIds string, err error) {
  273. pid, err := company.GetParentIdFromGroup(groupId)
  274. if err != nil {
  275. return
  276. }
  277. var ids []*string
  278. if pid != nil {
  279. if *pid != 0 {
  280. //销售主管放在三级列表中
  281. ids, err = company.GetGroupIdsByParentId(*pid)
  282. if err != nil {
  283. fmt.Println(err.Error())
  284. return
  285. }
  286. } else {
  287. //销售主管放在二级列表中
  288. ids, err = company.GetGroupIdsByParentId(groupId)
  289. if err != nil {
  290. fmt.Println(err.Error())
  291. return
  292. }
  293. }
  294. }
  295. var idSlice []string
  296. for _, id := range ids {
  297. idSlice = append(idSlice, *id)
  298. }
  299. //加入父级groupId
  300. if *pid > 0 {
  301. idSlice = append(idSlice, strconv.Itoa(*pid))
  302. } else {
  303. idSlice = append(idSlice, strconv.Itoa(groupId))
  304. }
  305. sid = strings.Join(idSlice, ",")
  306. //查询这些组内的所有seller_ids
  307. if len(idSlice) > 0 {
  308. admins, tErr := system.GetTeamSysUserListByIds(sid)
  309. if tErr != nil {
  310. err = tErr
  311. fmt.Println(tErr.Error())
  312. return
  313. }
  314. var sellerIdSlice []string
  315. for _, v := range admins {
  316. sellerIdSlice = append(sellerIdSlice, strconv.Itoa(v.AdminId))
  317. }
  318. sellerIds = strings.Join(sellerIdSlice, ",")
  319. }
  320. return
  321. }
  322. func GetFiccSellerIdsByGroupId(groupId int) (sellerIds string, err error) {
  323. //查询这些组内的所有seller_ids
  324. if groupId > 0 {
  325. admins, tErr := system.GetAdminByGroupId(groupId)
  326. if tErr != nil {
  327. err = tErr
  328. fmt.Println(tErr.Error())
  329. return
  330. }
  331. var sellerIdSlice []string
  332. for _, v := range admins {
  333. sellerIdSlice = append(sellerIdSlice, strconv.Itoa(v.AdminId))
  334. }
  335. sellerIds = strings.Join(sellerIdSlice, ",")
  336. }
  337. return
  338. }