package system import ( "errors" "hongze/hrms_api/models/base" "hongze/hrms_api/models/system" "hongze/hrms_api/utils" ) func DeptTreeList(page base.IPage, req *system.DeptListReq) (count int64, list []*system.DeptListItemResp, err error, errMsg string) { dept := new(system.SysDept) var condition string var pars []interface{} //根据名称找到所有最顶层的节点 var finalRootIds []int64 if req.DeptName != "" { var rootIds []int64 deptName := "%"+req.DeptName+"%" relationList, tErr := dept.GetDeptByName(deptName) if tErr != nil { errMsg = "查询部门出错" err = errors.New(errMsg+"Err:" + tErr.Error()) return } if len(relationList) == 0 { //查不到相关记录 return } for _, v := range relationList { if v.RootId == 0 { rootIds = append(rootIds, v.DeptId) }else{ rootIds = append(rootIds, v.RootId) } } //分页处理 查询最顶层结点,得到最终的顶层结点 condition = "parent_id=0 and dept_id in ?" pars = append(pars, rootIds) }else{ condition = "parent_id=0" } count, rootList, err := dept.SelectPage(page, condition, pars) if err != nil { errMsg = "查询部门列表出错" err = errors.New(errMsg+"Err:" + err.Error()) return } if len(rootList) == 0 { //查不到相关记录 return } for _, v := range rootList { finalRootIds = append(finalRootIds, v.DeptId) } //查询所有该顶层节点下的子节点 fCondition := "root_id in ? or dept_id in ?" fPars := make([]interface{}, 0) fPars = append(fPars, finalRootIds, finalRootIds) orderStr := "sort asc, dept_id desc" childList, err := dept.GetDeptListByCondition(fCondition, fPars, orderStr) if err != nil { errMsg = "查询部门列表出错" err = errors.New(errMsg+"Err:" + err.Error()) return } list = make([]*system.DeptListItemResp, 0) for _, v:= range childList { tmp := &system.DeptListItemResp{ DeptId: v.DeptId, ParentId: v.ParentId, DeptName: v.DeptName, Sort: v.Sort, CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime), ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime), Children: nil, } list = append(list, tmp) } //组装返回值 list = getDeptTreeRecursive(list, 0) return } func getDeptTreeRecursive(list []*system.DeptListItemResp, parentId int64) []*system.DeptListItemResp { res := make([]*system.DeptListItemResp, 0) for _, v := range list { if v.ParentId == parentId { v.Children = getDeptTreeRecursive(list, v.DeptId) res = append(res, v) } } return res } // DeleteDept 删除部门,连同删除子部门 func DeleteDept(req *system.DelSysDeptReq) (err error, errMsg string) { dept := new(system.SysDept) //找出当前层级,如果当前是最顶层,则批量删除和顶层相关的所有部门 deptInfo, err := dept.GetDeptByDeptId(req.DeptId) if err != nil { if err == utils.ErrNoRow { errMsg = "部门不存在" err = errors.New(errMsg+"Err:" + err.Error()) return } errMsg = "查询部门出错" err = errors.New(errMsg+"Err:" + err.Error()) return } admin := new(system.SysAdmin) var admins []*system.SysAdmin var depts []*system.SysDept var deptIdMap map[int64][]int64 if deptInfo.ParentId > 0 { //如果不是最顶层,则构建树形图,得到需要删除的dept_id //查询所有该顶层节点下的子节点 deptIdMap, err, errMsg = GetChildDeptIds(deptInfo.RootId) if err != nil { return err, errMsg } cond := "dept_id in ?" pars := make([]interface{}, 0) pars = append(pars, deptIdMap[deptInfo.DeptId]) //验证是否存在绑定的员工账号 admins, err = admin.GetAdminListByCondition(cond, pars) if err != nil { errMsg = "查询部门下的用户出错" err = errors.New(errMsg+"Err:" + err.Error()) return } if len(admins) > 0 { errMsg = "该部门绑定了员工,不可删除" err = errors.New(errMsg) return } err = dept.DeleteByCondition(cond, pars) }else{ //如果是最顶层,则删除所有相关的dept_id cond := "dept_id = ? or root_id = ?" pars := make([]interface{}, 0) pars = append(pars, deptInfo.DeptId, deptInfo.DeptId) depts, err = dept.GetDeptListByCondition(cond, pars, "") if err != nil { errMsg = "查询部门信息出错" err = errors.New(errMsg+"Err:" + err.Error()) return } var deptIds []int64 for _, v :=range depts { deptIds = append(deptIds, v.DeptId) } //验证是否存在绑定的员工账号 aCond := "dept_id in ?" aPars := make([]interface{}, 0) aPars = append(aPars, deptIds) admins, err = admin.GetAdminListByCondition(aCond, aPars) if err != nil { errMsg = "查询部门下的员工出错" err = errors.New(errMsg+"Err:" + err.Error()) return } if len(admins) > 0 { errMsg = "该部门绑定了员工,不可删除" err = errors.New(errMsg) return } err = dept.DeleteByCondition(cond, pars) } return } func GetChildDeptIds(rootId int64) (deptIdMap map[int64][]int64, err error, errMsg string) { dept := new(system.SysDept) fCondition := "" fPars := make([]interface{}, 0) if rootId > 0 { fCondition = "root_id = ? or dept_id = ?" fPars = append(fPars, rootId, rootId) } orderStr := "level desc" childList, tErr := dept.GetDeptListByCondition(fCondition, fPars, orderStr) if tErr != nil { errMsg = "查询部门列表出错" err = errors.New(errMsg+"Err:" + tErr.Error()) return } deptIdMap = make(map[int64][]int64) for _, v := range childList { deptIdMap[v.DeptId] = append(deptIdMap[v.DeptId], v.DeptId) if v.ParentId > 0 { deptIdMap[v.ParentId] = append(deptIdMap[v.ParentId], deptIdMap[v.DeptId]...) } } return } func GetChildDeptNames() (deptNameMap map[int64]string, err error, errMsg string) { dept := new(system.SysDept) fCondition := "" fPars := make([]interface{}, 0) orderStr := "level asc" childList, tErr := dept.GetDeptListByCondition(fCondition, fPars, orderStr) if tErr != nil { errMsg = "查询部门列表出错" err = errors.New(errMsg+"Err:" + tErr.Error()) return } deptNameMap = make(map[int64]string) for _, v := range childList { if v.ParentId > 0 { deptNameMap[v.DeptId] = deptNameMap[v.ParentId] + "/" + v.DeptName }else{ deptNameMap[v.DeptId] = v.DeptName } } return } func DeptAdminTreeList() (list []*system.DeptAdminListItem, err error, errMsg string) { dept := new(system.SysDept) var condition string var pars []interface{} childList, err := dept.GetDeptListByCondition(condition, pars, "") if err != nil { errMsg = "查询部门列表出错" err = errors.New(errMsg+"Err:" + err.Error()) return } if len(childList) == 0 { //查不到相关记录 return } // 查询管理员信息 admin := new(system.SysAdmin) var list1 []*system.DeptAdminListItem adminList, err := admin.GetAdminListByCondition(condition, pars) adminMap := make(map[int64][]*system.DeptAdminListItem) for _, v := range adminList { tmp1 := &system.DeptAdminListItem{ AdminId: v.AdminId, Name: v.RealName, DeptId: int64(v.AdminId)+1000, ParentId: v.DeptId, Sort: 0, Children: nil, } list1 = append(list1, tmp1) } list = make([]*system.DeptAdminListItem, 0) for _, v:= range childList { tmp := &system.DeptAdminListItem{ DeptId: v.DeptId, Name: v.DeptName, ParentId: v.ParentId, Sort: v.Sort, Children: nil, } if c, ok := adminMap[v.DeptId]; ok { tmp.Children = c } list = append(list, tmp) } list = append(list, list1...) //组装返回值 list = getDeptAdminTreeRecursive(list, 0) return } func getDeptAdminTreeRecursive(list []*system.DeptAdminListItem, parentId int64) []*system.DeptAdminListItem { res := make([]*system.DeptAdminListItem, 0) for _, v := range list { if v.ParentId == parentId { v.Children = getDeptAdminTreeRecursive(list, v.DeptId) res = append(res, v) } } return res }