This commit is contained in:
laodaming 2023-11-15 16:32:07 +08:00
parent 6d7a88ef29
commit 1a2c9a8f49
7 changed files with 163 additions and 17 deletions

View File

@ -1,2 +1,23 @@
package gmodel
// TODO: 使用model的属性做你想做的
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
}

View File

@ -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
}

View File

@ -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 必须重新处理

View File

@ -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 必须重新处理

View File

@ -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 必须重新处理

View File

@ -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 {

View File

@ -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"`