package service import ( "context" "kra/internal/biz" "kra/internal/service/dto" ) type OrganizationService struct{ uc *biz.OrganizationUsecase } func NewOrganizationService(uc *biz.OrganizationUsecase) *OrganizationService { return &OrganizationService{uc: uc} } func departmentResponse(value *biz.Department) *dto.DepartmentResponse { children := make([]*dto.DepartmentResponse, 0, len(value.Children)) for _, child := range value.Children { children = append(children, departmentResponse(child)) } var leader any if value.Leader != nil { leader = convertUser(value.Leader) } return &dto.DepartmentResponse{ID: value.ID, CreatedAt: value.CreatedAt, UpdatedAt: value.UpdatedAt, DeletedAt: nil, Name: value.Name, ParentID: value.ParentID, Ancestors: value.Ancestors, Sort: value.Sort, LeaderID: value.LeaderID, Leader: leader, Status: value.Status, Children: children, NamePath: value.NamePath} } func departmentDomain(value *dto.DepartmentRequest) *biz.Department { return &biz.Department{ID: value.ID, Name: value.Name, ParentID: value.ParentID, Sort: value.Sort, LeaderID: value.LeaderID, Status: value.Status} } func (s *OrganizationService) Departments(ctx context.Context, name string) ([]*dto.DepartmentResponse, error) { items, err := s.uc.Departments(ctx, name) if err != nil { return nil, err } out := make([]*dto.DepartmentResponse, 0, len(items)) for _, item := range items { out = append(out, departmentResponse(item)) } return out, nil } func (s *OrganizationService) CreateDepartment(ctx context.Context, req *dto.DepartmentRequest) error { return s.uc.CreateDepartment(ctx, departmentDomain(req)) } func (s *OrganizationService) UpdateDepartment(ctx context.Context, req *dto.DepartmentRequest) error { return s.uc.UpdateDepartment(ctx, departmentDomain(req)) } func (s *OrganizationService) DeleteDepartment(ctx context.Context, id uint) error { return s.uc.DeleteDepartment(ctx, id) } func (s *OrganizationService) Department(ctx context.Context, id uint) (*dto.DepartmentResponse, error) { value, err := s.uc.Department(ctx, id) if err != nil { return nil, err } return departmentResponse(value), nil } func (s *OrganizationService) DepartmentUserIDs(ctx context.Context, id uint) ([]uint, error) { return s.uc.DepartmentUserIDs(ctx, id) } func (s *OrganizationService) SetDepartmentUsers(ctx context.Context, req *dto.SetDepartmentUsersRequest) error { return s.uc.SetDepartmentUsers(ctx, req.DepartmentID, req.UserIDs) } func (s *OrganizationService) SetUserDepartments(ctx context.Context, req *dto.SetUserDepartmentsRequest) error { return s.uc.SetUserDepartments(ctx, req.ID, req.DepartmentIDs, req.Primary) } func positionDomain(value *dto.PositionRequest) *biz.Position { return &biz.Position{ID: value.ID, Name: value.Name, Code: value.Code, Sort: value.Sort, Status: value.Status, Remark: value.Remark} } func positionResponse(value *biz.Position) *dto.PositionResponse { return &dto.PositionResponse{ID: value.ID, CreatedAt: value.CreatedAt, UpdatedAt: value.UpdatedAt, DeletedAt: nil, Name: value.Name, Code: value.Code, Sort: value.Sort, Status: value.Status, Remark: value.Remark} } func (s *OrganizationService) Positions(ctx context.Context, req *dto.PositionListRequest) ([]*dto.PositionResponse, int64, error) { items, total, err := s.uc.Positions(ctx, req.Page, req.PageSize, &biz.PositionListFilter{Name: req.Name, Code: req.Code, Status: req.Status}) if err != nil { return nil, 0, err } out := make([]*dto.PositionResponse, 0, len(items)) for _, item := range items { out = append(out, positionResponse(item)) } return out, total, nil } func (s *OrganizationService) CreatePosition(ctx context.Context, req *dto.PositionRequest) error { return s.uc.CreatePosition(ctx, positionDomain(req)) } func (s *OrganizationService) UpdatePosition(ctx context.Context, req *dto.PositionRequest) error { return s.uc.UpdatePosition(ctx, positionDomain(req)) } func (s *OrganizationService) DeletePosition(ctx context.Context, id uint) error { return s.uc.DeletePosition(ctx, id) } func (s *OrganizationService) Position(ctx context.Context, id uint) (*dto.PositionResponse, error) { value, err := s.uc.Position(ctx, id) if err != nil { return nil, err } return positionResponse(value), nil } func (s *OrganizationService) PositionUserIDs(ctx context.Context, id uint) ([]uint, error) { return s.uc.PositionUserIDs(ctx, id) } func (s *OrganizationService) SetPositionUsers(ctx context.Context, req *dto.SetPositionUsersRequest) error { return s.uc.SetPositionUsers(ctx, req.PositionID, req.UserIDs) } func (s *OrganizationService) SetUserPositions(ctx context.Context, req *dto.SetUserPositionsRequest) error { return s.uc.SetUserPositions(ctx, req.ID, req.PositionIDs) }