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.
95 lines
2.5 KiB
95 lines
2.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 GetOrderLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOrderLogic {
|
|
return &GetOrderLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetOrderLogic) GetOrder(req *types.OrderIdReq) (resp *types.Order, err error) {
|
|
userID, err := GetUserIDFromCtx(l.ctx)
|
|
if err != nil {
|
|
return nil, errorx.ErrUnauthorized
|
|
}
|
|
|
|
var order model.Order
|
|
if err := l.svcCtx.DB.Where("id = ? AND user_id = ?", req.Id, userID).First(&order).Error; err != nil {
|
|
return nil, errorx.NewCodeError(404, "订单不存在")
|
|
}
|
|
|
|
// 获取订单商品
|
|
var items []model.OrderItem
|
|
l.svcCtx.DB.Where("order_id = ?", order.ID).Find(&items)
|
|
|
|
orderItems := make([]types.OrderItem, 0, len(items))
|
|
for _, item := range items {
|
|
orderItems = append(orderItems, types.OrderItem{
|
|
ID: uint(item.ID),
|
|
ProductID: item.ProductID,
|
|
SkuID: item.SkuID,
|
|
ProductName: item.ProductName,
|
|
SkuName: item.SkuName,
|
|
Image: item.Image,
|
|
Price: item.Price,
|
|
Quantity: item.Quantity,
|
|
TotalAmount: item.TotalAmount,
|
|
})
|
|
}
|
|
|
|
payTime, shipTime, receiveTime := "", "", ""
|
|
if order.PayTime != nil {
|
|
payTime = order.PayTime.Format("2006-01-02 15:04:05")
|
|
}
|
|
if order.ShipTime != nil {
|
|
shipTime = order.ShipTime.Format("2006-01-02 15:04:05")
|
|
}
|
|
if order.ReceiveTime != nil {
|
|
receiveTime = order.ReceiveTime.Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
resp = &types.Order{
|
|
ID: uint(order.ID),
|
|
OrderNo: order.OrderNo,
|
|
Status: order.Status,
|
|
TotalAmount: order.TotalAmount,
|
|
DiscountAmount: order.DiscountAmount,
|
|
ShippingFee: order.ShippingFee,
|
|
PayAmount: order.PayAmount,
|
|
PointsUsed: order.PointsUsed,
|
|
PointsEarned: order.PointsEarned,
|
|
PayMethod: order.PayMethod,
|
|
PayTime: payTime,
|
|
ShipTime: shipTime,
|
|
ReceiveTime: receiveTime,
|
|
ReceiverName: order.ReceiverName,
|
|
ReceiverPhone: order.ReceiverPhone,
|
|
ReceiverAddr: order.ReceiverAddr,
|
|
ShippingCompany: order.ShippingCompany,
|
|
TrackingNo: order.TrackingNo,
|
|
Remark: order.Remark,
|
|
CancelReason: order.CancelReason,
|
|
Items: orderItems,
|
|
CreatedAt: order.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|