组织列表接口
This commit is contained in:
parent
62d5b5f9b3
commit
b4956ea2be
|
|
@ -11,7 +11,7 @@ type Organization struct {
|
|||
Path string `json:"path"` // 全路径
|
||||
Leaders []string `json:"leaders"`
|
||||
CreatedOn time.Time `json:"created_on"` // 记录创建时间
|
||||
Children []Organization `json:"children"`
|
||||
Children []*Organization `json:"children"`
|
||||
}
|
||||
|
||||
type OrgTree []Organization
|
||||
type OrgTree []*Organization
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ package organization
|
|||
|
||||
type CreateOrgReq struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
ParentId uint `json:"parent_id" binding:"required"`
|
||||
Sort int `json:"sort" binding:"required"`
|
||||
ParentId uint `json:"parent_id"`
|
||||
Sort int `json:"sort"`
|
||||
Leaders []string `json:"leaders"`
|
||||
}
|
||||
|
||||
|
|
@ -12,7 +12,7 @@ type DelOrgReq struct {
|
|||
}
|
||||
|
||||
type GetOrgReq struct {
|
||||
Id uint `from:"id" json:"id" binding:"required"`
|
||||
Id uint `form:"id" json:"id" binding:"required"`
|
||||
}
|
||||
|
||||
type MoveOrgReq struct {
|
||||
|
|
@ -23,8 +23,8 @@ type MoveOrgReq struct {
|
|||
type SaveOrgReq struct {
|
||||
Id uint `json:"id" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
ParentId uint `json:"parent_id" binding:"required"`
|
||||
Sort int `json:"sort" binding:"required"`
|
||||
ParentId uint `json:"parent_id" `
|
||||
Sort int `json:"sort" `
|
||||
Leaders []string `json:"leaders"`
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ type OrganizationRepo interface{
|
|||
Save(ctx context.Context,org Organization)(error)
|
||||
GetById(ctx context.Context,id uint)(org Organization,err error)
|
||||
DelById(ctx context.Context,id uint)(err error)
|
||||
GetOrgs(ctx context.Context)(org []Organization,err error)
|
||||
GetOrgs(ctx context.Context)(org []*Organization,err error)
|
||||
ExistOrgId(ctx context.Context,id uint)(bool,error)
|
||||
SetStatus(ctx context.Context,id uint,status OrganizationStatus)error
|
||||
}
|
||||
|
|
@ -60,7 +60,7 @@ func NewOrganization(i *do.Injector)(OrganizationRepo,error){
|
|||
}
|
||||
|
||||
func (o *orginizationRepo)Create(ctx context.Context,org Organization)(id uint,err error){
|
||||
err = o.db.Create(org).Error
|
||||
err = o.db.Create(&org).Error
|
||||
id = org.ID
|
||||
return
|
||||
}
|
||||
|
|
@ -75,24 +75,24 @@ func (o *orginizationRepo)GetById(ctx context.Context,id uint)(org Organization,
|
|||
}
|
||||
|
||||
func (o *orginizationRepo)DelById(ctx context.Context,id uint)(err error){
|
||||
return o.db.Where("id = ?",id).Delete(&Organization{}).Error
|
||||
return o.db.Model(Organization{}).Where("id = ?",id).Delete(&Organization{}).Error
|
||||
}
|
||||
|
||||
func (o *orginizationRepo)GetOrgs(ctx context.Context)(orgs []Organization,err error){
|
||||
func (o *orginizationRepo)GetOrgs(ctx context.Context)(orgs []*Organization,err error){
|
||||
err = o.db.Model(&Organization{}).Find(&orgs).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (o *orginizationRepo)ExistOrgId(ctx context.Context,id uint)(bool,error){
|
||||
var count int64 = 0
|
||||
err := o.db.Where("id = ?",id).Count(&count).Error
|
||||
err := o.db.Model(Organization{}).Where("id = ?",id).Count(&count).Error
|
||||
if count >0{
|
||||
return false,err
|
||||
}
|
||||
|
||||
return true,err
|
||||
}
|
||||
|
||||
func (o *orginizationRepo)SetStatus(ctx context.Context,id uint,status OrganizationStatus)error{
|
||||
return o.db.Where("id = ?",id).Update("status",status).Error
|
||||
return false,err
|
||||
}
|
||||
|
||||
func (o *orginizationRepo)SetStatus(ctx context.Context,id uint,status OrganizationStatus)error{
|
||||
return o.db.Model(Organization{}).Where("id = ?",id).Update("status",status).Error
|
||||
}
|
||||
|
|
@ -6,8 +6,11 @@ import (
|
|||
"busniess-user-center/internal/repo"
|
||||
"busniess-user-center/pkg/redis"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"busniess-user-center/internal/service/user"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/samber/do"
|
||||
"go.uber.org/zap"
|
||||
|
|
@ -23,6 +26,7 @@ type organizationService struct {
|
|||
orgRepo repo.OrganizationRepo
|
||||
redis *redis.Redis
|
||||
conf *config.AppConfig
|
||||
userService user.UserService
|
||||
}
|
||||
|
||||
func NewOrganizationService(i *do.Injector) (OrganizationService, error) {
|
||||
|
|
@ -31,6 +35,7 @@ func NewOrganizationService(i *do.Injector) (OrganizationService, error) {
|
|||
orgRepo: do.MustInvoke[repo.OrganizationRepo](i),
|
||||
redis: do.MustInvoke[*redis.Redis](i),
|
||||
conf: do.MustInvoke[*config.AppConfig](i),
|
||||
userService: do.MustInvoke[user.UserService](i),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -43,11 +48,22 @@ func (o *organizationService) CreateOrganization(ctx context.Context, info *orgM
|
|||
}
|
||||
}
|
||||
|
||||
leader := "[]"
|
||||
if len(info.Leaders) > 0 {
|
||||
if err := o.verfyUsers(ctx, info.Leaders); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
leaderByte, _ := json.Marshal(info.Leaders)
|
||||
leader = string(leaderByte)
|
||||
}
|
||||
|
||||
dbOrg := repo.Organization{
|
||||
Name: info.Name,
|
||||
ParentID: info.ParentId,
|
||||
Sort: uint(info.Sort),
|
||||
Status: int(repo.OrganizationEnableStatus),
|
||||
Leaders: leader,
|
||||
}
|
||||
|
||||
_, err := o.orgRepo.Create(ctx, dbOrg)
|
||||
|
|
@ -65,8 +81,31 @@ func (o *organizationService) SaveOrganization(ctx context.Context, info *orgMod
|
|||
return fmt.Errorf("组织不存在")
|
||||
}
|
||||
|
||||
dbOrg.Name = info.Name
|
||||
err = o.orgRepo.Save(ctx, dbOrg)
|
||||
newOrg := repo.Organization{}
|
||||
copier.Copy(&newOrg, dbOrg)
|
||||
newOrg.Name = info.Name
|
||||
newOrg.Sort = uint(info.Sort)
|
||||
|
||||
if info.ParentId > 0 {
|
||||
if exist, err := o.orgRepo.ExistOrgId(ctx, info.ParentId); err != nil {
|
||||
return err
|
||||
} else if !exist {
|
||||
return fmt.Errorf("父组织%d不存在", info.ParentId)
|
||||
}
|
||||
|
||||
newOrg.ParentID = info.ParentId
|
||||
}
|
||||
|
||||
if len(info.Leaders) > 0 {
|
||||
if err := o.verfyUsers(ctx, info.Leaders); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
leaderByte, _ := json.Marshal(info.Leaders)
|
||||
newOrg.Leaders = string(leaderByte)
|
||||
}
|
||||
|
||||
err = o.orgRepo.Save(ctx, newOrg)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -83,12 +122,16 @@ func (o *organizationService) Organization(ctx context.Context, info *orgModel.G
|
|||
return
|
||||
}
|
||||
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
err = fmt.Errorf("组织不存在")
|
||||
return
|
||||
}
|
||||
|
||||
copier.Copy(&org, dbOrg)
|
||||
if len(dbOrg.Leaders) > 0 {
|
||||
json.Unmarshal([]byte(dbOrg.Leaders), &org.Leaders)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ package organization
|
|||
import (
|
||||
orgModel "busniess-user-center/internal/models/organization"
|
||||
"busniess-user-center/internal/repo"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
|
|
@ -114,20 +116,20 @@ func getChildren(orgId uint, orgChildrenMap map[uint][]repo.Organization) (orgs
|
|||
return
|
||||
}
|
||||
|
||||
func getOrgTree(orgs []repo.Organization) orgModel.OrgTree {
|
||||
func getOrgTree(orgs []*repo.Organization) orgModel.OrgTree {
|
||||
// top
|
||||
topOrgs := []orgModel.Organization{}
|
||||
parentMap := make(map[uint][]orgModel.Organization)
|
||||
topOrgs := []*orgModel.Organization{}
|
||||
parentMap := make(map[uint][]*orgModel.Organization)
|
||||
for _, org := range orgs {
|
||||
mOrg := convertOrgDTM(org)
|
||||
if org.ParentID == 0 {
|
||||
topOrgs = append(topOrgs, mOrg)
|
||||
}
|
||||
|
||||
if _, ok := parentMap[org.ID]; !ok {
|
||||
parentMap[org.ID] = []orgModel.Organization{mOrg}
|
||||
} else {
|
||||
parentMap[org.ID] = append(parentMap[org.ID], mOrg)
|
||||
if _, ok := parentMap[org.ParentID]; !ok {
|
||||
parentMap[org.ParentID] = []*orgModel.Organization{mOrg}
|
||||
} else {
|
||||
parentMap[org.ParentID] = append(parentMap[org.ParentID], mOrg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -158,8 +160,8 @@ func getOrgTree(orgs []repo.Organization) orgModel.OrgTree {
|
|||
return topOrgs
|
||||
}
|
||||
|
||||
func convertOrgDTM(org repo.Organization) orgModel.Organization {
|
||||
return orgModel.Organization{
|
||||
func convertOrgDTM(org *repo.Organization) *orgModel.Organization {
|
||||
return &orgModel.Organization{
|
||||
ID: org.ID, // id
|
||||
Name: org.Name, // 组织名
|
||||
ParentID: org.ParentID, // 上级组织id
|
||||
|
|
@ -169,3 +171,13 @@ func convertOrgDTM(org repo.Organization) orgModel.Organization {
|
|||
CreatedOn: org.CreatedOn, // 记录创建时间
|
||||
}
|
||||
}
|
||||
|
||||
func (o *organizationService) verfyUsers(ctx context.Context, accounts []string) error {
|
||||
for _, account := range accounts {
|
||||
if _, err := o.userService.ExistUserByAccount(ctx, account); err != nil {
|
||||
return fmt.Errorf("用户%s不存在", account)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,4 +16,5 @@ type UserService interface {
|
|||
Search(ctx context.Context, query *userModel.Query) (*userModel.SearchRsp, error)
|
||||
ResetPwd(ctx context.Context, req *userModel.ResetPwdReq) error
|
||||
GetUser(ctx context.Context, req *userModel.GetUserReq) (user userModel.User, err error)
|
||||
ExistUserByAccount(ctx context.Context, account string) (uint, error)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -263,3 +263,16 @@ func (u *userService) GetUser(ctx context.Context, req *userModel.GetUserReq) (u
|
|||
user = convertUser(rUser)
|
||||
return
|
||||
}
|
||||
|
||||
func (u *userService) ExistUserByAccount(ctx context.Context, account string) (uint, error) {
|
||||
user, err := u.repo.GetUserByAccount(ctx, account)
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
return user.ID, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ func RegisterRoute(api *gin.RouterGroup) {
|
|||
server := do.MustInvoke[*OrganizationServer](nil)
|
||||
api.POST("/create", ginUtil.WrapNoRsp(server.Create))
|
||||
api.POST("/save", ginUtil.WrapNoRsp(server.SaveOrganization))
|
||||
api.POST("/delete", ginUtil.WrapNoRsp(server.DelOrganization))
|
||||
api.DELETE("/delete", ginUtil.WrapNoRsp(server.DelOrganization))
|
||||
api.GET("/get", ginUtil.Wrap(server.Organization))
|
||||
api.POST("/move", ginUtil.WrapNoRsp(server.MoveOrganization))
|
||||
api.GET("/orgs", ginUtil.WrapNoReq(server.OrganizationTree))
|
||||
|
|
|
|||
Loading…
Reference in New Issue