muyu-apiserver/rpc/inventory/internal/logic/getproducttreelogic.go

96 lines
2.5 KiB
Go
Raw Normal View History

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())
}
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,
Name: pan.Name,
Position: pan.Position,
SortOrder: pan.SortOrder,
Bolts: pbBolts,
BoltCount: int64(len(bolts)),
TotalLength: fmt.Sprintf("%.2f", panLength),
})
}
return &pb.ProductTreeResp{
Product: &pb.ProductInfo{
ProductId: product.ProductId,
ProductName: product.ProductName,
ImageUrl: product.ImageUrl,
Spec: product.Spec,
Color: product.Color,
SalesPrice: fmt.Sprintf("%.2f", product.SalesPrice),
Remark: product.Remark.String,
Status: product.Status,
CreatedAt: product.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: product.UpdatedAt.Format("2006-01-02 15:04:05"),
},
Pans: pbPans,
TotalPanCount: int64(len(pans)),
TotalBoltCount: totalBoltCount,
TotalLengthM: fmt.Sprintf("%.2f", totalLengthM),
}, nil
}