76 lines
1.8 KiB
Go
Raw Permalink 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,
ColorNo: req.ColorNo,
ProductCode: req.ProductCode,
SalesPrice: req.SalesPrice,
Remark: req.Remark,
Pans: panInputsToPb(req.Pans),
InitialBatch: productBatchInputToPb(req.InitialBatch),
})
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,
BatchId: p.BatchId,
})
}
return out
}
func productBatchInputToPb(input types.ProductBatchInput) *pb.ProductBatchInput {
if input == (types.ProductBatchInput{}) {
return nil
}
return &pb.ProductBatchInput{
BatchNo: input.BatchNo,
YarnRatio: input.YarnRatio,
WarpWeightGM: input.WarpWeightGM,
WeftWeightGM: input.WeftWeightGM,
ProductionProcess: input.ProductionProcess,
Remark: input.Remark,
}
}