This commit is contained in:
laodaming 2023-11-14 12:13:04 +08:00
parent 1cfc190b73
commit 420829cec3

View File

@ -1,6 +1,7 @@
package logic
import (
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
@ -37,9 +38,17 @@ func (l *GetDepartmentsLogic) GetDepartments(req *types.Request, userinfo *auth.
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "获取部门列表失败")
}
//变成树形结构
list := l.DepartmentListToTree(departList)
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetDepartmentsRsp{
List: list,
})
}
func (l *GetDepartmentsLogic)DepartmentListToTree(deps []gmodel.LdapDepartment)[]*types.DepartmentsItem{
//存入map
mapDepartment := make(map[int64]*types.DepartmentsItem)
for _, v := range departList {
for _, v := range deps {
mapDepartment[v.Id] = &types.DepartmentsItem{
Id: v.Id,
Name: *v.Name,
@ -53,7 +62,7 @@ func (l *GetDepartmentsLogic) GetDepartments(req *types.Request, userinfo *auth.
}
}
for _,v := range mapDepartment{
for _,val := range departList{
for _,val := range deps{
if *val.ParentId != v.Id{
continue
}
@ -69,18 +78,12 @@ func (l *GetDepartmentsLogic) GetDepartments(req *types.Request, userinfo *auth.
})
}
}
list := make([]*types.DepartmentsItem, 0, len(departList))
for _, v := range mapDepartment {
if v.ParentId == 0 {
list = append(list, v)
list := make([]*types.DepartmentsItem, 0, len(deps))
for _, v := range deps {
if *v.ParentId == 0 {
list = append(list, mapDepartment[v.Id])
}
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetDepartmentsRsp{
List: list,
})
return list
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetDepartmentsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }