diff --git a/model/gmodel/ldap_group_menus_logic.go b/model/gmodel/ldap_group_menus_logic.go index e68225aa..cae06058 100644 --- a/model/gmodel/ldap_group_menus_logic.go +++ b/model/gmodel/ldap_group_menus_logic.go @@ -1,2 +1,23 @@ package gmodel -// TODO: 使用model的属性做你想做的 \ No newline at end of file + +import "context" + +func (gm *LdapGroupMenusModel) FindOne(ctx context.Context, menuId, groupId int64) (resp *LdapGroupMenus, err error) { + err = gm.db.WithContext(ctx).Model(&LdapGroupMenus{}).Where("menu_id = ? and group_id = ?", menuId, groupId).Take(&resp).Error + return resp, err +} + +func (gm *LdapGroupMenusModel) Create(ctx context.Context, data *LdapGroupMenus) error { + return gm.db.WithContext(ctx).Model(&LdapGroupMenus{}).Create(&data).Error +} + +func (gm *LdapGroupMenusModel) Delete(ctx context.Context, menuId, groupId int64) error { + return gm.db.WithContext(ctx).Model(&LdapGroupMenus{}).Where("menu_id = ? and group_id", menuId, groupId).Delete(&LdapGroupMenus{}).Error +} + +func (gm *LdapGroupMenusModel) DeleteByMenuId(ctx context.Context, menuId int64) error { + return gm.db.WithContext(ctx).Model(&LdapGroupMenus{}).Where("menu_id = ?", menuId).Delete(&LdapGroupMenus{}).Error +} +func (gm *LdapGroupMenusModel) DeleteByGroupId(ctx context.Context, groupId int64) error { + return gm.db.WithContext(ctx).Model(&LdapGroupMenus{}).Where("group_id = ?", groupId).Delete(&LdapGroupMenus{}).Error +} diff --git a/model/gmodel/ldap_menus_logic.go b/model/gmodel/ldap_menus_logic.go index 53d3491e..43cc2981 100644 --- a/model/gmodel/ldap_menus_logic.go +++ b/model/gmodel/ldap_menus_logic.go @@ -18,3 +18,46 @@ func (m *LdapMenusModel) FindByPath(ctx context.Context, path string) (resp *Lda func (m *LdapMenusModel) Update(ctx context.Context, id int64, data *LdapMenus) error { return m.db.WithContext(ctx).Model(&LdapMenus{}).Where("id = ?", id).Updates(&data).Error } + +type GetMenuListByParamReq struct { + Ids []int64 + Name string + Title string + Path string + Status *int64 + ParentId *int64 + Sort string + Page int + Limit int +} + +func (m *LdapMenusModel) GetMenuListByParam(ctx context.Context, req GetMenuListByParamReq) (resp []LdapMenus, total int64, err error) { + db := m.db.WithContext(ctx).Model(&LdapMenus{}) + if len(req.Ids) > 0 { + db = db.Where("id in(?)", req.Ids) + } + if req.Name != "" { + db = db.Where("name like ?", "%"+req.Name+"%") + } + if req.Title != "" { + db = db.Where("title like ?", "%"+req.Title+"%") + } + if req.Name != "" { + db = db.Where("path like ?", "%"+req.Path+"%") + } + if req.Status != nil { + db = db.Where("status = ?", req.Status) + } + if req.ParentId != nil { + db = db.Where("parent_id = ?", req.ParentId) + } + if req.Sort != "" { + db = db.Order(req.Sort) + } + if err = db.Count(&total).Error; err != nil { + return nil, 0, err + } + offset := (req.Page - 1) * req.Limit + err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error + return resp, total, err +} diff --git a/server/ldap-admin/internal/logic/deletemenulogic.go b/server/ldap-admin/internal/logic/deletemenulogic.go index cc6ba39e..695252bd 100644 --- a/server/ldap-admin/internal/logic/deletemenulogic.go +++ b/server/ldap-admin/internal/logic/deletemenulogic.go @@ -1,8 +1,11 @@ package logic import ( + "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" + "gorm.io/gorm" + "time" "context" @@ -31,10 +34,29 @@ func NewDeleteMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delete // } func (l *DeleteMenuLogic) DeleteMenu(req *types.DeleteMenuReq, userinfo *auth.UserInfo) (resp *basic.Response) { - // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) - // userinfo 传入值时, 一定不为null - - return resp.SetStatus(basic.CodeOK) + if req.Id <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数id无效") + } + err := l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error { + menusModel := gmodel.NewLdapMenusModel(tx) + status := int64(0) + now := time.Now().UTC() + err := menusModel.Update(l.ctx, req.Id, &gmodel.LdapMenus{ + Status: &status, + Utime: &now, + }) + if err != nil { + return err + } + //删除分组绑定的菜单 + groupMenusModel := gmodel.NewLdapGroupMenusModel(tx) + return groupMenusModel.DeleteByMenuId(l.ctx, req.Id) + }) + if 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/logic/getmenudetaillogic.go b/server/ldap-admin/internal/logic/getmenudetaillogic.go index 196681f4..48d98416 100644 --- a/server/ldap-admin/internal/logic/getmenudetaillogic.go +++ b/server/ldap-admin/internal/logic/getmenudetaillogic.go @@ -1,8 +1,10 @@ package logic import ( + "errors" "fusenapi/utils/auth" "fusenapi/utils/basic" + "gorm.io/gorm" "context" @@ -31,10 +33,27 @@ func NewGetMenuDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Get // } func (l *GetMenuDetailLogic) GetMenuDetail(req *types.GetMenuDetailReq, userinfo *auth.UserInfo) (resp *basic.Response) { - // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) - // userinfo 传入值时, 一定不为null - - return resp.SetStatus(basic.CodeOK) + if req.Id <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数id无效") + } + menu, err := l.svcCtx.AllModels.LdapMenus.FindOne(l.ctx, req.Id) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "该菜单信息不存在") + } + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "查询菜单详情失败") + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetMenuDetailRsp{ + Id: menu.Id, + Name: *menu.Name, + Title: *menu.Title, + Icon: *menu.Icon, + Path: *menu.Path, + Sort: *menu.Sort, + ParentId: *menu.ParentId, + Status: *menu.Status, + }) } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 diff --git a/server/ldap-admin/internal/logic/getmenuslogic.go b/server/ldap-admin/internal/logic/getmenuslogic.go index 6155b4b0..de2ac938 100644 --- a/server/ldap-admin/internal/logic/getmenuslogic.go +++ b/server/ldap-admin/internal/logic/getmenuslogic.go @@ -1,8 +1,11 @@ package logic import ( + "fusenapi/constants" + "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" + "math" "context" @@ -31,10 +34,48 @@ func NewGetMenusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenus // } func (l *GetMenusLogic) GetMenus(req *types.GetMenusReq, userinfo *auth.UserInfo) (resp *basic.Response) { - // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) - // userinfo 传入值时, 一定不为null - - return resp.SetStatus(basic.CodeOK) + if req.CurrentPage <= 0 { + req.CurrentPage = constants.DEFAULT_PAGE + } + limit := constants.DEFAULT_PAGE_SIZE + status := int64(1) + menusReq := gmodel.GetMenuListByParamReq{ + Name: req.Name, + Title: req.Title, + Path: req.Path, + Status: &status, + ParentId: req.ParentId, + Sort: "sort DESC", + Page: req.CurrentPage, + Limit: limit, + } + menus, total, err := l.svcCtx.AllModels.LdapMenus.GetMenuListByParam(l.ctx, menusReq) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "获取菜单列表失败") + } + list := make([]types.MenuItem, 0, len(menus)) + for _, v := range menus { + list = append(list, types.MenuItem{ + Id: v.Id, + Name: *v.Name, + Title: *v.Title, + Icon: *v.Icon, + Path: *v.Path, + Sort: *v.Sort, + ParentId: *v.ParentId, + Status: *v.Status, + }) + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetMenusRsp{ + List: list, + Meta: types.Meta{ + TotalCount: total, + PageCount: int64(math.Ceil(float64(total) / float64(limit))), + CurrentPage: req.CurrentPage, + PerPage: limit, + }, + }) } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 diff --git a/server/ldap-admin/internal/types/types.go b/server/ldap-admin/internal/types/types.go index affffe4b..9133df71 100644 --- a/server/ldap-admin/internal/types/types.go +++ b/server/ldap-admin/internal/types/types.go @@ -75,11 +75,11 @@ type GetMenuDetailRsp struct { } type GetMenusReq struct { - CurrentPage int64 `form:"current_page"` + CurrentPage int `form:"current_page"` Name string `form:"name"` Title string `form:"title"` Path string `form:"path"` - ParentId int64 `form:"parent_id"` + ParentId *int64 `form:"parent_id"` } type GetMenusRsp struct { diff --git a/server_api/ldap-admin.api b/server_api/ldap-admin.api index acd9ecef..fc7113ef 100644 --- a/server_api/ldap-admin.api +++ b/server_api/ldap-admin.api @@ -103,11 +103,11 @@ type GetMenuDetailRsp { } //获取菜单列表 type GetMenusReq { - CurrentPage int64 `form:"current_page"` + CurrentPage int `form:"current_page"` Name string `form:"name"` Title string `form:"title"` Path string `form:"path"` - ParentId int64 `form:"parent_id"` + ParentId *int64 `form:"parent_id"` } type GetMenusRsp { List []MenuItem `json:"list"`