package dept import ( "context" "time" "perms-system-server/internal/consts" authHelper "perms-system-server/internal/logic/auth" "perms-system-server/internal/response" "perms-system-server/internal/svc" "perms-system-server/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type UpdateDeptLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewUpdateDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateDeptLogic { return &UpdateDeptLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *UpdateDeptLogic) UpdateDept(req *types.UpdateDeptReq) error { if err := authHelper.RequireSuperAdmin(l.ctx); err != nil { return err } if len(req.Name) > 64 { return response.ErrBadRequest("部门名称长度不能超过64个字符") } if len(req.Remark) > 255 { return response.ErrBadRequest("备注长度不能超过255个字符") } dept, err := l.svcCtx.SysDeptModel.FindOne(l.ctx, req.Id) if err != nil { return response.ErrNotFound("部门不存在") } dept.Name = req.Name dept.Sort = req.Sort dept.Remark = req.Remark if req.DeptType != "" { if req.DeptType != consts.DeptTypeNormal && req.DeptType != consts.DeptTypeDev { return response.ErrBadRequest("部门类型无效,仅支持 NORMAL 和 DEV") } dept.DeptType = req.DeptType } if req.Status != 0 { if req.Status != consts.StatusEnabled && req.Status != consts.StatusDisabled { return response.ErrBadRequest("状态值无效,仅支持 1(启用) 和 2(禁用)") } dept.Status = req.Status } dept.UpdateTime = time.Now().Unix() if err := l.svcCtx.SysDeptModel.Update(l.ctx, dept); err != nil { return err } userIds, _ := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, req.Id) affectedCount := len(userIds) for _, uid := range userIds { l.svcCtx.UserDetailsLoader.Clean(l.ctx, uid) } if req.DeptType == consts.DeptTypeNormal || req.DeptType == consts.DeptTypeDev { childDepts, _ := l.svcCtx.SysDeptModel.FindByPathPrefix(l.ctx, dept.Path) for _, cd := range childDepts { if cd.Id == req.Id { continue } childUserIds, _ := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, cd.Id) affectedCount += len(childUserIds) for _, uid := range childUserIds { l.svcCtx.UserDetailsLoader.Clean(l.ctx, uid) } } } if affectedCount > 0 { l.Infof("UpdateDept id=%d deptType=%s affectedUsers=%d", req.Id, req.DeptType, affectedCount) } return nil }