123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- package crm
- import (
- "errors"
- "fmt"
- "hongze/fms_api/models/crm"
- )
- // GetSellerDepartmentAndGroupMap 获取销售部门列表-包含大组和小组
- func GetSellerDepartmentListWithGroupAndTeam() (sellerList []*crm.SellerAdminWithGroupTeam, err error) {
- departmentIds := []int{crm.SellerDepartmentId, crm.RaiSellerDepartmentId}
- sellerList = make([]*crm.SellerAdminWithGroupTeam, 0)
- // 获取组别列表
- groupCond := "department_id in (?)"
- groupPars := make([]interface{}, 0)
- groupPars = append(groupPars, departmentIds)
- groupOB := new(crm.SysGroup)
- groupList, e := groupOB.List(groupCond, groupPars)
- if e != nil {
- err = errors.New("获取组别列表失败, Err: " + e.Error())
- return
- }
- groupMap := make(map[int]*crm.SysGroup)
- for i := range groupList {
- groupMap[groupList[i].GroupId] = groupList[i]
- }
- // 获取销售部门所有销售信息
- adminCond := "department_id in (?) AND enabled = 1"
- adminPars := make([]interface{}, 0)
- adminPars = append(adminPars, departmentIds)
- adminOB := new(crm.Admin)
- adminList, e := adminOB.List(adminCond, adminPars)
- if e != nil {
- err = errors.New("获取销售列表失败, Err: " + e.Error())
- return
- }
- // 销售列表
- for i := range adminList {
- v := new(crm.SellerAdminWithGroupTeam)
- v.DepartmentId = adminList[i].DepartmentId
- v.SellerId = adminList[i].AdminId
- v.SellerName = adminList[i].RealName
- g := groupMap[adminList[i].GroupId]
- if g != nil {
- if g.ParentId > 0 {
- // 三级分组
- p := groupMap[g.ParentId]
- if p != nil {
- v.GroupId = p.GroupId
- v.GroupName = p.GroupName
- v.TeamId = g.GroupId
- v.TeamName = g.GroupName
- }
- } else {
- // 二级分组
- v.GroupId = g.GroupId
- v.GroupName = g.GroupName
- }
- }
- sellerList = append(sellerList, v)
- }
- return
- }
- // GetSellerTeamGroupMap 获取销售组别对应的大组信息
- func GetSellerTeamGroupMap() (teamGroupMap map[int]*crm.SysGroup, err error) {
- teamGroupMap = make(map[int]*crm.SysGroup)
- groupCond := `department_id IN (?,?)`
- groupPars := make([]interface{}, 0)
- groupPars = append(groupPars, crm.SellerDepartmentId, crm.RaiSellerDepartmentId)
- groupOB := new(crm.SysGroup)
- groupList, e := groupOB.List(groupCond, groupPars)
- if e != nil {
- err = errors.New("获取组别列表失败, Err: " + e.Error())
- return
- }
- groupMap := make(map[int]*crm.SysGroup)
- for i := range groupList {
- groupMap[groupList[i].GroupId] = groupList[i]
- }
- for i := range groupList {
- // 大组对应的是自己
- if groupList[i].ParentId == 0 {
- teamGroupMap[groupList[i].GroupId] = groupList[i]
- continue
- }
- teamGroupMap[groupList[i].GroupId] = groupMap[groupList[i].ParentId]
- }
- return
- }
- // GetSellerDepartmentList 获取销售部门列表-大组
- func GetSellerDepartmentList() (resp crm.SellerAdminWithGroupList, err error) {
- // 获取组别列表
- departmentIds := []int{crm.SellerDepartmentId, crm.RaiSellerDepartmentId}
- groupCond := "department_id in (?)"
- groupPars := make([]interface{}, 0)
- groupPars = append(groupPars, departmentIds)
- groupOB := new(crm.SysGroup)
- groupList, e := groupOB.List(groupCond, groupPars)
- if e != nil {
- err = errors.New("获取组别列表失败, Err: " + e.Error())
- return
- }
- groupMap := make(map[int]*crm.SysGroup)
- teamMap := make(map[int]*crm.SysGroup)
- for i := range groupList {
- if groupList[i].ParentId > 0 {
- teamMap[groupList[i].GroupId] = groupList[i]
- } else {
- groupMap[groupList[i].GroupId] = groupList[i]
- }
- }
- // 获取销售部门所有销售信息
- adminCond := "department_id in (?) "
- adminPars := make([]interface{}, 0)
- adminPars = append(adminPars, departmentIds)
- adminOB := new(crm.Admin)
- adminList, e := adminOB.List(adminCond, adminPars)
- if e != nil {
- err = errors.New("获取销售列表失败, Err: " + e.Error())
- return
- }
- // 销售列表
- departmentMap := make(map[int][]crm.SellerAdminWithGroupTree)
- groupSeller := make(map[int][]crm.SellerAdminWithGroupTree)
- ficcList := make([]crm.SellerAdminList, 0)
- raiList := make([]crm.SellerAdminList, 0)
- for i := range adminList {
- var productId int
- v := crm.SellerAdminList{
- SellerId: adminList[i].AdminId,
- SellerName: adminList[i].RealName,
- }
- if adminList[i].DepartmentId == crm.SellerDepartmentId {
- ficcList = append(ficcList, v)
- productId = 1
- } else if adminList[i].DepartmentId == crm.RaiSellerDepartmentId {
- raiList = append(raiList, v)
- productId = 2
- }
- if t, ok := teamMap[adminList[i].GroupId]; ok { // 如果销售是在三级分组下面,则找到对应的二级分组
- if g, ok1 := groupMap[t.ParentId]; ok1 {
- tmp := crm.SellerAdminWithGroupTree{
- SellerId: adminList[i].AdminId,
- SellerName: adminList[i].RealName,
- ProductId: productId,
- Child: nil,
- }
- groupSeller[g.GroupId] = append(groupSeller[g.GroupId], tmp)
- } else {
- fmt.Println("adminList[i]:", adminList[i].AdminId)
- err = errors.New("找不到对应的销售分组")
- return
- }
- } else {
- if g, ok1 := groupMap[adminList[i].GroupId]; ok1 {
- tmp := crm.SellerAdminWithGroupTree{
- SellerId: adminList[i].AdminId,
- SellerName: adminList[i].RealName,
- ProductId: productId,
- Child: nil,
- }
- groupSeller[g.GroupId] = append(groupSeller[g.GroupId], tmp)
- } else {
- tmp := crm.SellerAdminWithGroupTree{
- SellerId: adminList[i].AdminId,
- SellerName: adminList[i].RealName,
- ProductId: productId,
- Child: nil,
- }
- departmentMap[adminList[i].DepartmentId] = append(departmentMap[adminList[i].DepartmentId], tmp)
- }
- }
- }
- //分组
- for k, v := range groupMap {
- child, _ := groupSeller[k]
- tmp := crm.SellerAdminWithGroupTree{
- SellerId: k+10000,
- SellerName: v.GroupName,
- Child: child,
- }
- if v.DepartmentId == crm.SellerDepartmentId {
- tmp.ProductId = 1
- } else if v.DepartmentId == crm.RaiSellerDepartmentId {
- tmp.ProductId = 2
- }
- departmentMap[v.DepartmentId] = append(departmentMap[v.DepartmentId], tmp)
- }
- //分部门
- allTitleMap := map[int]string{
- crm.SellerDepartmentId: "FICC销售",
- crm.RaiSellerDepartmentId: "权益销售",
- }
- ficcTree, _ := departmentMap[crm.SellerDepartmentId]
- raiTree, _ := departmentMap[crm.RaiSellerDepartmentId]
- tmp1 := crm.SellerAdminWithGroupTree{
- SellerId: crm.SellerDepartmentId,
- SellerName: allTitleMap[crm.SellerDepartmentId],
- ProductId: 1,
- Child: ficcTree,
- }
- tmp2 := crm.SellerAdminWithGroupTree{
- SellerId: crm.RaiSellerDepartmentId,
- SellerName: allTitleMap[crm.RaiSellerDepartmentId],
- ProductId: 2,
- Child: raiTree,
- }
- allList := make([]crm.SellerAdminWithGroupTree, 0)
- allList = append(allList, tmp1)
- allList = append(allList, tmp2)
- resp.AllList = allList
- resp.FiccList = ficcList
- resp.RaiList = raiList
- return
- }
- // GetSellerDepartmentAndGroupMap 获取销售部门列表-包含大组和小组
- func GetSellerDepartmentListWithGroupAndTeamByDepartment() (ficcList []*crm.SellerAdminWithGroupTeam, raiList []*crm.SellerAdminWithGroupTeam, err error) {
- departmentIds := []int{crm.SellerDepartmentId, crm.RaiSellerDepartmentId}
- ficcList = make([]*crm.SellerAdminWithGroupTeam, 0)
- raiList = make([]*crm.SellerAdminWithGroupTeam, 0)
- // 获取组别列表
- groupCond := "department_id in (?)"
- groupPars := make([]interface{}, 0)
- groupPars = append(groupPars, departmentIds)
- groupOB := new(crm.SysGroup)
- groupList, e := groupOB.List(groupCond, groupPars)
- if e != nil {
- err = errors.New("获取组别列表失败, Err: " + e.Error())
- return
- }
- groupMap := make(map[int]*crm.SysGroup)
- for i := range groupList {
- groupMap[groupList[i].GroupId] = groupList[i]
- }
- // 获取销售部门所有销售信息
- adminCond := "department_id in (?) AND enabled = 1"
- adminPars := make([]interface{}, 0)
- adminPars = append(adminPars, departmentIds)
- adminOB := new(crm.Admin)
- adminList, e := adminOB.List(adminCond, adminPars)
- if e != nil {
- err = errors.New("获取销售列表失败, Err: " + e.Error())
- return
- }
- // 销售列表
- for i := range adminList {
- v := new(crm.SellerAdminWithGroupTeam)
- v.DepartmentId = adminList[i].DepartmentId
- v.SellerId = adminList[i].AdminId
- v.SellerName = adminList[i].RealName
- g := groupMap[adminList[i].GroupId]
- if g != nil {
- if g.ParentId > 0 {
- // 三级分组
- p := groupMap[g.ParentId]
- if p != nil {
- v.GroupId = p.GroupId
- v.GroupName = p.GroupName
- v.TeamId = g.GroupId
- v.TeamName = g.GroupName
- }
- } else {
- // 二级分组
- v.GroupId = g.GroupId
- v.GroupName = g.GroupName
- }
- }
- if v.DepartmentId == crm.SellerDepartmentId {
- ficcList = append(ficcList, v)
- } else {
- raiList = append(raiList, v)
- }
- }
- return
- }
- // GetSellerList 获取销售部门列表-大组和小组
- func GetSellerList() (resp crm.SellerAdminWithGroupList, err error) {
- // 获取组别列表
- departmentIds := []int{crm.SellerDepartmentId, crm.RaiSellerDepartmentId}
- groupCond := "department_id in (?)"
- groupPars := make([]interface{}, 0)
- groupPars = append(groupPars, departmentIds)
- groupOB := new(crm.SysGroup)
- fullGroups, e := groupOB.List(groupCond, groupPars)
- if e != nil {
- err = errors.New("获取组别列表失败, Err: " + e.Error())
- return
- }
- fullGroupMap := make(map[int]*crm.SysGroup)
- for _, v := range fullGroups {
- fullGroupMap[v.GroupId] = v
- }
- // 获取销售部门所有销售信息
- adminCond := "department_id in (?) "
- adminPars := make([]interface{}, 0)
- adminPars = append(adminPars, departmentIds)
- adminOB := new(crm.Admin)
- adminList, e := adminOB.List(adminCond, adminPars)
- if e != nil {
- err = errors.New("获取销售列表失败, Err: " + e.Error())
- return
- }
- var listGroup []crm.SellerAdminWithGroupTree
- //var listDepartment []crm.SellerAdminWithGroupTree
- departmentListMap := make(map[int][]crm.SellerAdminWithGroupTree)
- groupListMap := make(map[int][]crm.SellerAdminWithGroupTree)
- teamListMap := make(map[int][]crm.SellerAdminWithGroupTree)
- //departmentHasMap := make(map[int]bool)
- groupHasMap := make(map[int]bool)
- teamHasMap := make(map[int]bool)
- for _, v := range adminList {
- tmp := crm.SellerAdminWithGroupTree{
- SellerId: v.AdminId,
- SellerName: v.RealName,
- }
- if v.GroupId > 0 {
- if groupInfo, ok := fullGroupMap[v.GroupId]; ok {
- if groupInfo.ParentId > 0 {
- teamListMap[v.GroupId] = append(teamListMap[v.GroupId], tmp)
- } else {
- groupListMap[groupInfo.GroupId] = append(groupListMap[groupInfo.GroupId], tmp)
- }
- } else {
- departmentListMap[v.DepartmentId] = append(departmentListMap[v.DepartmentId], tmp)
- }
- } else if v.DepartmentId > 0 {
- departmentListMap[v.DepartmentId] = append(departmentListMap[v.DepartmentId], tmp)
- }
- }
- for _, groupInfo := range fullGroups {
- var team1 crm.SellerAdminWithGroupTree
- //处理小组
- if groupInfo.ParentId > 0 {
- if _, ok2 := teamHasMap[groupInfo.GroupId]; !ok2 {
- if len(teamListMap[groupInfo.GroupId]) > 0 {
- team1 = crm.SellerAdminWithGroupTree{
- SellerId: groupInfo.GroupId,
- SellerName: groupInfo.GroupName,
- Child: teamListMap[groupInfo.GroupId],
- }
- teamHasMap[groupInfo.GroupId] = true
- groupListMap[groupInfo.ParentId] = append(groupListMap[groupInfo.ParentId], team1)
- }
- }
- }
- }
- for _, groupInfo := range fullGroups {
- var group1 crm.SellerAdminWithGroupTree
- //处理大组
- if groupInfo.ParentId == 0 {
- if _, ok2 := groupHasMap[groupInfo.GroupId]; !ok2 {
- if len(groupListMap[groupInfo.GroupId]) > 0 {
- group1 = crm.SellerAdminWithGroupTree{
- SellerId: groupInfo.GroupId,
- SellerName: groupInfo.GroupName,
- Child: groupListMap[groupInfo.GroupId],
- }
- groupHasMap[groupInfo.GroupId] = true
- departmentListMap[groupInfo.DepartmentId] = append(departmentListMap[groupInfo.DepartmentId], group1)
- listGroup = append(listGroup, group1)
- }
- }
- }
- }
- //分部门
- allTitleMap := map[int]string{
- crm.SellerDepartmentId: "FICC销售",
- crm.RaiSellerDepartmentId: "权益销售",
- }
- tmp1 := crm.SellerAdminWithGroupTree{
- SellerId: crm.SellerDepartmentId,
- SellerName: allTitleMap[crm.SellerDepartmentId],
- Child: departmentListMap[crm.SellerDepartmentId],
- }
- tmp2 := crm.SellerAdminWithGroupTree{
- SellerId: crm.RaiSellerDepartmentId,
- SellerName: allTitleMap[crm.RaiSellerDepartmentId],
- Child: departmentListMap[crm.RaiSellerDepartmentId],
- }
- allList := make([]crm.SellerAdminWithGroupTree, 0)
- allList = append(allList, tmp1)
- allList = append(allList, tmp2)
- resp.AllList = allList
- return
- }
- // 获取销售部门列表-包含禁用
- func GetSellerDepartmentListWithEnable() (sellerList []*crm.SellerAdminWithGroupTeam, err error) {
- departmentIds := []int{crm.SellerDepartmentId, crm.RaiSellerDepartmentId}
- sellerList = make([]*crm.SellerAdminWithGroupTeam, 0)
- // 获取组别列表
- groupCond := "department_id in (?)"
- groupPars := make([]interface{}, 0)
- groupPars = append(groupPars, departmentIds)
- groupOB := new(crm.SysGroup)
- groupList, e := groupOB.List(groupCond, groupPars)
- if e != nil {
- err = errors.New("获取组别列表失败, Err: " + e.Error())
- return
- }
- groupMap := make(map[int]*crm.SysGroup)
- for i := range groupList {
- groupMap[groupList[i].GroupId] = groupList[i]
- }
- // 获取销售部门所有销售信息
- adminCond := "department_id in (?) "
- adminPars := make([]interface{}, 0)
- adminPars = append(adminPars, departmentIds)
- adminOB := new(crm.Admin)
- adminList, e := adminOB.List(adminCond, adminPars)
- if e != nil {
- err = errors.New("获取销售列表失败, Err: " + e.Error())
- return
- }
- // 销售列表
- for i := range adminList {
- v := new(crm.SellerAdminWithGroupTeam)
- v.DepartmentId = adminList[i].DepartmentId
- v.SellerId = adminList[i].AdminId
- v.SellerName = adminList[i].RealName
- g := groupMap[adminList[i].GroupId]
- if g != nil {
- if g.ParentId > 0 {
- // 三级分组
- p := groupMap[g.ParentId]
- if p != nil {
- v.GroupId = p.GroupId
- v.GroupName = p.GroupName
- v.TeamId = g.GroupId
- v.TeamName = g.GroupName
- }
- } else {
- // 二级分组
- v.GroupId = g.GroupId
- v.GroupName = g.GroupName
- }
- }
- sellerList = append(sellerList, v)
- }
- return
- }
|