Files
2026-10-19 17:30:45 +08:00

140 lines
4.3 KiB
Go

package logic
import (
"context"
"strconv"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
user "lone-services/rpc/user/pb"
"lone-services/services/user/internal/dao"
"lone-services/services/user/internal/model"
"lone-services/services/user/internal/svc"
"lone-services/services/user/validator"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type InternalUsersByBizLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewInternalUsersByBizLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InternalUsersByBizLogic {
return &InternalUsersByBizLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *InternalUsersByBizLogic) InternalUsersByBiz(in *user.InternalUsersByBizReq) (*user.InternalUsersByBizData, error) {
var req validator.InternalUsersByBizValidator
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
return nil, status.Error(codes.InvalidArgument, msg)
}
hasFilter := req.Keyword != utils.StringEmpty || req.Gender > utils.NumberZero || req.BirthdayGte != utils.StringEmpty || req.BirthdayLte != utils.StringEmpty
if len(req.BizIds) == utils.NumberZero && !hasFilter {
return nil, status.Error(codes.InvalidArgument, "请指定用户或筛选条件")
}
bizIds := make([]string, 0, len(req.BizIds))
seenBiz := make(map[int64]struct{}, len(req.BizIds))
for _, id := range req.BizIds {
if id < utils.NumberOne {
continue
}
if _, ok := seenBiz[id]; ok {
continue
}
seenBiz[id] = struct{}{}
bizIds = append(bizIds, strconv.FormatInt(id, utils.NumberTen))
}
clientWhere := modelbase.Params{
Eq: map[string]string{
"client_code": req.ClientCode,
"status": strconv.Itoa(int(dao.StatusEnabled)),
},
}
if len(bizIds) > utils.NumberZero {
clientWhere.In = map[string][]string{"biz_id": bizIds}
}
var clients []dao.UserClient
clientModel := model.UserClientModel{}.Init()
if err := clientModel.Items(clientWhere, &clients); err != nil {
l.Errorf("internal users by biz clients: %v", err)
return nil, status.Error(codes.Internal, utils.Fail.Msg)
}
if len(clients) == utils.NumberZero {
return &user.InternalUsersByBizData{Items: []*user.BizUserItem{}}, nil
}
userIds := make([]string, 0, len(clients))
bizByUser := make(map[int64]int64, len(clients))
seenUser := make(map[int64]struct{}, len(clients))
for _, row := range clients {
if row.UserId < utils.NumberOne || row.BizId < utils.NumberOne {
continue
}
bizByUser[row.UserId] = row.BizId
if _, ok := seenUser[row.UserId]; ok {
continue
}
seenUser[row.UserId] = struct{}{}
userIds = append(userIds, strconv.FormatInt(row.UserId, utils.NumberTen))
}
if len(userIds) == utils.NumberZero {
return &user.InternalUsersByBizData{Items: []*user.BizUserItem{}}, nil
}
userWhere := modelbase.Params{
In: map[string][]string{"id": userIds},
}
if req.Keyword != utils.StringEmpty {
userWhere.Like = map[string]string{"name LIKE ?": "%" + req.Keyword + "%"}
}
if req.Gender > utils.NumberZero {
if userWhere.Eq == nil {
userWhere.Eq = map[string]string{}
}
userWhere.Eq["gender"] = strconv.FormatUint(uint64(req.Gender), utils.NumberTen)
}
switch {
case req.BirthdayGte != utils.StringEmpty && req.BirthdayLte != utils.StringEmpty:
userWhere.Between = map[string][2]string{"birthday": {req.BirthdayGte, req.BirthdayLte}}
case req.BirthdayGte != utils.StringEmpty:
userWhere.Other = map[string]string{"birthday >= ?": req.BirthdayGte}
case req.BirthdayLte != utils.StringEmpty:
userWhere.Other = map[string]string{"birthday <= ?": req.BirthdayLte}
}
var rows []dao.UserRow
userModel := model.UserModel{}.Init()
if err := userModel.Items(userWhere, &rows); err != nil {
l.Errorf("internal users by biz users: %v", err)
return nil, status.Error(codes.Internal, utils.Fail.Msg)
}
items := make([]*user.BizUserItem, 0, len(rows))
for i := range rows {
items = append(items, &user.BizUserItem{
BizId: bizByUser[rows[i].Id],
UserId: rows[i].Id,
Mobile: rows[i].Mobile,
Name: rows[i].Name,
Avatar: rows[i].Avatar,
Gender: uint32(rows[i].Gender),
Birthday: rows[i].Birthday.DateString(),
Status: uint32(rows[i].Status),
})
}
return &user.InternalUsersByBizData{Items: items}, nil
}