57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
|
|
package logic
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"muyu-apiserver/model"
|
||
|
|
"muyu-apiserver/pkg/tenantctx"
|
||
|
|
"muyu-apiserver/pkg/uid"
|
||
|
|
"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 CreateProductBatchLogic struct {
|
||
|
|
ctx context.Context
|
||
|
|
svcCtx *svc.ServiceContext
|
||
|
|
logx.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewCreateProductBatchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateProductBatchLogic {
|
||
|
|
return &CreateProductBatchLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *CreateProductBatchLogic) CreateProductBatch(in *pb.CreateProductBatchReq) (*pb.IdResp, error) {
|
||
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
||
|
|
product, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, in.ProductId)
|
||
|
|
if err != nil || product.TenantId != tenantId {
|
||
|
|
return nil, status.Error(codes.NotFound, "product not found")
|
||
|
|
}
|
||
|
|
batchNo, err := l.svcCtx.ProductBatchModel.NextBatchNo(l.ctx, tenantId, product.ProductCode)
|
||
|
|
if err != nil {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
if requestedBatchNo := strings.TrimSpace(in.Batch.GetBatchNo()); requestedBatchNo != "" {
|
||
|
|
batchNo = requestedBatchNo
|
||
|
|
}
|
||
|
|
batchId := uid.Generate()
|
||
|
|
batch := &model.InvProductBatch{
|
||
|
|
BatchId: batchId,
|
||
|
|
TenantId: tenantId,
|
||
|
|
ProductId: product.ProductId,
|
||
|
|
BatchNo: batchNo,
|
||
|
|
Status: 1,
|
||
|
|
}
|
||
|
|
if err := batchInputToModelFields(in.Batch, batch); err != nil {
|
||
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||
|
|
}
|
||
|
|
if _, err := l.svcCtx.ProductBatchModel.Insert(l.ctx, batch); err != nil {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
return &pb.IdResp{Id: batchId}, nil
|
||
|
|
}
|