95 lines
3.2 KiB
Go
95 lines
3.2 KiB
Go
package service
|
|
|
|
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 authorityResponse(value *system.Authority) *dto.AuthorityResponse {
|
|
var menus []*dto.MenuResponse
|
|
if value.Menus != nil {
|
|
menus = menuResponses(value.Menus)
|
|
}
|
|
return &dto.AuthorityResponse{CreatedAt: value.CreatedAt, UpdatedAt: value.UpdatedAt, DeletedAt: value.DeletedAt, AuthorityID: value.AuthorityID, AuthorityName: value.AuthorityName, ParentID: value.ParentID, Children: nil, Menus: menus, DataScope: value.DataScope, DefaultRouter: value.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 authorityResponse(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 := authorityResponse(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 authorityResponse(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)
|
|
}
|