53d203ee14
CI / changes (push) Successful in 14s
CI / docker-bff (push) Successful in 3s
CI / docker-product (push) Successful in 24s
CI / docker-admin (push) Has been skipped
CI / docker-user (push) Has been skipped
CI / docker-ad (push) Has been skipped
CI / docker-chore (push) Successful in 25s
CI / docker-express (push) Has been skipped
CI / docker-sale (push) Successful in 26s
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
chore "lone-services/rpc/chore/pb"
|
|
"lone-services/services/chore/internal/dao"
|
|
"lone-services/services/chore/internal/model"
|
|
"lone-services/services/chore/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type RegionsByIdsLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewRegionsByIdsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegionsByIdsLogic {
|
|
return &RegionsByIdsLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// 服务间调用:按 id 批量查地区名称
|
|
func (l *RegionsByIdsLogic) RegionsByIds(in *chore.RegionsByIdsReq) (*chore.RegionsByIdsData, error) {
|
|
if len(in.GetIds()) < utils.NumberOne {
|
|
return nil, status.Error(codes.InvalidArgument, utils.ErrorParams.Msg)
|
|
}
|
|
|
|
seen := make(map[int64]struct{}, len(in.GetIds()))
|
|
ids := make([]string, 0, len(in.GetIds()))
|
|
for _, id := range in.GetIds() {
|
|
if id < utils.NumberOne {
|
|
continue
|
|
}
|
|
if _, ok := seen[id]; ok {
|
|
continue
|
|
}
|
|
seen[id] = struct{}{}
|
|
ids = append(ids, strconv.FormatInt(id, 10))
|
|
}
|
|
if len(ids) < utils.NumberOne {
|
|
return nil, status.Error(codes.InvalidArgument, utils.ErrorParams.Msg)
|
|
}
|
|
|
|
var list []dao.Region
|
|
if err := (model.RegionModel{}.Init().Items(modelbase.Params{
|
|
In: map[string][]string{"id in ?": ids},
|
|
}, &list)); err != nil {
|
|
l.Errorf("regions by ids: %v", err)
|
|
return nil, status.Error(codes.Internal, utils.Fail.Msg)
|
|
}
|
|
|
|
items := make([]*chore.RegionItem, 0, len(list))
|
|
for _, row := range list {
|
|
items = append(items, &chore.RegionItem{
|
|
Id: int64(row.Id),
|
|
Name: row.Name,
|
|
})
|
|
}
|
|
return &chore.RegionsByIdsData{Items: items}, nil
|
|
}
|