77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
sale "lone-services/rpc/sale/pb"
|
|
"lone-services/services/sale/internal/dao"
|
|
"lone-services/services/sale/internal/model"
|
|
"lone-services/services/sale/internal/svc"
|
|
"lone-services/services/sale/validator"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type NamesByIdsLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewNamesByIdsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NamesByIdsLogic {
|
|
return &NamesByIdsLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *NamesByIdsLogic) NamesByIds(in *sale.NamesByIdsReq) (*sale.NamesByIdsData, error) {
|
|
var req validator.NamesByIdsValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return nil, status.Error(codes.InvalidArgument, msg)
|
|
}
|
|
if len(req.Ids) < utils.NumberOne {
|
|
return &sale.NamesByIdsData{Items: []*sale.NamesByIdsItem{}}, nil
|
|
}
|
|
|
|
seen := make(map[int64]struct{}, len(req.Ids))
|
|
ids := make([]string, 0, len(req.Ids))
|
|
for _, id := range req.Ids {
|
|
if id < utils.NumberOne {
|
|
continue
|
|
}
|
|
if _, ok := seen[id]; ok {
|
|
continue
|
|
}
|
|
seen[id] = struct{}{}
|
|
ids = append(ids, strconv.FormatInt(id, utils.NumberTen))
|
|
}
|
|
if len(ids) < utils.NumberOne {
|
|
return &sale.NamesByIdsData{Items: []*sale.NamesByIdsItem{}}, nil
|
|
}
|
|
|
|
var rows []dao.SaleNameItem
|
|
if err := (model.SaleModel{}.Init().Items(modelbase.Params{
|
|
In: map[string][]string{"id in ?": ids},
|
|
}, &rows)); err != nil {
|
|
l.Errorf("sale NamesByIds: %v", err)
|
|
return nil, status.Error(codes.Internal, utils.Fail.Msg)
|
|
}
|
|
|
|
items := make([]*sale.NamesByIdsItem, 0, len(rows))
|
|
for _, row := range rows {
|
|
items = append(items, &sale.NamesByIdsItem{
|
|
Id: row.Id,
|
|
Name: row.Name,
|
|
})
|
|
}
|
|
return &sale.NamesByIdsData{Items: items}, nil
|
|
}
|