app-cloudep-member-server/internal/logic/account/list_member_logic.go

160 lines
3.4 KiB
Go

package accountlogic
import (
"app-cloudep-member-server/internal/domain"
"app-cloudep-member-server/internal/model"
"context"
"fmt"
"google.golang.org/protobuf/proto"
"strconv"
ers "code.30cm.net/digimon/library-go/errors"
"app-cloudep-member-server/gen_result/pb/member"
"app-cloudep-member-server/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type ListMemberLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewListMemberLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListMemberLogic {
return &ListMemberLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// ListMember 取得會員列表
func (l *ListMemberLogic) ListMember(in *member.ListUserInfoReq) (*member.ListUserInfoResp, error) {
params, err := parse(in)
if err != nil {
return nil, ers.InvalidFormat("failed to get correct param", err.Error())
}
members, err := l.svcCtx.UserModel.ListMembers(l.ctx, &params)
if err != nil {
return nil, ers.DBError(err.Error())
}
count, err := l.svcCtx.UserModel.Count(l.ctx)
if err != nil {
return nil, ers.DBError(err.Error())
}
var data = make([]*member.UserInfo, 0, len(members))
for _, item := range members {
data = append(data, &member.UserInfo{
Uid: item.Uid,
VerifyType: member.VerifyType(item.VerifyType),
AlarmType: member.AlarmType(item.AlarmType),
Status: member.MemberStatus(item.Status),
Language: item.Language,
Currency: item.Currency,
NickName: proto.String(item.NickName),
CreateTime: item.CreateTime,
UpdateTime: item.UpdateTime,
Avatar: item.Avatar,
})
}
return &member.ListUserInfoResp{
Data: data,
Page: &member.Pager{
Total: count,
Size: in.PageSize,
Index: in.PageIndex,
},
}, nil
}
func parse(in *member.ListUserInfoReq) (model.UserQueryParams, error) {
var filter model.UserQueryParams
// 設置 PageSize
filter.PageSize = getValidPageSize(in.PageSize)
// 設置 PageIndex
filter.PageIndex = getValidPageIndex(in.PageIndex)
// 設置 VerifyType
svt := fmt.Sprintf("%d", in.GetVerifyType().Number())
vt, err := getInt32FromStringPointer(&svt)
if err != nil {
return model.UserQueryParams{}, err
}
if vt != nil {
filter.VerifyType = vt
}
// 設置 AlarmType
sat := fmt.Sprintf("%d", in.GetAlarmType().Number())
at, err := getInt32FromStringPointer(&sat)
if err != nil {
return model.UserQueryParams{}, err
}
if at != nil {
filter.AlarmType = at
}
// 設置 Status
sst := fmt.Sprintf("%d", in.GetStatus().Number())
st, err := getInt32FromStringPointer(&sst)
if err != nil {
return model.UserQueryParams{}, err
}
if st != nil {
filter.Status = st
}
// 設置 CreateStartTime
if in.CreateStartTime != nil {
ct := in.GetCreateStartTime()
filter.CreateStartTime = &ct
}
// 設置 CreateEndTime
if in.CreateEndTime != nil {
et := in.GetCreateEndTime()
filter.CreateEndTime = &et
}
return filter, nil
}
func getValidPageSize(pageSize int64) int64 {
if pageSize <= 0 {
return domain.DefaultPageSize
}
return pageSize
}
func getValidPageIndex(pageIndex int64) int64 {
if pageIndex <= 0 {
return domain.DefaultPageIndex
}
return pageIndex
}
func getInt32FromStringPointer(strPtr *string) (*int32, error) {
if strPtr == nil {
return nil, nil
}
value, err := strconv.Atoi(*strPtr)
if err != nil {
return nil, err
}
int32Value := int32(value)
if int32Value == 0 {
return nil, nil
}
return &int32Value, nil
}