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 }