53 lines
1.4 KiB
Go
Raw Normal View History

package logic
import (
"context"
"fmt"
"muyu-apiserver/rpc/inventory/internal/svc"
"muyu-apiserver/rpc/inventory/pb"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type GetProductLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductLogic {
return &GetProductLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetProductLogic) GetProduct(in *pb.GetProductReq) (*pb.ProductInfo, error) {
product, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, in.ProductId)
if err != nil {
return nil, status.Error(codes.NotFound, "product not found")
}
return &pb.ProductInfo{
ProductId: product.ProductId,
ProductName: product.ProductName,
ImageUrl: product.ImageUrl,
Spec: product.Spec,
Color: product.Color,
UnitPieces: product.UnitPieces,
UnitRolls: product.UnitRolls,
StockQuantity: fmt.Sprintf("%.2f", product.StockQuantity),
Location: product.Location,
CostPrice: fmt.Sprintf("%.2f", product.CostPrice),
SalesPrice: fmt.Sprintf("%.2f", product.SalesPrice),
Remark: product.Remark.String,
Status: product.Status,
CreatedAt: product.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: product.UpdatedAt.Format("2006-01-02 15:04:05"),
}, nil
}