muyu-apiserver/rpc/inventory/internal/logic/getproducttreelogic.go
kae_mihara e3f6fa236d
feat:improve product yarn ratio workflow (#5)
* 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>
2026-07-05 08:06:28 +09:00

95 lines
2.4 KiB
Go

package logic
import (
"context"
"fmt"
"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 GetProductTreeLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetProductTreeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductTreeLogic {
return &GetProductTreeLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetProductTreeLogic) GetProductTree(in *pb.GetProductReq) (*pb.ProductTreeResp, error) {
product, err := l.svcCtx.ProductModel.FindOneByProductId(l.ctx, in.ProductId)
if err != nil {
return nil, status.Error(codes.NotFound, "product not found")
}
pans, err := l.svcCtx.PanModel.FindByProductId(l.ctx, in.ProductId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
batches, err := l.svcCtx.ProductBatchModel.FindByProductId(l.ctx, product.TenantId, in.ProductId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
batchNoById := make(map[string]string, len(batches))
for _, b := range batches {
batchNoById[b.BatchId] = b.BatchNo
}
var totalBoltCount int64
var totalLengthM float64
pbPans := make([]*pb.PanInfo, 0, len(pans))
for _, pan := range pans {
bolts, err := l.svcCtx.BoltModel.FindByPanId(l.ctx, pan.PanId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
var panLength float64
pbBolts := make([]*pb.BoltInfo, 0, len(bolts))
for _, b := range bolts {
pbBolts = append(pbBolts, &pb.BoltInfo{
BoltId: b.BoltId,
PanId: b.PanId,
LengthM: fmt.Sprintf("%.2f", b.LengthM),
SortOrder: b.SortOrder,
})
panLength += b.LengthM
}
totalBoltCount += int64(len(bolts))
totalLengthM += panLength
pbPans = append(pbPans, &pb.PanInfo{
PanId: pan.PanId,
ProductId: pan.ProductId,
BatchId: pan.BatchId,
BatchNo: batchNoById[pan.BatchId],
Name: pan.Name,
Position: pan.Position,
SortOrder: pan.SortOrder,
Bolts: pbBolts,
BoltCount: int64(len(bolts)),
TotalLength: fmt.Sprintf("%.2f", panLength),
})
}
return &pb.ProductTreeResp{
Product: productToPb(product),
Pans: pbPans,
TotalPanCount: int64(len(pans)),
TotalBoltCount: totalBoltCount,
TotalLengthM: fmt.Sprintf("%.2f", totalLengthM),
}, nil
}