company_seller.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package crm
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/fms_api/models/crm"
  6. )
  7. // GetSellerDepartmentAndGroupMap 获取销售部门列表-包含大组和小组
  8. func GetSellerDepartmentListWithGroupAndTeam() (sellerList []*crm.SellerAdminWithGroupTeam, err error) {
  9. departmentIds := []int{crm.SellerDepartmentId, crm.RaiSellerDepartmentId}
  10. sellerList = make([]*crm.SellerAdminWithGroupTeam, 0)
  11. // 获取组别列表
  12. groupCond := "department_id in (?)"
  13. groupPars := make([]interface{}, 0)
  14. groupPars = append(groupPars, departmentIds)
  15. groupOB := new(crm.SysGroup)
  16. groupList, e := groupOB.List(groupCond, groupPars)
  17. if e != nil {
  18. err = errors.New("获取组别列表失败, Err: " + e.Error())
  19. return
  20. }
  21. groupMap := make(map[int]*crm.SysGroup)
  22. for i := range groupList {
  23. groupMap[groupList[i].GroupId] = groupList[i]
  24. }
  25. // 获取销售部门所有销售信息
  26. adminCond := "department_id in (?) AND enabled = 1"
  27. adminPars := make([]interface{}, 0)
  28. adminPars = append(adminPars, departmentIds)
  29. adminOB := new(crm.Admin)
  30. adminList, e := adminOB.List(adminCond, adminPars)
  31. if e != nil {
  32. err = errors.New("获取销售列表失败, Err: " + e.Error())
  33. return
  34. }
  35. // 销售列表
  36. for i := range adminList {
  37. v := new(crm.SellerAdminWithGroupTeam)
  38. v.DepartmentId = adminList[i].DepartmentId
  39. v.SellerId = adminList[i].AdminId
  40. v.SellerName = adminList[i].RealName
  41. g := groupMap[adminList[i].GroupId]
  42. if g != nil {
  43. if g.ParentId > 0 {
  44. // 三级分组
  45. p := groupMap[g.ParentId]
  46. if p != nil {
  47. v.GroupId = p.GroupId
  48. v.GroupName = p.GroupName
  49. v.TeamId = g.GroupId
  50. v.TeamName = g.GroupName
  51. }
  52. } else {
  53. // 二级分组
  54. v.GroupId = g.GroupId
  55. v.GroupName = g.GroupName
  56. }
  57. }
  58. sellerList = append(sellerList, v)
  59. }
  60. return
  61. }
  62. // GetSellerTeamGroupMap 获取销售组别对应的大组信息
  63. func GetSellerTeamGroupMap() (teamGroupMap map[int]*crm.SysGroup, err error) {
  64. teamGroupMap = make(map[int]*crm.SysGroup)
  65. groupCond := `department_id IN (?,?)`
  66. groupPars := make([]interface{}, 0)
  67. groupPars = append(groupPars, crm.SellerDepartmentId, crm.RaiSellerDepartmentId)
  68. groupOB := new(crm.SysGroup)
  69. groupList, e := groupOB.List(groupCond, groupPars)
  70. if e != nil {
  71. err = errors.New("获取组别列表失败, Err: " + e.Error())
  72. return
  73. }
  74. groupMap := make(map[int]*crm.SysGroup)
  75. for i := range groupList {
  76. groupMap[groupList[i].GroupId] = groupList[i]
  77. }
  78. for i := range groupList {
  79. // 大组对应的是自己
  80. if groupList[i].ParentId == 0 {
  81. teamGroupMap[groupList[i].GroupId] = groupList[i]
  82. continue
  83. }
  84. teamGroupMap[groupList[i].GroupId] = groupMap[groupList[i].ParentId]
  85. }
  86. return
  87. }
  88. // GetSellerDepartmentList 获取销售部门列表-大组
  89. func GetSellerDepartmentList() (resp crm.SellerAdminWithGroupList, err error) {
  90. // 获取组别列表
  91. departmentIds := []int{crm.SellerDepartmentId, crm.RaiSellerDepartmentId}
  92. groupCond := "department_id in (?)"
  93. groupPars := make([]interface{}, 0)
  94. groupPars = append(groupPars, departmentIds)
  95. groupOB := new(crm.SysGroup)
  96. groupList, e := groupOB.List(groupCond, groupPars)
  97. if e != nil {
  98. err = errors.New("获取组别列表失败, Err: " + e.Error())
  99. return
  100. }
  101. groupMap := make(map[int]*crm.SysGroup)
  102. teamMap := make(map[int]*crm.SysGroup)
  103. for i := range groupList {
  104. if groupList[i].ParentId > 0 {
  105. teamMap[groupList[i].GroupId] = groupList[i]
  106. } else {
  107. groupMap[groupList[i].GroupId] = groupList[i]
  108. }
  109. }
  110. // 获取销售部门所有销售信息
  111. adminCond := "department_id in (?) "
  112. adminPars := make([]interface{}, 0)
  113. adminPars = append(adminPars, departmentIds)
  114. adminOB := new(crm.Admin)
  115. adminList, e := adminOB.List(adminCond, adminPars)
  116. if e != nil {
  117. err = errors.New("获取销售列表失败, Err: " + e.Error())
  118. return
  119. }
  120. // 销售列表
  121. departmentMap := make(map[int][]crm.SellerAdminWithGroupTree)
  122. groupSeller := make(map[int][]crm.SellerAdminWithGroupTree)
  123. ficcList := make([]crm.SellerAdminList, 0)
  124. raiList := make([]crm.SellerAdminList, 0)
  125. for i := range adminList {
  126. var productId int
  127. v := crm.SellerAdminList{
  128. SellerId: adminList[i].AdminId,
  129. SellerName: adminList[i].RealName,
  130. }
  131. if adminList[i].DepartmentId == crm.SellerDepartmentId {
  132. ficcList = append(ficcList, v)
  133. productId = 1
  134. } else if adminList[i].DepartmentId == crm.RaiSellerDepartmentId {
  135. raiList = append(raiList, v)
  136. productId = 2
  137. }
  138. if t, ok := teamMap[adminList[i].GroupId]; ok { // 如果销售是在三级分组下面,则找到对应的二级分组
  139. if g, ok1 := groupMap[t.ParentId]; ok1 {
  140. tmp := crm.SellerAdminWithGroupTree{
  141. SellerId: adminList[i].AdminId,
  142. SellerName: adminList[i].RealName,
  143. ProductId: productId,
  144. Child: nil,
  145. }
  146. groupSeller[g.GroupId] = append(groupSeller[g.GroupId], tmp)
  147. } else {
  148. fmt.Println("adminList[i]:", adminList[i].AdminId)
  149. err = errors.New("找不到对应的销售分组")
  150. return
  151. }
  152. } else {
  153. if g, ok1 := groupMap[adminList[i].GroupId]; ok1 {
  154. tmp := crm.SellerAdminWithGroupTree{
  155. SellerId: adminList[i].AdminId,
  156. SellerName: adminList[i].RealName,
  157. ProductId: productId,
  158. Child: nil,
  159. }
  160. groupSeller[g.GroupId] = append(groupSeller[g.GroupId], tmp)
  161. } else {
  162. tmp := crm.SellerAdminWithGroupTree{
  163. SellerId: adminList[i].AdminId,
  164. SellerName: adminList[i].RealName,
  165. ProductId: productId,
  166. Child: nil,
  167. }
  168. departmentMap[adminList[i].DepartmentId] = append(departmentMap[adminList[i].DepartmentId], tmp)
  169. }
  170. }
  171. }
  172. //分组
  173. for k, v := range groupMap {
  174. child, _ := groupSeller[k]
  175. tmp := crm.SellerAdminWithGroupTree{
  176. SellerId: k+10000,
  177. SellerName: v.GroupName,
  178. Child: child,
  179. }
  180. if v.DepartmentId == crm.SellerDepartmentId {
  181. tmp.ProductId = 1
  182. } else if v.DepartmentId == crm.RaiSellerDepartmentId {
  183. tmp.ProductId = 2
  184. }
  185. departmentMap[v.DepartmentId] = append(departmentMap[v.DepartmentId], tmp)
  186. }
  187. //分部门
  188. allTitleMap := map[int]string{
  189. crm.SellerDepartmentId: "FICC销售",
  190. crm.RaiSellerDepartmentId: "权益销售",
  191. }
  192. ficcTree, _ := departmentMap[crm.SellerDepartmentId]
  193. raiTree, _ := departmentMap[crm.RaiSellerDepartmentId]
  194. tmp1 := crm.SellerAdminWithGroupTree{
  195. SellerId: crm.SellerDepartmentId,
  196. SellerName: allTitleMap[crm.SellerDepartmentId],
  197. ProductId: 1,
  198. Child: ficcTree,
  199. }
  200. tmp2 := crm.SellerAdminWithGroupTree{
  201. SellerId: crm.RaiSellerDepartmentId,
  202. SellerName: allTitleMap[crm.RaiSellerDepartmentId],
  203. ProductId: 2,
  204. Child: raiTree,
  205. }
  206. allList := make([]crm.SellerAdminWithGroupTree, 0)
  207. allList = append(allList, tmp1)
  208. allList = append(allList, tmp2)
  209. resp.AllList = allList
  210. resp.FiccList = ficcList
  211. resp.RaiList = raiList
  212. return
  213. }
  214. // GetSellerDepartmentAndGroupMap 获取销售部门列表-包含大组和小组
  215. func GetSellerDepartmentListWithGroupAndTeamByDepartment() (ficcList []*crm.SellerAdminWithGroupTeam, raiList []*crm.SellerAdminWithGroupTeam, err error) {
  216. departmentIds := []int{crm.SellerDepartmentId, crm.RaiSellerDepartmentId}
  217. ficcList = make([]*crm.SellerAdminWithGroupTeam, 0)
  218. raiList = make([]*crm.SellerAdminWithGroupTeam, 0)
  219. // 获取组别列表
  220. groupCond := "department_id in (?)"
  221. groupPars := make([]interface{}, 0)
  222. groupPars = append(groupPars, departmentIds)
  223. groupOB := new(crm.SysGroup)
  224. groupList, e := groupOB.List(groupCond, groupPars)
  225. if e != nil {
  226. err = errors.New("获取组别列表失败, Err: " + e.Error())
  227. return
  228. }
  229. groupMap := make(map[int]*crm.SysGroup)
  230. for i := range groupList {
  231. groupMap[groupList[i].GroupId] = groupList[i]
  232. }
  233. // 获取销售部门所有销售信息
  234. adminCond := "department_id in (?) AND enabled = 1"
  235. adminPars := make([]interface{}, 0)
  236. adminPars = append(adminPars, departmentIds)
  237. adminOB := new(crm.Admin)
  238. adminList, e := adminOB.List(adminCond, adminPars)
  239. if e != nil {
  240. err = errors.New("获取销售列表失败, Err: " + e.Error())
  241. return
  242. }
  243. // 销售列表
  244. for i := range adminList {
  245. v := new(crm.SellerAdminWithGroupTeam)
  246. v.DepartmentId = adminList[i].DepartmentId
  247. v.SellerId = adminList[i].AdminId
  248. v.SellerName = adminList[i].RealName
  249. g := groupMap[adminList[i].GroupId]
  250. if g != nil {
  251. if g.ParentId > 0 {
  252. // 三级分组
  253. p := groupMap[g.ParentId]
  254. if p != nil {
  255. v.GroupId = p.GroupId
  256. v.GroupName = p.GroupName
  257. v.TeamId = g.GroupId
  258. v.TeamName = g.GroupName
  259. }
  260. } else {
  261. // 二级分组
  262. v.GroupId = g.GroupId
  263. v.GroupName = g.GroupName
  264. }
  265. }
  266. if v.DepartmentId == crm.SellerDepartmentId {
  267. ficcList = append(ficcList, v)
  268. } else {
  269. raiList = append(raiList, v)
  270. }
  271. }
  272. return
  273. }
  274. // GetSellerList 获取销售部门列表-大组和小组
  275. func GetSellerList() (resp crm.SellerAdminWithGroupList, err error) {
  276. // 获取组别列表
  277. departmentIds := []int{crm.SellerDepartmentId, crm.RaiSellerDepartmentId}
  278. groupCond := "department_id in (?)"
  279. groupPars := make([]interface{}, 0)
  280. groupPars = append(groupPars, departmentIds)
  281. groupOB := new(crm.SysGroup)
  282. fullGroups, e := groupOB.List(groupCond, groupPars)
  283. if e != nil {
  284. err = errors.New("获取组别列表失败, Err: " + e.Error())
  285. return
  286. }
  287. fullGroupMap := make(map[int]*crm.SysGroup)
  288. for _, v := range fullGroups {
  289. fullGroupMap[v.GroupId] = v
  290. }
  291. // 获取销售部门所有销售信息
  292. adminCond := "department_id in (?) "
  293. adminPars := make([]interface{}, 0)
  294. adminPars = append(adminPars, departmentIds)
  295. adminOB := new(crm.Admin)
  296. adminList, e := adminOB.List(adminCond, adminPars)
  297. if e != nil {
  298. err = errors.New("获取销售列表失败, Err: " + e.Error())
  299. return
  300. }
  301. var listGroup []crm.SellerAdminWithGroupTree
  302. //var listDepartment []crm.SellerAdminWithGroupTree
  303. departmentListMap := make(map[int][]crm.SellerAdminWithGroupTree)
  304. groupListMap := make(map[int][]crm.SellerAdminWithGroupTree)
  305. teamListMap := make(map[int][]crm.SellerAdminWithGroupTree)
  306. //departmentHasMap := make(map[int]bool)
  307. groupHasMap := make(map[int]bool)
  308. teamHasMap := make(map[int]bool)
  309. for _, v := range adminList {
  310. tmp := crm.SellerAdminWithGroupTree{
  311. SellerId: v.AdminId,
  312. SellerName: v.RealName,
  313. }
  314. if v.GroupId > 0 {
  315. if groupInfo, ok := fullGroupMap[v.GroupId]; ok {
  316. if groupInfo.ParentId > 0 {
  317. teamListMap[v.GroupId] = append(teamListMap[v.GroupId], tmp)
  318. } else {
  319. groupListMap[groupInfo.GroupId] = append(groupListMap[groupInfo.GroupId], tmp)
  320. }
  321. } else {
  322. departmentListMap[v.DepartmentId] = append(departmentListMap[v.DepartmentId], tmp)
  323. }
  324. } else if v.DepartmentId > 0 {
  325. departmentListMap[v.DepartmentId] = append(departmentListMap[v.DepartmentId], tmp)
  326. }
  327. }
  328. for _, groupInfo := range fullGroups {
  329. var team1 crm.SellerAdminWithGroupTree
  330. //处理小组
  331. if groupInfo.ParentId > 0 {
  332. if _, ok2 := teamHasMap[groupInfo.GroupId]; !ok2 {
  333. if len(teamListMap[groupInfo.GroupId]) > 0 {
  334. team1 = crm.SellerAdminWithGroupTree{
  335. SellerId: groupInfo.GroupId,
  336. SellerName: groupInfo.GroupName,
  337. Child: teamListMap[groupInfo.GroupId],
  338. }
  339. teamHasMap[groupInfo.GroupId] = true
  340. groupListMap[groupInfo.ParentId] = append(groupListMap[groupInfo.ParentId], team1)
  341. }
  342. }
  343. }
  344. }
  345. for _, groupInfo := range fullGroups {
  346. var group1 crm.SellerAdminWithGroupTree
  347. //处理大组
  348. if groupInfo.ParentId == 0 {
  349. if _, ok2 := groupHasMap[groupInfo.GroupId]; !ok2 {
  350. if len(groupListMap[groupInfo.GroupId]) > 0 {
  351. group1 = crm.SellerAdminWithGroupTree{
  352. SellerId: groupInfo.GroupId,
  353. SellerName: groupInfo.GroupName,
  354. Child: groupListMap[groupInfo.GroupId],
  355. }
  356. groupHasMap[groupInfo.GroupId] = true
  357. departmentListMap[groupInfo.DepartmentId] = append(departmentListMap[groupInfo.DepartmentId], group1)
  358. listGroup = append(listGroup, group1)
  359. }
  360. }
  361. }
  362. }
  363. //分部门
  364. allTitleMap := map[int]string{
  365. crm.SellerDepartmentId: "FICC销售",
  366. crm.RaiSellerDepartmentId: "权益销售",
  367. }
  368. tmp1 := crm.SellerAdminWithGroupTree{
  369. SellerId: crm.SellerDepartmentId,
  370. SellerName: allTitleMap[crm.SellerDepartmentId],
  371. Child: departmentListMap[crm.SellerDepartmentId],
  372. }
  373. tmp2 := crm.SellerAdminWithGroupTree{
  374. SellerId: crm.RaiSellerDepartmentId,
  375. SellerName: allTitleMap[crm.RaiSellerDepartmentId],
  376. Child: departmentListMap[crm.RaiSellerDepartmentId],
  377. }
  378. allList := make([]crm.SellerAdminWithGroupTree, 0)
  379. allList = append(allList, tmp1)
  380. allList = append(allList, tmp2)
  381. resp.AllList = allList
  382. return
  383. }
  384. // 获取销售部门列表-包含禁用
  385. func GetSellerDepartmentListWithEnable() (sellerList []*crm.SellerAdminWithGroupTeam, err error) {
  386. departmentIds := []int{crm.SellerDepartmentId, crm.RaiSellerDepartmentId}
  387. sellerList = make([]*crm.SellerAdminWithGroupTeam, 0)
  388. // 获取组别列表
  389. groupCond := "department_id in (?)"
  390. groupPars := make([]interface{}, 0)
  391. groupPars = append(groupPars, departmentIds)
  392. groupOB := new(crm.SysGroup)
  393. groupList, e := groupOB.List(groupCond, groupPars)
  394. if e != nil {
  395. err = errors.New("获取组别列表失败, Err: " + e.Error())
  396. return
  397. }
  398. groupMap := make(map[int]*crm.SysGroup)
  399. for i := range groupList {
  400. groupMap[groupList[i].GroupId] = groupList[i]
  401. }
  402. // 获取销售部门所有销售信息
  403. adminCond := "department_id in (?) "
  404. adminPars := make([]interface{}, 0)
  405. adminPars = append(adminPars, departmentIds)
  406. adminOB := new(crm.Admin)
  407. adminList, e := adminOB.List(adminCond, adminPars)
  408. if e != nil {
  409. err = errors.New("获取销售列表失败, Err: " + e.Error())
  410. return
  411. }
  412. // 销售列表
  413. for i := range adminList {
  414. v := new(crm.SellerAdminWithGroupTeam)
  415. v.DepartmentId = adminList[i].DepartmentId
  416. v.SellerId = adminList[i].AdminId
  417. v.SellerName = adminList[i].RealName
  418. g := groupMap[adminList[i].GroupId]
  419. if g != nil {
  420. if g.ParentId > 0 {
  421. // 三级分组
  422. p := groupMap[g.ParentId]
  423. if p != nil {
  424. v.GroupId = p.GroupId
  425. v.GroupName = p.GroupName
  426. v.TeamId = g.GroupId
  427. v.TeamName = g.GroupName
  428. }
  429. } else {
  430. // 二级分组
  431. v.GroupId = g.GroupId
  432. v.GroupName = g.GroupName
  433. }
  434. }
  435. sellerList = append(sellerList, v)
  436. }
  437. return
  438. }