// Code generated by hertz generator.

package order

import (
	"apis/biz/utils"
	common "apis/hertz_gen/api/common"
	order "apis/hertz_gen/api/order"
	"apis/rpc"
	"context"
	"github.com/BeroKiTeer/MyGoMall/common/kitex_gen/auth"
	order_kitex "github.com/BeroKiTeer/MyGoMall/common/kitex_gen/order"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

// PlaceOrder .
// @router /api/cart/add [POST]
func PlaceOrder(ctx context.Context, c *app.RequestContext) {
	var err error
	var req order.PlaceOrderReq
	err = c.BindAndValidate(&req)
	if err != nil {
		c.String(consts.StatusBadRequest, err.Error())
		return
	}

	//获取请求头的token
	token := c.Request.Header.Get("Authorization")
	if token == "" {
		hlog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusUnauthorized, err)
		return
	}
	//获取用户id
	rawID, err := rpc.AuthClient.DecodeToken(ctx, &auth.DecodeTokenReq{Token: token})
	if err != nil {
		hlog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusInternalServerError, err)
		return
	}

	placeOrder, err := rpc.OrderClient.PlaceOrder(ctx, &order_kitex.PlaceOrderReq{
		UserId: uint32(rawID.UserId),
	})
	hlog.Info(placeOrder)
	if err != nil {
		hlog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}

	resp := new(common.Empty)

	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}
