58 lines
1.3 KiB
Go
Raw Normal View History

package product
import (
"context"
"muyu-apiserver/gateway/internal/svc"
"muyu-apiserver/gateway/internal/types"
"muyu-apiserver/rpc/inventory/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type CreateProductLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCreateProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateProductLogic {
return &CreateProductLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CreateProductLogic) CreateProduct(req *types.CreateProductReq) (resp *types.IdResp, err error) {
rpcResp, err := l.svcCtx.InventoryRpc.CreateProduct(l.ctx, &pb.CreateProductReq{
ProductName: req.ProductName,
ImageUrl: req.ImageUrl,
Spec: req.Spec,
Color: req.Color,
SalesPrice: req.SalesPrice,
Remark: req.Remark,
Pans: panInputsToPb(req.Pans),
})
if err != nil {
return nil, err
}
return &types.IdResp{Id: rpcResp.Id}, nil
}
func panInputsToPb(pans []types.PanInput) []*pb.PanInput {
if len(pans) == 0 {
return nil
}
out := make([]*pb.PanInput, 0, len(pans))
for _, p := range pans {
out = append(out, &pb.PanInput{
Name: p.Name,
Position: p.Position,
BoltLengths: p.BoltLengths,
})
}
return out
}