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