package service import ( "context" "strconv" "git.nobla.cn/golang/aeus-admin/models" "git.nobla.cn/golang/aeus-admin/pb" "gorm.io/gorm" ) type ( departmentOptions struct { db *gorm.DB } DepartmentOption func(o *departmentOptions) DepartmentService struct { opts *departmentOptions } ) func WithDepartmentDB(db *gorm.DB) DepartmentOption { return func(o *departmentOptions) { o.db = db } } func (s *DepartmentService) GetDepartmentLabels(ctx context.Context, req *pb.GetDepartmentLabelRequest) (res *pb.GetDepartmentLabelResponse, err error) { values := make([]*models.Department, 0) if err = s.opts.db.WithContext(ctx).Find(&values).Error; err == nil { res = &pb.GetDepartmentLabelResponse{ Data: make([]*pb.LabelValue, 0, len(values)), } for _, v := range values { res.Data = append(res.Data, &pb.LabelValue{ Label: v.Name, Value: strconv.FormatInt(v.Id, 10), }) } } return } func NewDepartmentService(cbs ...DepartmentOption) *DepartmentService { opts := &departmentOptions{} for _, cb := range cbs { cb(opts) } return &DepartmentService{ opts: opts, } }