package service import ( "context" "kra/internal/biz" ) type DepartmentService struct{ uc *biz.DepartmentUsecase } func NewDepartmentService(uc *biz.DepartmentUsecase) *DepartmentService { return &DepartmentService{uc: uc} } func departmentResponse(value *biz.Department) *DepartmentResponse { var children []*DepartmentResponse if value.Children != nil { children = make([]*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 &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 *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 *DepartmentService) Departments(ctx context.Context, name string) ([]*DepartmentResponse, error) { items, err := s.uc.Departments(ctx, name) if err != nil { return nil, err } out := make([]*DepartmentResponse, 0, len(items)) for _, item := range items { out = append(out, departmentResponse(item)) } return out, nil } func (s *DepartmentService) Create(ctx context.Context, req *DepartmentRequest) error { return s.uc.CreateDepartment(ctx, departmentDomain(req)) } func (s *DepartmentService) Update(ctx context.Context, req *DepartmentRequest) error { return s.uc.UpdateDepartment(ctx, departmentDomain(req)) } func (s *DepartmentService) Delete(ctx context.Context, id uint) error { return s.uc.DeleteDepartment(ctx, id) } func (s *DepartmentService) Department(ctx context.Context, id uint) (*DepartmentResponse, error) { value, err := s.uc.Department(ctx, id) if err != nil { return nil, err } return departmentResponse(value), nil } func (s *DepartmentService) UserIDs(ctx context.Context, id uint) ([]uint, error) { return s.uc.DepartmentUserIDs(ctx, id) } func (s *DepartmentService) SetUsers(ctx context.Context, req *SetDepartmentUsersRequest) error { return s.uc.SetDepartmentUsers(ctx, req.DepartmentID, req.UserIDs) } func (s *DepartmentService) SetUserDepartments(ctx context.Context, req *SetUserDepartmentsRequest) error { return s.uc.SetUserDepartments(ctx, req.ID, req.DepartmentIDs, req.Primary) }