组织用户处理接口
This commit is contained in:
parent
88771a470a
commit
0418bc4ab0
|
|
@ -51,6 +51,12 @@ type RemoveUsersReq struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type SearchOrgUserReq struct {
|
type SearchOrgUserReq struct {
|
||||||
OrgId uint `json:"org_id"`
|
OrgId uint `form:"org_id" json:"org_id"`
|
||||||
base.Query
|
base.Query
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MoveOrgUserReq struct {
|
||||||
|
SourceOrgId uint `json:"source_org_id"`
|
||||||
|
DestOrgId uint `json:"dest_org_id"`
|
||||||
|
UserIds []uint `json:"user_ids"`
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ package repo
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/samber/do"
|
"github.com/samber/do"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
@ -33,6 +34,7 @@ type UserOrganizationRepo interface{
|
||||||
Create(ctx context.Context,userOrgs []UserOrganization)error
|
Create(ctx context.Context,userOrgs []UserOrganization)error
|
||||||
Delete(ctx context.Context,orgId uint,userIds []uint)error
|
Delete(ctx context.Context,orgId uint,userIds []uint)error
|
||||||
SearchOrgUser(ctx context.Context,orgId uint,query Query)([]User,error)
|
SearchOrgUser(ctx context.Context,orgId uint,query Query)([]User,error)
|
||||||
|
GetOrgUsers(ctx context.Context,orgId uint,userIds []uint)([]UserOrganization,error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type userOrganizationRepo struct{
|
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){
|
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
|
||||||
}
|
}
|
||||||
|
|
@ -18,4 +18,5 @@ type OrganizationService interface {
|
||||||
AddUser(ctx context.Context, info *orgModel.AddUsersReq) error
|
AddUser(ctx context.Context, info *orgModel.AddUsersReq) error
|
||||||
RemoveUser(ctx context.Context, info *orgModel.RemoveUsersReq) error
|
RemoveUser(ctx context.Context, info *orgModel.RemoveUsersReq) error
|
||||||
SearchOrgUsers(ctx context.Context, query *orgModel.SearchOrgUserReq) ([]userModel.UserInfo, error)
|
SearchOrgUsers(ctx context.Context, query *orgModel.SearchOrgUserReq) ([]userModel.UserInfo, error)
|
||||||
|
MoveOrgUsers(ctx context.Context, query *orgModel.MoveOrgUserReq) error
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ type organizationService struct {
|
||||||
redis *redis.Redis
|
redis *redis.Redis
|
||||||
conf *config.AppConfig
|
conf *config.AppConfig
|
||||||
userService user.UserService
|
userService user.UserService
|
||||||
|
uOrgRepo repo.UserOrganizationRepo
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOrganizationService(i *do.Injector) (OrganizationService, error) {
|
func NewOrganizationService(i *do.Injector) (OrganizationService, error) {
|
||||||
|
|
@ -38,6 +39,7 @@ func NewOrganizationService(i *do.Injector) (OrganizationService, error) {
|
||||||
redis: do.MustInvoke[*redis.Redis](i),
|
redis: do.MustInvoke[*redis.Redis](i),
|
||||||
conf: do.MustInvoke[*config.AppConfig](i),
|
conf: do.MustInvoke[*config.AppConfig](i),
|
||||||
userService: do.MustInvoke[user.UserService](i),
|
userService: do.MustInvoke[user.UserService](i),
|
||||||
|
uOrgRepo: do.MustInvoke[repo.UserOrganizationRepo](i),
|
||||||
}, nil
|
}, 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 {
|
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 {
|
if err != nil && err != gorm.ErrRecordNotFound {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -196,14 +198,85 @@ func (o *organizationService) AddUser(ctx context.Context, info *orgModel.AddUse
|
||||||
return fmt.Errorf("组织%d不存在", info.OrgId)
|
return fmt.Errorf("组织%d不存在", info.OrgId)
|
||||||
}
|
}
|
||||||
|
|
||||||
o.orgRepo.Create()
|
return o.addUser(ctx, info.OrgId, info.Ids)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *organizationService) RemoveUser(ctx context.Context, info *orgModel.RemoveUsersReq) error {
|
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) {
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
package organization
|
package organization
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"busniess-user-center/internal/repo"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// func getOrgParentsWihtSelf(orgId uint, allOrgs []repo.Organization) (orgs []repo.Organization) {
|
// 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
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ func tableName(fl validator.FieldLevel) bool {
|
||||||
func sortVerify(fl validator.FieldLevel) bool {
|
func sortVerify(fl validator.FieldLevel) bool {
|
||||||
value := fl.Field().String()
|
value := fl.Field().String()
|
||||||
if value == "" {
|
if value == "" {
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if isOk := sortMatch.MatchString(value); isOk {
|
if isOk := sortMatch.MatchString(value); isOk {
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,8 @@ func RegisterRoute(api *gin.RouterGroup) {
|
||||||
api.POST("/enable", ginUtil.WrapNoRsp(server.EnableOrganization))
|
api.POST("/enable", ginUtil.WrapNoRsp(server.EnableOrganization))
|
||||||
api.POST("/adduser", ginUtil.WrapNoRsp(server.AddUsers))
|
api.POST("/adduser", ginUtil.WrapNoRsp(server.AddUsers))
|
||||||
api.DELETE("/removeuser", ginUtil.WrapNoRsp(server.RemoveUsers))
|
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) {
|
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) {
|
func (u *OrganizationServer) SearchOrgUsers(ctx context.Context, query *orgModel.SearchOrgUserReq) ([]userModel.UserInfo, error) {
|
||||||
return u.organizationService.SearchOrgUsers(ctx, query)
|
return u.organizationService.SearchOrgUsers(ctx, query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *OrganizationServer) MoveOrgUsers(ctx context.Context, query *orgModel.MoveOrgUserReq) error {
|
||||||
|
return u.organizationService.MoveOrgUsers(ctx, query)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue