48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
// Code scaffolded by goctl. Safe to edit.
|
|
package role
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"sundynix-micro-go/app/system/api/internal/svc"
|
|
"sundynix-micro-go/app/system/api/internal/types"
|
|
sysModel "sundynix-micro-go/app/system/model"
|
|
)
|
|
|
|
type UpdateRoleLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewUpdateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateRoleLogic {
|
|
return &UpdateRoleLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
|
}
|
|
|
|
func (l *UpdateRoleLogic) UpdateRole(req *types.RoleUpdateReq) error {
|
|
updates := map[string]interface{}{}
|
|
if req.Name != "" {
|
|
updates["name"] = req.Name
|
|
}
|
|
if req.Code != "" {
|
|
updates["code"] = req.Code
|
|
}
|
|
if req.Sort > 0 {
|
|
updates["sort"] = req.Sort
|
|
}
|
|
if len(updates) > 0 {
|
|
if err := l.svcCtx.DB.Model(&sysModel.SundynixRole{}).Where("id = ?", req.Id).Updates(updates).Error; err != nil {
|
|
return fmt.Errorf("更新角色失败")
|
|
}
|
|
}
|
|
// 更新菜单关联
|
|
if len(req.MenuIds) > 0 {
|
|
l.svcCtx.DB.Where("role_id = ?", req.Id).Delete(&sysModel.SundynixRoleMenu{})
|
|
for _, menuID := range req.MenuIds {
|
|
l.svcCtx.DB.Create(&sysModel.SundynixRoleMenu{RoleID: req.Id, MenuID: menuID})
|
|
}
|
|
}
|
|
return nil
|
|
}
|