49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
sysModel "sundynix-micro-go/app/system/model"
|
|
"sundynix-micro-go/app/system/rpc/internal/svc"
|
|
"sundynix-micro-go/app/system/rpc/system"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type UpdateRoleLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewUpdateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateRoleLogic {
|
|
return &UpdateRoleLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
|
}
|
|
|
|
func (l *UpdateRoleLogic) UpdateRole(in *system.RoleUpdateReq) (*system.CommonResp, error) {
|
|
err := l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Model(&sysModel.SundynixRole{}).Where("id = ?", in.Id).Updates(map[string]interface{}{
|
|
"name": in.Name, "code": in.Code, "sort": in.Sort,
|
|
}).Error; err != nil {
|
|
return fmt.Errorf("更新角色失败: %w", err)
|
|
}
|
|
// 先删除旧的菜单关联
|
|
if err := tx.Where("role_id = ?", in.Id).Delete(&sysModel.SundynixRoleMenu{}).Error; err != nil {
|
|
return fmt.Errorf("清除菜单关联失败: %w", err)
|
|
}
|
|
// 重新关联菜单
|
|
for _, mid := range in.MenuIds {
|
|
if err := tx.Create(&sysModel.SundynixRoleMenu{RoleID: in.Id, MenuID: mid}).Error; err != nil {
|
|
return fmt.Errorf("关联菜单失败: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &system.CommonResp{Code: 200, Msg: "success"}, nil
|
|
}
|