33 lines
776 B
Go
33 lines
776 B
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"lone-services/pkg/rpcclient"
|
|
"lone-services/pkg/utils"
|
|
productpb "lone-services/rpc/product/pb"
|
|
)
|
|
|
|
// GetProductById 通过产品服务 InfoById 获取产品详情
|
|
func GetProductById(ctx context.Context, id int) (*productpb.Item, error) {
|
|
svcName := utils.GetConfigString("services.product")
|
|
if svcName == utils.StringEmpty {
|
|
return nil, fmt.Errorf("config services.product empty")
|
|
}
|
|
cli, err := rpcclient.Get(svcName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
item, err := productpb.NewProductClient(cli.Conn()).InfoById(ctx, &productpb.InfoByIdReq{
|
|
Id: int64(id),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if item == nil || item.GetId() < 1 {
|
|
return nil, fmt.Errorf("product not found: %d", id)
|
|
}
|
|
return item, nil
|
|
}
|