3 Star 1 Fork 0

lzz/orders

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
getorderbysnlogic.go 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
package orderslogic
import (
"context"
"fmt"
"gitee.com/lzzandlyx/orders/model"
"github.com/google/uuid"
"github.com/jinzhu/copier"
"time"
"gitee.com/lzzandlyx/orders/internal/svc"
"gitee.com/lzzandlyx/orders/orders"
"github.com/zeromicro/go-zero/core/logx"
)
type GetOrderBySnLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetOrderBySnLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOrderBySnLogic {
return &GetOrderBySnLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetOrderBySnLogic) GetOrderBySn(in *orders.GetOrderBySnRequest) (*orders.GetOrderBySnResponse, error) {
if _, err := uuid.Parse(in.Sn); err != nil {
return nil, fmt.Errorf("订单编号校验不通过: %v", err)
}
orderInfo, err := l.getOrder(in)
if err != nil {
return nil, err
}
err = l.getOrderItem(orderInfo)
if err != nil {
return nil, err
}
err = l.getOrderAddress(orderInfo)
if err != nil {
return nil, err
}
return &orders.GetOrderBySnResponse{OrderInfo: orderInfo}, nil
}
func (l *GetOrderBySnLogic) getOrder(in *orders.GetOrderBySnRequest) (*orders.OrderInfo, error) {
order, err := model.GetOrderBySn(l.svcCtx, in.Sn)
var orderInfo orders.OrderInfo
err = copier.Copy(&orderInfo, &order)
if err != nil {
return nil, err
}
orderInfo.OrderId = int64(order.ID)
orderInfo.PayTime = order.PayTime.Format(time.DateTime)
return &orderInfo, nil
}
func (l *GetOrderBySnLogic) getOrderItem(orderInfo *orders.OrderInfo) (err error) {
items, err := model.GetOrderItem(l.svcCtx, orderInfo.OrderId)
var itemInfos []*orders.OrderItemInfo
for _, v := range items {
itemInfos = append(itemInfos, &orders.OrderItemInfo{
OrderItemId: int64(v.ID),
OrderId: v.OrderId,
GoodsId: v.GoodsId,
Quantity: v.Quantity,
GoodsTitle: v.GoodsTitle,
GoodsPrice: v.GoodsPrice,
Img: v.Img,
})
}
orderInfo.Items = itemInfos
return
}
func (l *GetOrderBySnLogic) getOrderAddress(orderInfo *orders.OrderInfo) error {
address, err := model.GetOrderAddress(l.svcCtx, orderInfo.OrderId)
if err != nil {
return err
}
orderInfo.OrderAddr = &orders.OrderAddr{
OrderAddrId: int64(address.ID),
OrderId: address.OrderId,
Mobile: address.Mobile,
Nickname: address.Nickname,
Address: address.Address,
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lzzandlyx/orders.git
git@gitee.com:lzzandlyx/orders.git
lzzandlyx
orders
orders
bb9100882654

搜索帮助