From 0418bc4ab05a970b2460e7547aa607e226754455 Mon Sep 17 00:00:00 2001 From: guosl Date: Thu, 1 Aug 2024 16:19:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=84=E7=BB=87=E7=94=A8=E6=88=B7=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/models/organization/request.go | 8 +- internal/repo/user_organization.go | 19 ++++- internal/service/organization/interface.go | 1 + internal/service/organization/organization.go | 83 +++++++++++++++++-- internal/service/organization/util.go | 23 +++++ pkg/validator/custom_rules.go | 2 +- server/organization/organization.go | 7 +- 7 files changed, 134 insertions(+), 9 deletions(-) diff --git a/internal/models/organization/request.go b/internal/models/organization/request.go index af7ea4c..018f426 100644 --- a/internal/models/organization/request.go +++ b/internal/models/organization/request.go @@ -51,6 +51,12 @@ type RemoveUsersReq struct { } type SearchOrgUserReq struct { - OrgId uint `json:"org_id"` + OrgId uint `form:"org_id" json:"org_id"` base.Query } + +type MoveOrgUserReq struct { + SourceOrgId uint `json:"source_org_id"` + DestOrgId uint `json:"dest_org_id"` + UserIds []uint `json:"user_ids"` +} diff --git a/internal/repo/user_organization.go b/internal/repo/user_organization.go index 7d68e9b..e4b6b0a 100644 --- a/internal/repo/user_organization.go +++ b/internal/repo/user_organization.go @@ -4,6 +4,7 @@ package repo import ( "context" "time" + "fmt" "github.com/samber/do" "gorm.io/gorm" @@ -33,6 +34,7 @@ type UserOrganizationRepo interface{ Create(ctx context.Context,userOrgs []UserOrganization)error Delete(ctx context.Context,orgId uint,userIds []uint)error SearchOrgUser(ctx context.Context,orgId uint,query Query)([]User,error) + GetOrgUsers(ctx context.Context,orgId uint,userIds []uint)([]UserOrganization,error) } type userOrganizationRepo struct{ @@ -54,5 +56,20 @@ func (u *userOrganizationRepo)Delete(ctx context.Context,orgId uint,userIds []ui } func (u *userOrganizationRepo)SearchOrgUser(ctx context.Context,orgId uint,query Query)([]User,error){ - return nil,nil + users := make([]User,0) + db := u.db.Model(User{}).Joins("join user_organization on user_organization.user_id = user.id and user_organization.org_id = ?",orgId) + + if query.Keyword != "" { + keyword := fmt.Sprintf("%%%s%%", query.Keyword) + db = db.Where("user.name like ? or user.account like ? ", keyword,keyword) + } + + err := db.Order("user_organization.created_on desc").Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&users).Error + return users,err +} + +func (u *userOrganizationRepo)GetOrgUsers(ctx context.Context,orgId uint,userIds []uint)([]UserOrganization,error){ + uorgs := make([]UserOrganization,0) + err := u.db.Where("org_id = ? and user_id in ?",orgId,userIds).Find(&uorgs).Error + return uorgs,err } \ No newline at end of file diff --git a/internal/service/organization/interface.go b/internal/service/organization/interface.go index 08f2138..3ac8404 100644 --- a/internal/service/organization/interface.go +++ b/internal/service/organization/interface.go @@ -18,4 +18,5 @@ type OrganizationService interface { AddUser(ctx context.Context, info *orgModel.AddUsersReq) error RemoveUser(ctx context.Context, info *orgModel.RemoveUsersReq) error SearchOrgUsers(ctx context.Context, query *orgModel.SearchOrgUserReq) ([]userModel.UserInfo, error) + MoveOrgUsers(ctx context.Context, query *orgModel.MoveOrgUserReq) error } diff --git a/internal/service/organization/organization.go b/internal/service/organization/organization.go index d5d5214..8d1b2ed 100644 --- a/internal/service/organization/organization.go +++ b/internal/service/organization/organization.go @@ -29,6 +29,7 @@ type organizationService struct { redis *redis.Redis conf *config.AppConfig userService user.UserService + uOrgRepo repo.UserOrganizationRepo } func NewOrganizationService(i *do.Injector) (OrganizationService, error) { @@ -38,6 +39,7 @@ func NewOrganizationService(i *do.Injector) (OrganizationService, error) { redis: do.MustInvoke[*redis.Redis](i), conf: do.MustInvoke[*config.AppConfig](i), userService: do.MustInvoke[user.UserService](i), + uOrgRepo: do.MustInvoke[repo.UserOrganizationRepo](i), }, nil } @@ -187,7 +189,7 @@ func (o *organizationService) EnableOrganization(ctx context.Context, info *orgM } func (o *organizationService) AddUser(ctx context.Context, info *orgModel.AddUsersReq) error { - org, err := o.orgRepo.GetById(ctx, info.OrgId) + _, err := o.orgRepo.GetById(ctx, info.OrgId) if err != nil && err != gorm.ErrRecordNotFound { return err } @@ -196,14 +198,85 @@ func (o *organizationService) AddUser(ctx context.Context, info *orgModel.AddUse return fmt.Errorf("组织%d不存在", info.OrgId) } - o.orgRepo.Create() - return nil + return o.addUser(ctx, info.OrgId, info.Ids) } func (o *organizationService) RemoveUser(ctx context.Context, info *orgModel.RemoveUsersReq) error { - return nil + _, err := o.orgRepo.GetById(ctx, info.OrgId) + if err != nil && err != gorm.ErrRecordNotFound { + return err + } + + if err == gorm.ErrRecordNotFound { + return fmt.Errorf("组织%d不存在", info.OrgId) + } + + return o.uOrgRepo.Delete(ctx, info.OrgId, info.Ids) } func (o *organizationService) SearchOrgUsers(ctx context.Context, query *orgModel.SearchOrgUserReq) ([]userModel.UserInfo, error) { - return nil, nil + query.Default() + dbQuery := repo.Query{} + copier.Copy(&dbQuery, query) + + users, err := o.uOrgRepo.SearchOrgUser(ctx, query.OrgId, dbQuery) + if err != nil { + return nil, err + } + + rUsers := make([]userModel.UserInfo, 0, len(users)) + copier.Copy(&rUsers, users) + return rUsers, nil +} + +func (o *organizationService) MoveOrgUsers(ctx context.Context, info *orgModel.MoveOrgUserReq) error { + if info.DestOrgId == info.SourceOrgId { + return nil + } + + _, err := o.orgRepo.GetById(ctx, info.SourceOrgId) + if err != nil && err != gorm.ErrRecordNotFound { + return err + } + + if err == gorm.ErrRecordNotFound { + return fmt.Errorf("来源组织%d不存在", info.SourceOrgId) + } + + _, err = o.orgRepo.GetById(ctx, info.DestOrgId) + if err != nil && err != gorm.ErrRecordNotFound { + return err + } + + if err == gorm.ErrRecordNotFound { + return fmt.Errorf("目标组织%d不存在", info.SourceOrgId) + } + + err = o.uOrgRepo.Delete(ctx, info.SourceOrgId, info.UserIds) + if err != nil { + return err + } + + // + return o.addUser(ctx, info.DestOrgId, info.UserIds) +} + +func (o *organizationService) addUser(ctx context.Context, orgId uint, userIds []uint) error { + users, err := o.userService.BatchGetUserByIDs(ctx, userIds) + if err != nil { + return err + } + + uOrgs, err := o.uOrgRepo.GetOrgUsers(ctx, orgId, userIds) + if err != nil { + return err + } + + ids := make([]uint, 0) + for _, item := range users { + ids = append(ids, item.Id) + } + + uOrg := genUserOrg(orgId, ids, uOrgs) + return o.uOrgRepo.Create(ctx, uOrg) } diff --git a/internal/service/organization/util.go b/internal/service/organization/util.go index 0415b1f..98e0086 100644 --- a/internal/service/organization/util.go +++ b/internal/service/organization/util.go @@ -1,8 +1,10 @@ package organization import ( + "busniess-user-center/internal/repo" "context" "fmt" + "strconv" ) // func getOrgParentsWihtSelf(orgId uint, allOrgs []repo.Organization) (orgs []repo.Organization) { @@ -178,3 +180,24 @@ func (o *organizationService) verfyUsers(ctx context.Context, accounts []string) return nil } + +func genUserOrg(orgId uint, userIds []uint, dbUserOrgs []repo.UserOrganization) []repo.UserOrganization { + uOrgMaps := make(map[string]repo.UserOrganization, 0) + for _, item := range dbUserOrgs { + key := strconv.FormatUint(uint64(item.OrgID), 10) + strconv.FormatUint(uint64(item.UserID), 10) + uOrgMaps[key] = item + } + + rUserOrgs := make([]repo.UserOrganization, 0) + for _, id := range userIds { + key := strconv.FormatUint(uint64(orgId), 10) + strconv.FormatUint(uint64(id), 10) + if _, ok := uOrgMaps[key]; !ok { + rUserOrgs = append(rUserOrgs, repo.UserOrganization{ + UserID: id, + OrgID: orgId, + }) + } + } + + return rUserOrgs +} diff --git a/pkg/validator/custom_rules.go b/pkg/validator/custom_rules.go index aea686c..25211ef 100644 --- a/pkg/validator/custom_rules.go +++ b/pkg/validator/custom_rules.go @@ -26,7 +26,7 @@ func tableName(fl validator.FieldLevel) bool { func sortVerify(fl validator.FieldLevel) bool { value := fl.Field().String() if value == "" { - return false + return true } if isOk := sortMatch.MatchString(value); isOk { diff --git a/server/organization/organization.go b/server/organization/organization.go index 45cc2bc..94a9785 100644 --- a/server/organization/organization.go +++ b/server/organization/organization.go @@ -41,7 +41,8 @@ func RegisterRoute(api *gin.RouterGroup) { api.POST("/enable", ginUtil.WrapNoRsp(server.EnableOrganization)) api.POST("/adduser", ginUtil.WrapNoRsp(server.AddUsers)) api.DELETE("/removeuser", ginUtil.WrapNoRsp(server.RemoveUsers)) - api.GET("/search_org_user", ginUtil.WrapNoRsp(server.RemoveUsers)) + api.GET("/search_org_user", ginUtil.Wrap(server.SearchOrgUsers)) + api.POST("/move_org_user", ginUtil.WrapNoRsp(server.MoveOrgUsers)) } func (u *OrganizationServer) Create(ctx context.Context, req *orgModel.CreateOrgReq) (err error) { @@ -88,3 +89,7 @@ func (u *OrganizationServer) RemoveUsers(ctx context.Context, info *orgModel.Rem func (u *OrganizationServer) SearchOrgUsers(ctx context.Context, query *orgModel.SearchOrgUserReq) ([]userModel.UserInfo, error) { return u.organizationService.SearchOrgUsers(ctx, query) } + +func (u *OrganizationServer) MoveOrgUsers(ctx context.Context, query *orgModel.MoveOrgUserReq) error { + return u.organizationService.MoveOrgUsers(ctx, query) +}