66 lines
1.6 KiB
Go
66 lines
1.6 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 ListPansLogic struct {
|
||
|
|
ctx context.Context
|
||
|
|
svcCtx *svc.ServiceContext
|
||
|
|
logx.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewListPansLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPansLogic {
|
||
|
|
return &ListPansLogic{
|
||
|
|
ctx: ctx,
|
||
|
|
svcCtx: svcCtx,
|
||
|
|
Logger: logx.WithContext(ctx),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *ListPansLogic) ListPans(in *pb.ListPanReq) (*pb.ListPanResp, error) {
|
||
|
|
pans, err := l.svcCtx.PanModel.FindByProductId(l.ctx, in.ProductId)
|
||
|
|
if err != nil {
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
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
|
||
|
|
}
|
||
|
|
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.ListPanResp{List: pbPans}, nil
|
||
|
|
}
|