From 420829cec33b6864f1b9708efc65c04ffc956d7d Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 14 Nov 2023 12:13:04 +0800 Subject: [PATCH 1/3] fix --- .../internal/logic/getdepartmentslogic.go | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/server/ldap-admin/internal/logic/getdepartmentslogic.go b/server/ldap-admin/internal/logic/getdepartmentslogic.go index 668ee0f1..1157ba52 100644 --- a/server/ldap-admin/internal/logic/getdepartmentslogic.go +++ b/server/ldap-admin/internal/logic/getdepartmentslogic.go @@ -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) -// } From 79a6c3fa246986918d7bab983b0a83bdec16c03b Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 14 Nov 2023 14:00:00 +0800 Subject: [PATCH 2/3] fix --- .../internal/logic/getdepartmentslogic.go | 21 +++++++------------ .../internal/logic/savedepartmentlogic.go | 5 ++++- server/ldap-admin/internal/types/types.go | 14 +++++++------ server_api/ldap-admin.api | 14 +++++++------ 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/server/ldap-admin/internal/logic/getdepartmentslogic.go b/server/ldap-admin/internal/logic/getdepartmentslogic.go index 1157ba52..4efb60c8 100644 --- a/server/ldap-admin/internal/logic/getdepartmentslogic.go +++ b/server/ldap-admin/internal/logic/getdepartmentslogic.go @@ -4,6 +4,7 @@ import ( "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" + "sort" "context" @@ -61,23 +62,17 @@ func (l *GetDepartmentsLogic)DepartmentListToTree(deps []gmodel.LdapDepartment)[ Child: make([]*types.DepartmentsItem, 0, 50), } } + //组织从属关系 for _,v := range mapDepartment{ - for _,val := range deps{ - if *val.ParentId != v.Id{ - continue - } - v.Child = append(v.Child,&types.DepartmentsItem{ - Id: val.Id, - Name: *val.Name, - Remark: *val.Remark, - Type: *val.Type, - ParentId: *val.ParentId, - Dn: *val.Dn, - SyncState: *val.SyncState, - Sort: *val.Sort, + //如果有父级 + if parent,ok := mapDepartment[v.ParentId];ok{ + parent.Child = append(parent.Child,v) + sort.Slice(parent.Child, func(i, j int) bool { + return parent.Child[i].Sort < parent.Child[j].Sort //升序 }) } } + //排序 list := make([]*types.DepartmentsItem, 0, len(deps)) for _, v := range deps { if *v.ParentId == 0 { diff --git a/server/ldap-admin/internal/logic/savedepartmentlogic.go b/server/ldap-admin/internal/logic/savedepartmentlogic.go index b3b616c7..a16a6c63 100644 --- a/server/ldap-admin/internal/logic/savedepartmentlogic.go +++ b/server/ldap-admin/internal/logic/savedepartmentlogic.go @@ -31,7 +31,10 @@ func NewSaveDepartmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Sa // } func (l *SaveDepartmentLogic) SaveDepartment(req *types.SaveDepartmentReq, userinfo *auth.UserInfo) (resp *basic.Response) { - //todo 鉴权。。。 + + if req.Id > 0{//更新 + + } return resp.SetStatus(basic.CodeOK) } diff --git a/server/ldap-admin/internal/types/types.go b/server/ldap-admin/internal/types/types.go index 7ddf6522..272fe4b3 100644 --- a/server/ldap-admin/internal/types/types.go +++ b/server/ldap-admin/internal/types/types.go @@ -22,12 +22,14 @@ type DepartmentsItem struct { } type SaveDepartmentReq 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"` + 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"` + Sort int64 `json:"sort"` } type Request struct { diff --git a/server_api/ldap-admin.api b/server_api/ldap-admin.api index 1c3319bc..45a28b20 100644 --- a/server_api/ldap-admin.api +++ b/server_api/ldap-admin.api @@ -35,10 +35,12 @@ type DepartmentsItem { } //保存部门信息 type SaveDepartmentReq { - 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"` + 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"` + Sort int64 `json:"sort"` } \ No newline at end of file From d471c1b43e19344631601cc3c2bec03248b8f2fc Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 14 Nov 2023 14:20:14 +0800 Subject: [PATCH 3/3] fix --- model/gmodel/ldap_department_gen.go | 9 +++--- model/gmodel/ldap_menus_gen.go | 6 ++-- model/gmodel/ldap_roles_gen.go | 4 +-- model/gmodel/ldap_users_gen.go | 4 +-- .../internal/logic/savedepartmentlogic.go | 29 ++++++++++++++++--- server/ldap-admin/internal/types/types.go | 15 +++++----- server_api/ldap-admin.api | 15 +++++----- 7 files changed, 50 insertions(+), 32 deletions(-) diff --git a/model/gmodel/ldap_department_gen.go b/model/gmodel/ldap_department_gen.go index 533341e3..7a968aab 100644 --- a/model/gmodel/ldap_department_gen.go +++ b/model/gmodel/ldap_department_gen.go @@ -1,21 +1,20 @@ package gmodel import ( - "time" - "gorm.io/gorm" + "time" ) // ldap_department 部门表 type LdapDepartment struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // - Name *string `gorm:"default:'';" json:"name"` // - Remark *string `gorm:"default:'';" json:"remark"` // + Name *string `gorm:"unique_key;default:'';" json:"name"` // + Remark *string `gorm:"unique_key;default:'';" json:"remark"` // Creator *string `gorm:"default:'';" json:"creator"` // Type *string `gorm:"default:'';" json:"type"` // ParentId *int64 `gorm:"default:0;" json:"parent_id"` // 层级如 10/20/30 Dn *string `gorm:"default:'';" json:"dn"` // - SyncState *int64 `gorm:"default:1;" json:"sync_state"` // + SyncState *int64 `gorm:"default:1;" json:"sync_state"` // 同步状态:1已同步, 2未同步 Sort *int64 `gorm:"default:999;" json:"sort"` // 排序 Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` // Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` // diff --git a/model/gmodel/ldap_menus_gen.go b/model/gmodel/ldap_menus_gen.go index 3783934e..be33649f 100644 --- a/model/gmodel/ldap_menus_gen.go +++ b/model/gmodel/ldap_menus_gen.go @@ -12,9 +12,9 @@ type LdapMenus struct { Title *string `gorm:"default:'';" json:"title"` // Icon *string `gorm:"default:'';" json:"icon"` // Path *string `gorm:"default:'';" json:"path"` // - Sort *int64 `gorm:"default:999;" json:"sort"` // - Status *int64 `gorm:"default:1;" json:"status"` // - ParentId *int64 `gorm:"default:0;" json:"parent_id"` // + Sort *int64 `gorm:"default:999;" json:"sort"` // 菜单顺序(1-999) + Status *int64 `gorm:"default:1;" json:"status"` // 菜单状态(正常/禁用, 默认正常) + ParentId *int64 `gorm:"default:0;" json:"parent_id"` // 父菜单编号(编号为0时表示根菜单) Creator *string `gorm:"default:'';" json:"creator"` // Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` // Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` // diff --git a/model/gmodel/ldap_roles_gen.go b/model/gmodel/ldap_roles_gen.go index 7dc8d906..4f72bbf3 100644 --- a/model/gmodel/ldap_roles_gen.go +++ b/model/gmodel/ldap_roles_gen.go @@ -11,8 +11,8 @@ type LdapRoles struct { Name *string `gorm:"unique_key;default:'';" json:"name"` // Keyword *string `gorm:"unique_key;default:'';" json:"keyword"` // Remark *string `gorm:"default:'';" json:"remark"` // - Status *int64 `gorm:"default:1;" json:"status"` // - Sort *int64 `gorm:"default:999;" json:"sort"` // + Status *int64 `gorm:"default:1;" json:"status"` // 1正常, 2禁用 + Sort *int64 `gorm:"default:999;" json:"sort"` // 角色排序(排序越大权限越低, 不能查看比自己序号小的角色, 不能编辑同序号用户权限, 排序为1表示超级管理员) Creator *string `gorm:"default:'';" json:"creator"` // Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` // Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` // diff --git a/model/gmodel/ldap_users_gen.go b/model/gmodel/ldap_users_gen.go index 1e86fde5..25f1a7cb 100644 --- a/model/gmodel/ldap_users_gen.go +++ b/model/gmodel/ldap_users_gen.go @@ -18,10 +18,10 @@ type LdapUsers struct { Avatar *string `gorm:"default:'';" json:"avatar"` // Position *string `gorm:"default:'';" json:"position"` // Introduction *string `gorm:"default:'';" json:"introduction"` // - Status *int64 `gorm:"default:1;" json:"status"` // + Status *int64 `gorm:"default:1;" json:"status"` // 状态:1在职, 2离职 Creator *string `gorm:"default:'';" json:"creator"` // UserDn *string `gorm:"default:'';" json:"user_dn"` // - SyncState *int64 `gorm:"default:1;" json:"sync_state"` // + SyncState *int64 `gorm:"default:1;" json:"sync_state"` // 同步状态:1已同步, 2未同步 Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` // Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` // } diff --git a/server/ldap-admin/internal/logic/savedepartmentlogic.go b/server/ldap-admin/internal/logic/savedepartmentlogic.go index a16a6c63..2539b4d4 100644 --- a/server/ldap-admin/internal/logic/savedepartmentlogic.go +++ b/server/ldap-admin/internal/logic/savedepartmentlogic.go @@ -1,8 +1,10 @@ package logic import ( + "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" + "time" "context" @@ -31,11 +33,30 @@ func NewSaveDepartmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Sa // } func (l *SaveDepartmentLogic) SaveDepartment(req *types.SaveDepartmentReq, userinfo *auth.UserInfo) (resp *basic.Response) { - - if req.Id > 0{//更新 - + now := time.Now().UTC() + data := &gmodel.LdapDepartment{ + Name: &req.Name, + Remark: &req.Remark, + Type: &req.Type, + ParentId: &req.ParentId, + Dn: &req.Dn, + Sort: &req.Sort, + Utime: &now, } - return resp.SetStatus(basic.CodeOK) + if req.Id > 0{//更新 + if err := l.svcCtx.AllModels.LdapDepartment.Update(l.ctx,req.Id,data);err != nil{ + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr,"更新失败") + } + return resp.SetStatusWithMessage(basic.CodeOK,"更新成功") + } + //添加 + data.Ctime = &now + if err := l.svcCtx.AllModels.LdapDepartment.Create(l.ctx,data);err != nil{ + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr,"添加失败") + } + return resp.SetStatusWithMessage(basic.CodeOK,"添加成功") } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 diff --git a/server/ldap-admin/internal/types/types.go b/server/ldap-admin/internal/types/types.go index 272fe4b3..2e301b91 100644 --- a/server/ldap-admin/internal/types/types.go +++ b/server/ldap-admin/internal/types/types.go @@ -22,14 +22,13 @@ type DepartmentsItem struct { } type SaveDepartmentReq 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"` - Sort int64 `json:"sort"` + 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"` + Sort int64 `json:"sort"` } type Request struct { diff --git a/server_api/ldap-admin.api b/server_api/ldap-admin.api index 45a28b20..51c3243e 100644 --- a/server_api/ldap-admin.api +++ b/server_api/ldap-admin.api @@ -35,12 +35,11 @@ type DepartmentsItem { } //保存部门信息 type SaveDepartmentReq { - 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"` - Sort int64 `json:"sort"` + 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"` + Sort int64 `json:"sort"` } \ No newline at end of file