* feat: support product yarn batches * feat: improve product yarn ratio workflow * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"muyu-apiserver/model"
|
|
"muyu-apiserver/pkg/metrics"
|
|
"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 SaveBoltsLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewSaveBoltsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveBoltsLogic {
|
|
return &SaveBoltsLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *SaveBoltsLogic) SaveBolts(in *pb.SaveBoltsReq) (*pb.Empty, error) {
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
|
pan, err := l.svcCtx.PanModel.FindOneByPanId(l.ctx, in.PanId)
|
|
if err != nil || pan.TenantId != tenantId {
|
|
return nil, status.Error(codes.NotFound, "pan not found")
|
|
}
|
|
product, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, pan.ProductId)
|
|
if err != nil || product.TenantId != tenantId {
|
|
return nil, status.Error(codes.NotFound, "product not found")
|
|
}
|
|
|
|
var totalLengthM float64
|
|
bolts := make([]*model.InvProductBolt, 0, len(in.Lengths))
|
|
for i, lengthStr := range in.Lengths {
|
|
lengthM, _ := strconv.ParseFloat(lengthStr, 64)
|
|
totalLengthM += lengthM
|
|
bolts = append(bolts, &model.InvProductBolt{
|
|
BoltId: uid.Generate(),
|
|
PanId: in.PanId,
|
|
LengthM: lengthM,
|
|
Color: product.Color,
|
|
SortOrder: int64(i + 1),
|
|
TenantId: tenantId,
|
|
})
|
|
}
|
|
|
|
if err := l.svcCtx.BoltModel.BulkReplace(l.ctx, in.PanId, tenantId, bolts); err != nil {
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
}
|
|
|
|
metrics.InboundBoltsTotal.WithLabelValues(tenantId, "save_bolts").Add(float64(len(in.Lengths)))
|
|
if totalLengthM > 0 {
|
|
metrics.InboundLengthMetersTotal.WithLabelValues(tenantId, "", "", "save_bolts").Add(totalLengthM)
|
|
}
|
|
|
|
return &pb.Empty{}, nil
|
|
}
|