15a5616308
CI / changes (push) Successful in 1m31s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 0s
CI / aiserver (push) Successful in 0s
CI / analysis (push) Successful in 37s
CI / bff (push) Successful in 18s
CI / chore (push) Successful in 0s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 1s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 0s
CI / order (push) Successful in 0s
CI / payment (push) Successful in 0s
CI / percutaneous (push) Successful in 0s
CI / product (push) Successful in 0s
CI / record (push) Successful in 1s
CI / sale (push) Successful in 0s
CI / store (push) Successful in 22s
CI / support (push) Successful in 0s
CI / task (push) Successful in 0s
CI / user (push) Successful in 21s
CI / water (push) Successful in 0s
CI / wecom (push) Successful in 0s
CI / ws (push) Successful in 0s
163 lines
4.6 KiB
Go
163 lines
4.6 KiB
Go
package analysisadminlogic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
analysis "lone-services/rpc/analysis/pb"
|
|
userpb "lone-services/rpc/user/pb"
|
|
"lone-services/services/analysis/internal/dao"
|
|
"lone-services/services/analysis/internal/logic/common"
|
|
"lone-services/services/analysis/internal/model"
|
|
"lone-services/services/analysis/internal/svc"
|
|
"lone-services/services/analysis/validator"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type BindUserLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewBindUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindUserLogic {
|
|
return &BindUserLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *BindUserLogic) BindUser(in *analysis.AdminBindUserReq) (*analysis.Response, error) {
|
|
var req validator.AdminBindUserValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return outParams(msg), nil
|
|
}
|
|
if _, fail := common.RequireAdmin(l.ctx); fail != nil {
|
|
return fail, nil
|
|
}
|
|
|
|
params := modelbase.Params{
|
|
Eq: map[string]string{
|
|
"store_id": strconv.FormatInt(req.StoreId, utils.NumberTen),
|
|
"status": strconv.Itoa(int(dao.StatusEnabled)),
|
|
},
|
|
Order: "id desc",
|
|
Page: int(req.Page),
|
|
Size: int(req.Size),
|
|
}
|
|
|
|
if req.Keyword != utils.StringEmpty {
|
|
bizIds, err := l.filterBizIds(req.Keyword)
|
|
if err != nil {
|
|
l.Errorf("admin bind user filter: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if len(bizIds) == utils.NumberZero {
|
|
return common.OkResponse(modelbase.PageResult{Items: []dao.AdminBindUserItem{}}), nil
|
|
}
|
|
params.In = map[string][]string{"user_id": idStrings(bizIds)}
|
|
}
|
|
|
|
var authList []dao.UserStoreAuth
|
|
page, err := model.UserStoreAuthModel{}.Init().Page(params, &authList)
|
|
if err != nil {
|
|
l.Errorf("admin bind user page: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if len(authList) == utils.NumberZero {
|
|
page.Items = []dao.AdminBindUserItem{}
|
|
return common.OkResponse(page), nil
|
|
}
|
|
|
|
pageIds := make([]int64, 0, len(authList))
|
|
for _, row := range authList {
|
|
pageIds = append(pageIds, row.UserId)
|
|
}
|
|
profiles, err := l.loadProfiles(pageIds)
|
|
if err != nil {
|
|
l.Errorf("admin bind user profiles: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
|
|
items := make([]dao.AdminBindUserItem, 0, len(authList))
|
|
for _, row := range authList {
|
|
item := dao.AdminBindUserItem{
|
|
Id: row.Id,
|
|
UserId: row.UserId,
|
|
ValidityTime: row.ValidityTime.Format(bindValidityLayout),
|
|
}
|
|
if profile, ok := profiles[row.UserId]; ok {
|
|
item.Name = profile.GetName()
|
|
item.Nickname = profile.GetName()
|
|
item.Avatar = profile.GetAvatar()
|
|
mobile, _ := utils.DecryptPhone(profile.GetMobile())
|
|
item.Mobile = mobile
|
|
item.Gender = profile.GetGender()
|
|
item.GenderText = formatGender(item.Gender)
|
|
item.Birthday = profile.GetBirthday()
|
|
item.Age = utils.CalcAgeStr(item.Birthday)
|
|
} else {
|
|
item.GenderText = formatGender(utils.NumberZero)
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
page.Items = items
|
|
return common.OkResponse(page), nil
|
|
}
|
|
|
|
func (l *BindUserLogic) filterBizIds(keyword string) ([]int64, error) {
|
|
if l.svcCtx.UserSvcName == utils.StringEmpty {
|
|
return nil, status.Error(codes.FailedPrecondition, "user service empty")
|
|
}
|
|
cli, err := svc.GetRpcClient(l.svcCtx.UserSvcName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := userpb.NewUserClient(cli.Conn()).InternalUsersByBiz(l.ctx, &userpb.InternalUsersByBizReq{
|
|
ClientCode: dao.ClientCodeUserSkin,
|
|
Keyword: keyword,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ids := make([]int64, 0, len(res.GetItems()))
|
|
for _, item := range res.GetItems() {
|
|
if item.GetBizId() > utils.NumberZero {
|
|
ids = append(ids, item.GetBizId())
|
|
}
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
func (l *BindUserLogic) loadProfiles(bizIds []int64) (map[int64]*userpb.BizUserItem, error) {
|
|
out := make(map[int64]*userpb.BizUserItem, len(bizIds))
|
|
if len(bizIds) == utils.NumberZero {
|
|
return out, nil
|
|
}
|
|
if l.svcCtx.UserSvcName == utils.StringEmpty {
|
|
return nil, status.Error(codes.FailedPrecondition, "user service empty")
|
|
}
|
|
cli, err := svc.GetRpcClient(l.svcCtx.UserSvcName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := userpb.NewUserClient(cli.Conn()).InternalUsersByBiz(l.ctx, &userpb.InternalUsersByBizReq{
|
|
ClientCode: dao.ClientCodeUserSkin,
|
|
BizIds: bizIds,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, item := range res.GetItems() {
|
|
out[item.GetBizId()] = item
|
|
}
|
|
return out, nil
|
|
}
|