106 lines
2.8 KiB
Go
106 lines
2.8 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 SavePansLogic struct {
|
||
|
|
ctx context.Context
|
||
|
|
svcCtx *svc.ServiceContext
|
||
|
|
logx.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewSavePansLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SavePansLogic {
|
||
|
|
return &SavePansLogic{
|
||
|
|
ctx: ctx,
|
||
|
|
svcCtx: svcCtx,
|
||
|
|
Logger: logx.WithContext(ctx),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *SavePansLogic) SavePans(in *pb.SavePansReq) (*pb.Empty, error) {
|
||
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
||
|
|
|
||
|
|
if err := l.svcCtx.BoltModel.DeleteByProductId(l.ctx, in.ProductId); err != nil {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
var totalLengthM float64
|
||
|
|
var totalBolts int
|
||
|
|
|
||
|
|
pans := make([]*model.InvProductPan, 0, len(in.Pans))
|
||
|
|
for i, panInput := range in.Pans {
|
||
|
|
panId := uid.Generate()
|
||
|
|
pans = append(pans, &model.InvProductPan{
|
||
|
|
PanId: panId,
|
||
|
|
ProductId: in.ProductId,
|
||
|
|
Name: panInput.Name,
|
||
|
|
Position: panInput.Position,
|
||
|
|
SortOrder: int64(i + 1),
|
||
|
|
TenantId: tenantId,
|
||
|
|
})
|
||
|
|
|
||
|
|
if len(panInput.BoltLengths) > 0 {
|
||
|
|
bolts := make([]*model.InvProductBolt, 0, len(panInput.BoltLengths))
|
||
|
|
for j, lengthStr := range panInput.BoltLengths {
|
||
|
|
lengthM, _ := strconv.ParseFloat(lengthStr, 64)
|
||
|
|
totalLengthM += lengthM
|
||
|
|
bolts = append(bolts, &model.InvProductBolt{
|
||
|
|
BoltId: uid.Generate(),
|
||
|
|
PanId: panId,
|
||
|
|
LengthM: lengthM,
|
||
|
|
SortOrder: int64(j + 1),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
totalBolts += len(panInput.BoltLengths)
|
||
|
|
if err := l.svcCtx.BoltModel.BulkReplace(l.ctx, panId, tenantId, bolts); err != nil {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := l.svcCtx.PanModel.BulkReplace(l.ctx, in.ProductId, tenantId, pans); err != nil {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
metrics.PansSavedTotal.WithLabelValues(tenantId, in.ProductId).Inc()
|
||
|
|
if totalLengthM > 0 {
|
||
|
|
productName := l.resolveProductName(in.ProductId)
|
||
|
|
color := l.resolveProductColor(in.ProductId)
|
||
|
|
metrics.InboundLengthMetersTotal.WithLabelValues(tenantId, productName, color, "save_pans").Add(totalLengthM)
|
||
|
|
}
|
||
|
|
if totalBolts > 0 {
|
||
|
|
metrics.InboundBoltsTotal.WithLabelValues(tenantId, "save_pans").Add(float64(totalBolts))
|
||
|
|
}
|
||
|
|
|
||
|
|
return &pb.Empty{}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *SavePansLogic) resolveProductName(productId string) string {
|
||
|
|
p, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, productId)
|
||
|
|
if err != nil {
|
||
|
|
return "unknown"
|
||
|
|
}
|
||
|
|
return p.ProductName
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *SavePansLogic) resolveProductColor(productId string) string {
|
||
|
|
p, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, productId)
|
||
|
|
if err != nil {
|
||
|
|
return "unknown"
|
||
|
|
}
|
||
|
|
return p.Color
|
||
|
|
}
|