You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.5 KiB
65 lines
1.5 KiB
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"healthapi/internal/model"
|
|
"healthapi/internal/svc"
|
|
"healthapi/internal/types"
|
|
"healthapi/pkg/errorx"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetPurchaseHistoryLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetPurchaseHistoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPurchaseHistoryLogic {
|
|
return &GetPurchaseHistoryLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetPurchaseHistoryLogic) GetPurchaseHistory(req *types.PageReq) (resp *types.PurchaseHistoryResp, err error) {
|
|
userID, err := GetUserIDFromCtx(l.ctx)
|
|
if err != nil {
|
|
return nil, errorx.ErrUnauthorized
|
|
}
|
|
|
|
var histories []model.PurchaseHistory
|
|
var total int64
|
|
|
|
query := l.svcCtx.DB.Model(&model.PurchaseHistory{}).Where("user_id = ?", userID)
|
|
query.Count(&total)
|
|
|
|
offset := (req.Page - 1) * req.PageSize
|
|
query.Order("purchased_at DESC").Offset(offset).Limit(req.PageSize).Find(&histories)
|
|
|
|
resp = &types.PurchaseHistoryResp{
|
|
History: make([]types.PurchaseHistory, 0, len(histories)),
|
|
PageInfo: types.PageInfo{
|
|
Total: total,
|
|
Page: req.Page,
|
|
PageSize: req.PageSize,
|
|
},
|
|
}
|
|
|
|
for _, h := range histories {
|
|
resp.History = append(resp.History, types.PurchaseHistory{
|
|
ID: uint(h.ID),
|
|
UserID: h.UserID,
|
|
OrderNo: h.OrderNo,
|
|
ProductID: h.ProductID,
|
|
ProductName: h.ProductName,
|
|
PurchasedAt: h.PurchasedAt.Format("2006-01-02 15:04:05"),
|
|
Source: h.Source,
|
|
})
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|