88 lines
2.1 KiB
Go
88 lines
2.1 KiB
Go
package staff
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"bsm/full/module/ec/mall/internal/impl"
|
||
"bsm/full/module/ec/mall/internal/models"
|
||
pb "bsm/full/module/ec/mall/pb"
|
||
"git.apinb.com/bsm-sdk/core/errcode"
|
||
"git.apinb.com/bsm-sdk/core/printer"
|
||
"git.apinb.com/bsm-sdk/core/service"
|
||
)
|
||
|
||
// 获取工作人员列表
|
||
func Fetch(ctx context.Context, in *pb.MallFetchRequest) (reply *pb.StaffListReply, err error) {
|
||
// parse authorization meta.
|
||
auth, err := service.ParseMetaCtx(ctx, &service.ParseOptions{RoleValue: "Mall_Admin"})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 仅可查询当前登录者所属店铺的员工,店铺取自 token,不采信请求参数
|
||
owner, _ := auth.Owner.(map[string]any)
|
||
storeIdentity, _ := owner["store_identity"].(string)
|
||
if storeIdentity == "" {
|
||
return nil, errcode.ErrPermissionDenied
|
||
}
|
||
|
||
var (
|
||
cnt int64 = 0
|
||
pageNo int64 = in.GetPageNo()
|
||
pageSize int64 = in.GetPageSize()
|
||
offset int64 = (pageNo - 1) * pageSize
|
||
params = map[string]string{
|
||
"store_identity": storeIdentity,
|
||
}
|
||
)
|
||
|
||
// vlidate request page_no,page_size.
|
||
if pageNo < 1 {
|
||
pageNo = 1
|
||
offset = 0
|
||
}
|
||
if pageSize < 50 {
|
||
pageSize = 50
|
||
offset = (pageNo - 1) * pageSize
|
||
}
|
||
|
||
var data []*models.MallStaff
|
||
if err := impl.DBService.Model(&models.MallStaff{}).Where(params).Order("created_at desc").Count(&cnt).
|
||
Offset(int(offset)).Limit(int(pageSize)).Find(&data).Error; err != nil {
|
||
printer.Error(err.Error())
|
||
return nil, errcode.ErrDB
|
||
}
|
||
|
||
// TODO: add your logic code & delete this line.
|
||
|
||
return &pb.StaffListReply{
|
||
Count: cnt,
|
||
Data: ModelToReply(data),
|
||
}, nil
|
||
}
|
||
func ModelToReply(data []*models.MallStaff) (list []*pb.StaffItem) {
|
||
if len(data) == 0 {
|
||
return
|
||
}
|
||
|
||
for _, in := range data {
|
||
row := pb.StaffItem{
|
||
Id: int64(in.ID),
|
||
Identity: in.Identity,
|
||
Name: in.Name,
|
||
Account: in.Account,
|
||
Profile: in.Profile,
|
||
Role: in.Role,
|
||
Phone: in.Phone,
|
||
Email: in.Email,
|
||
Status: int32(in.Status),
|
||
Avatar: in.Avatar,
|
||
CreatedAt: in.CreatedAt.Format(time.DateTime),
|
||
}
|
||
|
||
list = append(list, &row)
|
||
}
|
||
return
|
||
}
|