feat: internal product items
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"lone-services/pkg/modelbase"
|
||||
"lone-services/pkg/utils"
|
||||
product "lone-services/rpc/product/pb"
|
||||
"lone-services/services/product/internal/dao"
|
||||
"lone-services/services/product/internal/model"
|
||||
"lone-services/services/product/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type InternalItemsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewInternalItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InternalItemsLogic {
|
||||
return &InternalItemsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *InternalItemsLogic) InternalItems(in *product.InternalItemsReq) (*product.ItemsByIdsData, error) {
|
||||
w := modelbase.Params{
|
||||
Order: "sort DESC, id DESC",
|
||||
}
|
||||
inMap := make(map[string][]string)
|
||||
if status := toUint32Strings(in.GetStatus()); len(status) > utils.NumberZero {
|
||||
inMap["status in ?"] = status
|
||||
}
|
||||
if types := toUint32Strings(in.GetType()); len(types) > utils.NumberZero {
|
||||
inMap["type in ?"] = types
|
||||
}
|
||||
if len(inMap) > utils.NumberZero {
|
||||
w.In = inMap
|
||||
}
|
||||
|
||||
var infos []dao.Info
|
||||
err := model.ProductModel{}.Init().Items(w, &infos)
|
||||
if err != nil {
|
||||
l.Errorf("product InternalItems: %v", err)
|
||||
return nil, status.Error(codes.Internal, utils.Fail.Msg)
|
||||
}
|
||||
|
||||
items := make([]*product.Item, 0, len(infos))
|
||||
for _, row := range infos {
|
||||
items = append(items, toProductItem(row))
|
||||
}
|
||||
return &product.ItemsByIdsData{Items: items}, nil
|
||||
}
|
||||
|
||||
func toUint32Strings(vals []uint32) []string {
|
||||
if len(vals) < 1 {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(vals))
|
||||
for _, v := range vals {
|
||||
out = append(out, strconv.FormatUint(uint64(v), 10))
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user