新增部门列表

This commit is contained in:
laodaming 2023-11-14 11:26:08 +08:00
parent 561ce385a1
commit 15671c02c3
7 changed files with 55 additions and 62 deletions

View File

@ -7,7 +7,7 @@ import (
)
//获取列表
func (d *LdapDepartmentModel)GetList(ctx context.Context,page,limit int,sort string)(resp []LdapDepartment,total int64,err error){
func (d *LdapDepartmentModel)GetAll(ctx context.Context,sort string)(resp []LdapDepartment,total int64,err error){
db := d.db.WithContext(ctx).Model(&LdapDepartment{})
if sort != ""{
db = db.Order(sort)
@ -15,8 +15,7 @@ func (d *LdapDepartmentModel)GetList(ctx context.Context,page,limit int,sort str
if err = db.Count(&total).Error;err != nil{
return nil, 0, err
}
offset := (page - 1) * limit
err = db.Offset(offset).Limit(limit).Find(&resp).Error
err = db.Find(&resp).Error
return resp, total, err
}

View File

@ -1,6 +1,9 @@
package config
import "github.com/zeromicro/go-zero/rest"
import (
"fusenapi/server/ldap-admin/internal/types"
"github.com/zeromicro/go-zero/rest"
)
type Config struct {
rest.RestConf

View File

@ -14,7 +14,7 @@ import (
func GetDepartmentsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetDepartmentsReq
var req types.Request
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return

View File

@ -13,7 +13,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Method: http.MethodGet,
Path: "/api/ldap-admin/get_departments",
Handler: GetDepartmentsHandler(serverCtx),
},

View File

@ -1,12 +1,9 @@
package logic
import (
"fusenapi/constants"
"context"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"math"
"context"
"fusenapi/server/ldap-admin/internal/svc"
"fusenapi/server/ldap-admin/internal/types"
@ -32,39 +29,42 @@ func NewGetDepartmentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
// func (l *GetDepartmentsLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *GetDepartmentsLogic) GetDepartments(req *types.GetDepartmentsReq, userinfo *auth.UserInfo) (resp *basic.Response) {
func (l *GetDepartmentsLogic) GetDepartments(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
//todo 鉴权 。。。。
if req.CurrentPage <= 0{
req.CurrentPage = constants.DEFAULT_PAGE
}
if req.Limit <= 0{
req.Limit = constants.DEFAULT_PAGE_SIZE
}
departList,total,err := l.svcCtx.AllModels.LdapDepartment.GetList(l.ctx,req.CurrentPage,req.Limit,"")
departList,_,err := l.svcCtx.AllModels.LdapDepartment.GetAll(l.ctx,"")
if err != nil{
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr,"获取部门列表失败")
}
list := make([]types.DepartmentsItem,0,len(departList))
mapDepartment := make(map[int64]*types.DepartmentsItem)
for _,v := range departList{
list = append(list,types.DepartmentsItem{
Id: v.Id,
Name: *v.Name,
Remark: *v.Remark,
Type: *v.Type,
ParentId: *v.ParentId,
Dn: *v.Dn,
mapDepartment[v.Id] = &types.DepartmentsItem{
Id: v.Id,
Name: *v.Name,
Remark: *v.Remark,
Type: *v.Type,
ParentId: *v.ParentId,
Dn: *v.Dn,
SyncState: *v.SyncState,
})
Child: make([]*types.DepartmentsItem,0,50),
}
}
return resp.SetStatus(basic.CodeOK,"success",types.GetDepartmentsRsp{
//组织树形关系
for _,v := range mapDepartment{
//有父级,把他付给父级
if info,ok := mapDepartment[v.ParentId];ok{
info.Child = append(info.Child,v)
}
}
list := make([]*types.DepartmentsItem,0,len(departList))
for _,v := range mapDepartment{
if v.ParentId == 0{
list = append(list,v)
}
}
return resp.SetStatusWithMessage(basic.CodeOK,"success",types.GetDepartmentsRsp{
List: list,
Meta: types.Meta{
TotalCount: total,
PageCount: int64(math.Ceil(float64(total) / float64(req.Limit))),
CurrentPage: req.CurrentPage,
PerPage: req.Limit,
},
})
}

View File

@ -5,24 +5,19 @@ import (
"fusenapi/utils/basic"
)
type GetDepartmentsReq struct {
CurrentPage int `form:"current_page"`
Limit int `form:"limit"`
}
type GetDepartmentsRsp struct {
List []DepartmentsItem `json:"list"`
Meta Meta `json:"meta"`
List []*DepartmentsItem `json:"list"`
}
type DepartmentsItem struct {
Id int64 `json:"id"`
Name string `json:"name"`
Remark string `json:"remark"`
Type string `json:"type"`
ParentId int64 `json:"parent_id"`
Dn string `json:"dn"`
SyncState int64 `json:"sync_state"`
Id int64 `json:"id"`
Name string `json:"name"`
Remark string `json:"remark"`
Type string `json:"type"`
ParentId int64 `json:"parent_id"`
Dn string `json:"dn"`
SyncState int64 `json:"sync_state"`
Child []*DepartmentsItem `json:"child"`
}
type SaveDepartmentReq struct {

View File

@ -12,29 +12,25 @@ import "basic.api"
service ldap-admin {
//获取部门列表
@handler GetDepartmentsHandler
post /api/ldap-admin/get_departments(GetDepartmentsReq) returns (response);
get /api/ldap-admin/get_departments(request) returns (response);
//保存部门信息
@handler SaveDepartmentHandler
post /api/ldap-admin/save_department(SaveDepartmentReq) returns (response);
}
//获取部门列表
type GetDepartmentsReq {
CurrentPage int `form:"current_page"`
Limit int `form:"limit"`
}
type GetDepartmentsRsp {
List []DepartmentsItem `json:"list"`
Meta Meta `json:"meta"`
List []*DepartmentsItem `json:"list"`
}
type DepartmentsItem {
Id int64 `json:"id"`
Name string `json:"name"`
Remark string `json:"remark"`
Type string `json:"type"`
ParentId int64 `json:"parent_id"`
Dn string `json:"dn"`
SyncState int64 `json:"sync_state"`
Id int64 `json:"id"`
Name string `json:"name"`
Remark string `json:"remark"`
Type string `json:"type"`
ParentId int64 `json:"parent_id"`
Dn string `json:"dn"`
SyncState int64 `json:"sync_state"`
Child []*DepartmentsItem `json:"child"`
}
//保存部门信息
type SaveDepartmentReq {