2026-02-28 15:29:16 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"muyu-apiserver/model"
|
2026-03-30 02:53:55 +00:00
|
|
|
"muyu-apiserver/pkg/tenantctx"
|
2026-02-28 15:29:16 +08:00
|
|
|
"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 GetStockGroupLogic struct {
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
logx.Logger
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewGetStockGroupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetStockGroupLogic {
|
|
|
|
|
return &GetStockGroupLogic{
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *GetStockGroupLogic) GetStockGroup(in *pb.StockGroupReq) (*pb.StockGroupResp, error) {
|
2026-03-30 02:53:55 +00:00
|
|
|
tenantId := tenantctx.ExtractTenantId(l.ctx)
|
2026-02-28 15:29:16 +08:00
|
|
|
var list []model.StockGroupResult
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
switch in.GroupBy {
|
|
|
|
|
case "color":
|
2026-03-30 02:53:55 +00:00
|
|
|
list, err = l.svcCtx.ProductModel.FindGroupByColor(l.ctx, tenantId)
|
2026-02-28 15:29:16 +08:00
|
|
|
case "location":
|
2026-03-30 02:53:55 +00:00
|
|
|
list, err = l.svcCtx.ProductModel.FindGroupByLocation(l.ctx, tenantId)
|
2026-02-28 15:29:16 +08:00
|
|
|
default:
|
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "unsupported group_by: %s", in.GroupBy)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pbList := make([]*pb.StockGroupItem, 0, len(list))
|
|
|
|
|
for _, item := range list {
|
|
|
|
|
pbList = append(pbList, &pb.StockGroupItem{
|
|
|
|
|
Name: item.Name,
|
|
|
|
|
Count: item.Count,
|
|
|
|
|
Quantity: fmt.Sprintf("%.2f", item.Quantity),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.StockGroupResp{List: pbList}, nil
|
|
|
|
|
}
|