This commit is contained in:
2025-12-09 18:55:28 +08:00
parent 8d00d67540
commit c23ab8338b
209 changed files with 5445 additions and 3963 deletions

View File

@@ -1,17 +1,18 @@
package admin_user
import (
"context"
"context"
"ycc-server/app/main/api/internal/svc"
"ycc-server/app/main/api/internal/types"
"ycc-server/app/main/model"
"ycc-server/common/xerr"
"ycc-server/app/main/api/internal/svc"
"ycc-server/app/main/api/internal/types"
"ycc-server/app/main/model"
"ycc-server/common/xerr"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/google/uuid"
)
type AdminUpdateUserLogic struct {
@@ -76,7 +77,7 @@ func (l *AdminUpdateUserLogic) AdminUpdateUser(req *types.AdminUpdateUserReq) (r
}
// 2. 转换为map便于查找
currentRoleMap := make(map[int64]*model.AdminUserRole)
currentRoleMap := make(map[string]*model.AdminUserRole)
for _, role := range currentRoles {
currentRoleMap[role.RoleId] = role
}
@@ -85,13 +86,13 @@ func (l *AdminUpdateUserLogic) AdminUpdateUser(req *types.AdminUpdateUserReq) (r
for _, roleId := range req.RoleIds {
exists, err := l.svcCtx.AdminRoleModel.FindOne(ctx, roleId)
if err != nil || exists == nil {
return errors.Wrapf(xerr.NewErrMsg("角色不存在"), "角色ID: %d", roleId)
return errors.Wrapf(xerr.NewErrMsg("角色不存在"), "角色ID: %s", roleId)
}
}
// 4. 找出需要删除和新增的关联
var toDelete []*model.AdminUserRole
var toInsert []int64
var toInsert []string
// 需要删除的:当前存在但新列表中没有的
for roleId, userRole := range currentRoleMap {
@@ -116,16 +117,17 @@ func (l *AdminUpdateUserLogic) AdminUpdateUser(req *types.AdminUpdateUserReq) (r
}
// 6. 添加新的关联
for _, roleId := range toInsert {
userRole := &model.AdminUserRole{
UserId: req.Id,
RoleId: roleId,
}
_, err = l.svcCtx.AdminUserRoleModel.Insert(ctx, session, userRole)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "添加用户角色关联失败: %v", err)
}
}
for _, roleId := range toInsert {
userRole := &model.AdminUserRole{
Id: uuid.NewString(),
UserId: req.Id,
RoleId: roleId,
}
_, err = l.svcCtx.AdminUserRoleModel.Insert(ctx, session, userRole)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "添加用户角色关联失败: %v", err)
}
}
}
return nil