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

94 lines
3.1 KiB
Go

package service
import (
"context"
"kra/app/system/internal/biz"
)
type AuthorityService struct{ uc *biz.AuthorityUsecase }
func NewAuthorityService(uc *biz.AuthorityUsecase) *AuthorityService {
return &AuthorityService{uc: uc}
}
func authorityDTO(value *biz.Authority) *AuthorityResponse {
out := convertAuthority(*value)
children := make([]*AuthorityResponse, 0, len(value.Children))
for _, child := range value.Children {
children = append(children, authorityDTO(child))
}
out.Children = children
return out
}
func authorityDomain(req *AuthorityRequest) *biz.Authority {
return &biz.Authority{AuthorityID: req.AuthorityID, AuthorityName: req.AuthorityName, ParentID: req.ParentID, DataScope: req.DataScope, DefaultRouter: req.DefaultRouter}
}
func authorityResponse(value *biz.Authority) *AuthorityResponse {
var menus []*MenuResponse
if value.Menus != nil {
menus = menuResponses(value.Menus)
}
return &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) ([]*AuthorityResponse, error) {
items, err := s.uc.AuthorityTree(ctx)
if err != nil {
return nil, err
}
roots := make([]*AuthorityResponse, 0, len(items))
for _, value := range items {
roots = append(roots, authorityDTO(value))
}
return roots, nil
}
func (s *AuthorityService) CreateAuthorityRequest(ctx context.Context, req *AuthorityRequest) (*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 *CopyAuthorityRequest) (*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 = []*AuthorityResponse{}
return result, nil
}
func (s *AuthorityService) UpdateAuthorityRequest(ctx context.Context, req *AuthorityRequest) (*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)
}