package service import ( "context" "strings" "kra/internal/biz" "kra/internal/service/dto" ) type AccessService struct{ uc *biz.AccessUsecase } func NewAccessService(uc *biz.AccessUsecase) *AccessService { return &AccessService{uc: uc} } func (s *AccessService) Authorize(ctx context.Context, aid uint, path, method string) (bool, error) { return s.uc.Authorize(ctx, aid, path, method) } func authorityDTO(v *biz.Authority) map[string]any { return convertAuthority(*v) } func apiDTO(v *biz.API) map[string]any { return map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "UpdatedAt": v.UpdatedAt, "DeletedAt": nil, "path": v.Path, "description": v.Description, "apiGroup": v.APIGroup, "method": v.Method} } func departmentDTO(v *biz.Department) map[string]any { children := make([]map[string]any, 0, len(v.Children)) for _, c := range v.Children { children = append(children, departmentDTO(c)) } return map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "UpdatedAt": v.UpdatedAt, "DeletedAt": nil, "name": v.Name, "parentId": v.ParentID, "ancestors": v.Ancestors, "sort": v.Sort, "leaderId": v.LeaderID, "leader": nil, "status": v.Status, "children": children, "namePath": ""} } func positionDTO(v *biz.Position) map[string]any { return map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "UpdatedAt": v.UpdatedAt, "DeletedAt": nil, "name": v.Name, "code": v.Code, "sort": v.Sort, "status": v.Status, "remark": v.Remark} } func (s *AccessService) Authorities(ctx context.Context) ([]map[string]any, error) { items, err := s.uc.Repo().ListAuthorities(ctx) if err != nil { return nil, err } byID := make(map[uint]map[string]any, len(items)) for _, v := range items { byID[v.AuthorityID] = authorityDTO(v) } roots := make([]map[string]any, 0) for _, v := range items { item := byID[v.AuthorityID] if v.ParentID != nil && *v.ParentID != 0 && byID[*v.ParentID] != nil { parent := byID[*v.ParentID] children, _ := parent["children"].([]map[string]any) parent["children"] = append(children, item) } else { roots = append(roots, item) } } return roots, nil } func (s *AccessService) CreateAuthority(ctx context.Context, v *biz.Authority) error { return s.uc.Repo().CreateAuthority(ctx, v) } func (s *AccessService) CopyAuthority(ctx context.Context, sourceID uint, v *biz.Authority) error { return s.uc.Repo().CopyAuthority(ctx, sourceID, v) } func (s *AccessService) UpdateAuthority(ctx context.Context, v *biz.Authority) error { return s.uc.Repo().UpdateAuthority(ctx, v) } func (s *AccessService) DeleteAuthority(ctx context.Context, id uint) error { return s.uc.Repo().DeleteAuthority(ctx, id) } func authorityDomain(req *dto.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) *dto.AuthorityResponse { return &dto.AuthorityResponse{AuthorityID: value.AuthorityID, AuthorityName: value.AuthorityName, ParentID: value.ParentID, DataScope: value.DataScope, DefaultRouter: value.DefaultRouter} } func (s *AccessService) CreateAuthorityRequest(ctx context.Context, req *dto.AuthorityRequest) (*dto.AuthorityResponse, error) { value := authorityDomain(req) if err := s.CreateAuthority(ctx, value); err != nil { return nil, err } return authorityResponse(value), nil } func (s *AccessService) CopyAuthorityRequest(ctx context.Context, req *dto.CopyAuthorityRequest) (*dto.AuthorityResponse, error) { value := authorityDomain(&req.Authority) if err := s.CopyAuthority(ctx, req.OldAuthorityID, value); err != nil { return nil, err } return authorityResponse(value), nil } func (s *AccessService) UpdateAuthorityRequest(ctx context.Context, req *dto.AuthorityRequest) (*dto.AuthorityResponse, error) { value := authorityDomain(req) if err := s.UpdateAuthority(ctx, value); err != nil { return nil, err } return authorityResponse(value), nil } func (s *AccessService) SetAuthorityUsers(ctx context.Context, id uint, ids []uint) error { return s.uc.Repo().SetAuthorityUsers(ctx, id, ids) } func (s *AccessService) AuthorityUserIDs(ctx context.Context, id uint) ([]uint, error) { return s.uc.Repo().AuthorityUserIDs(ctx, id) } func (s *AccessService) SetDataScope(ctx context.Context, id uint, scope int, ids []uint) error { return s.uc.Repo().SetDataScope(ctx, id, scope, ids) } func (s *AccessService) DataScopeDepartmentIDs(ctx context.Context, id uint) ([]uint, error) { return s.uc.Repo().DataScopeDepartmentIDs(ctx, id) } func (s *AccessService) ResolveDataScope(ctx context.Context, authorityID, userID uint) (biz.DataScope, error) { return s.uc.Repo().ResolveDataScope(ctx, authorityID, userID) } func (s *AccessService) DeleteAPIs(ctx context.Context, ids []uint) error { return s.uc.Repo().DeleteAPIs(ctx, ids) } func (s *AccessService) APIRoleIDs(ctx context.Context, id uint) ([]uint, error) { return s.uc.Repo().APIRoleIDs(ctx, id) } func (s *AccessService) SetAPIRoles(ctx context.Context, id uint, ids []uint) error { return s.uc.Repo().SetAPIRoles(ctx, id, ids) } func (s *AccessService) SyncAPIs(ctx context.Context, routes []*biz.API) (*dto.APISyncResponse, error) { stored, _, err := s.uc.Repo().ListAPIs(ctx, 1, 100000, nil) if err != nil { return nil, err } ignored, err := s.uc.Repo().IgnoredAPIs(ctx) if err != nil { return nil, err } key := func(v *biz.API) string { return strings.ToUpper(v.Method) + " " + v.Path } ignoreSet := make(map[string]bool, len(ignored)) for _, item := range ignored { ignoreSet[key(item)] = true } routeSet := make(map[string]*biz.API, len(routes)) for _, item := range routes { if !ignoreSet[key(item)] { routeSet[key(item)] = item } } storedSet := make(map[string]*biz.API, len(stored)) for _, item := range stored { storedSet[key(item)] = item } added := make([]*dto.APIResponse, 0) deleted := make([]*dto.APIResponse, 0) ignoredDTO := make([]*dto.APIResponse, 0, len(ignored)) for k, item := range routeSet { if storedSet[k] == nil { added = append(added, apiResponse(item)) } } for k, item := range storedSet { if routeSet[k] == nil && !ignoreSet[k] { deleted = append(deleted, apiResponse(item)) } } for _, item := range ignored { ignoredDTO = append(ignoredDTO, apiResponse(item)) } return &dto.APISyncResponse{NewAPIs: added, DeleteAPIs: deleted, IgnoreAPIs: ignoredDTO}, nil } func (s *AccessService) SetAPIIgnored(ctx context.Context, path, method string, ignored bool) error { return s.uc.Repo().SetAPIIgnored(ctx, path, method, ignored) } func (s *AccessService) Buttons(ctx context.Context, id uint) ([]map[string]any, error) { items, err := s.uc.Repo().Buttons(ctx, id) if err != nil { return nil, err } out := make([]map[string]any, 0, len(items)) for _, v := range items { out = append(out, map[string]any{"ID": v.ID, "name": v.Name, "desc": v.Description, "sysBaseMenuID": v.MenuID}) } return out, nil } func (s *AccessService) SetAuthorityButtons(ctx context.Context, id uint, v map[uint][]uint) error { return s.uc.Repo().SetAuthorityButtons(ctx, id, v) } func (s *AccessService) AuthorityButtonIDs(ctx context.Context, id uint) ([]uint, error) { return s.uc.Repo().AuthorityButtonIDs(ctx, id) } func (s *AccessService) SelectedButtons(ctx context.Context, aid, menuID uint) ([]uint, error) { return s.uc.Repo().SelectedButtons(ctx, aid, menuID) } func (s *AccessService) SetSelectedButtons(ctx context.Context, aid, menuID uint, ids []uint) error { return s.uc.Repo().SetSelectedButtons(ctx, aid, menuID, ids) } func (s *AccessService) CanRemoveButton(ctx context.Context, id uint) (bool, error) { return s.uc.Repo().CanRemoveButton(ctx, id) }