kra-new/internal/service/system/authority.go

87 lines
2.7 KiB
Go

package system
import (
"context"
"kra/internal/biz/system"
"kra/internal/service/dto"
)
type AuthorityService struct{ uc *system.AuthorityUsecase }
func NewAuthorityService(uc *system.AuthorityUsecase) *AuthorityService {
return &AuthorityService{uc: uc}
}
func authorityDTO(value *system.Authority) *dto.AuthorityResponse {
out := convertAuthority(*value)
children := make([]*dto.AuthorityResponse, 0, len(value.Children))
for _, child := range value.Children {
children = append(children, authorityDTO(child))
}
out.Children = children
return out
}
func authorityDomain(req *dto.AuthorityRequest) *system.Authority {
return &system.Authority{AuthorityID: req.AuthorityID, AuthorityName: req.AuthorityName, ParentID: req.ParentID, DataScope: req.DataScope, DefaultRouter: req.DefaultRouter}
}
func (s *AuthorityService) Authorities(ctx context.Context) ([]*dto.AuthorityResponse, error) {
items, err := s.uc.AuthorityTree(ctx)
if err != nil {
return nil, err
}
roots := make([]*dto.AuthorityResponse, 0, len(items))
for _, value := range items {
roots = append(roots, authorityDTO(value))
}
return roots, nil
}
func (s *AuthorityService) CreateAuthorityRequest(ctx context.Context, req *dto.AuthorityRequest) (*dto.AuthorityResponse, error) {
value := authorityDomain(req)
if err := s.uc.CreateAuthority(ctx, value); err != nil {
return nil, err
}
return convertAuthority(*value), nil
}
func (s *AuthorityService) CopyAuthorityRequest(ctx context.Context, req *dto.CopyAuthorityRequest) (*dto.AuthorityResponse, error) {
value := authorityDomain(&req.Authority)
if err := s.uc.CopyAuthority(ctx, req.OldAuthorityID, value); err != nil {
return nil, err
}
result := convertAuthority(*value)
result.Children = []*dto.AuthorityResponse{}
return result, nil
}
func (s *AuthorityService) UpdateAuthorityRequest(ctx context.Context, req *dto.AuthorityRequest) (*dto.AuthorityResponse, error) {
value := authorityDomain(req)
if err := s.uc.UpdateAuthority(ctx, value); err != nil {
return nil, err
}
return convertAuthority(*value), nil
}
func (s *AuthorityService) DeleteAuthority(ctx context.Context, id uint) error {
return s.uc.DeleteAuthority(ctx, id)
}
func (s *AuthorityService) SetAuthorityUsers(ctx context.Context, id uint, ids []uint) error {
return s.uc.SetAuthorityUsers(ctx, id, ids)
}
func (s *AuthorityService) AuthorityUserIDs(ctx context.Context, id uint) ([]uint, error) {
return s.uc.AuthorityUserIDs(ctx, id)
}
func (s *AuthorityService) SetDataScope(ctx context.Context, id uint, scope int, ids []uint) error {
return s.uc.SetDataScope(ctx, id, scope, ids)
}
func (s *AuthorityService) DataScopeDepartmentIDs(ctx context.Context, id uint) ([]uint, error) {
return s.uc.DataScopeDepartmentIDs(ctx, id)
}