fusenapi/server/order/internal/logic/createorderlogic.go

83 lines
2.2 KiB
Go
Raw Normal View History

2023-09-20 07:07:12 +00:00
package logic
import (
2023-10-07 08:12:29 +00:00
"fmt"
2023-09-20 07:07:12 +00:00
"fusenapi/constants"
"fusenapi/service/repositories"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"time"
"context"
"fusenapi/server/order/internal/svc"
"fusenapi/server/order/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CreateOrderLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
return &CreateOrderLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *CreateOrderLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
2023-09-20 10:04:33 +00:00
if !userinfo.IsUser() {
2023-09-20 07:07:12 +00:00
// 如果是,返回未授权的错误码
return resp.SetStatus(basic.CodeUnAuth)
}
2023-09-27 09:23:58 +00:00
tPlus60Days := time.Now().AddDate(0, 0, 60).UTC()
2023-09-20 07:07:12 +00:00
res, err := l.svcCtx.Repositories.NewOrder.Create(l.ctx, &repositories.CreateReq{
ExpectedDeliveryTime: tPlus60Days,
CurrentCurrency: string(constants.CURRENCYUSD),
OriginalCurrency: string(constants.CURRENCYUSD),
UserId: userinfo.UserId,
CartIds: req.CartIds,
2023-09-22 03:00:54 +00:00
DeliveryMethod: constants.DELIVERYMETHODDSCLOUDSTORE,
2023-09-20 07:07:12 +00:00
})
if err != nil {
return resp.SetStatus(&res.ErrorCode)
}
2023-10-07 08:12:29 +00:00
// 延时任务
2023-10-07 08:14:46 +00:00
time.AfterFunc(time.Minute*30, func() {
2023-10-07 08:12:29 +00:00
orderSn := res.OrderSn
fmt.Println("延时任务: OrderSn--", orderSn)
ctx := context.Background()
svcCtx := svc.ServiceContext{
Config: l.svcCtx.Config,
Repositories: l.svcCtx.Repositories,
}
svcCtx.Repositories.NewOrder.Close(ctx, &repositories.CloseReq{
OrderSn: orderSn,
Type: 1,
})
})
2023-09-20 10:04:33 +00:00
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"order_sn": res.OrderSn,
})
2023-09-20 07:07:12 +00:00
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *CreateOrderLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }